UI组件之TextView及其子类(二)RadioButton和CheckBox
生活随笔
收集整理的這篇文章主要介紹了
UI组件之TextView及其子类(二)RadioButton和CheckBox
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
單選按鈕(RadioButton)和復(fù)選框(CheckBox),狀態(tài)開關(guān)按鈕(ToggleButton),開關(guān)(Switch)都是普通的UI組件,都繼承了Button類,因此都可以用Button的各種屬性和方法。
RadioButton通常要與RadioGroup一起使用,用于定義一組單選按鈕
對(duì)于二者而言,最主要的還是要看他們的監(jiān)聽(tīng)器,RadioButton的事件監(jiān)聽(tīng)器是:
單選按鈕的監(jiān)聽(tīng)接口是OnCheckedChangeListener
radiobutton.setOnCheckedChangeListener(new OnCheckedChangeListener(){//單選框的監(jiān)聽(tīng)器,OnCheckedChangeListener是單選框的監(jiān)聽(tīng)接口,為radiogroup組建的Oncheck事件綁定監(jiān)聽(tīng)器@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stub//處理操作} }); 而復(fù)選框CheckBox的監(jiān)聽(tīng)器是:注意復(fù)選框的監(jiān)聽(tīng)接口是CompoundButton.OnCheckedChangeListener
//復(fù)選框的監(jiān)聽(tīng)器,CompoundButton.OnCheckedChangeListener是復(fù)選框的監(jiān)聽(tīng)接口checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {// TODO Auto-generated method stub//處理操作}});例:
main.xml
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TableRowandroid:id="@+id/tableRow1"android:layout_width="wrap_content"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="性 別:"android:textSize="23dp" /><!-- RadioButton需要在RadioGroup組件里面 --><RadioGroupandroid:id="@+id/radioGroup1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal" ><RadioButtonandroid:id="@+id/radio0"android:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="true"android:text="男"android:textSize="23dp" /><RadioButtonandroid:id="@+id/radio1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女"android:textSize="23dp" /></RadioGroup></TableRow><TableRowandroid:id="@+id/tableRow2"android:layout_width="wrap_content"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="喜歡的顏色:"android:textSize="23dp" /><!-- 線性布局組件中有三個(gè)復(fù)選框 --><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical" ><CheckBoxandroid:id="@+id/checkBox1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="紅色" android:textSize="23dp"/><CheckBoxandroid:id="@+id/checkBox2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="藍(lán)色" android:textSize="23dp"/><CheckBoxandroid:id="@+id/checkBox3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="綠色" android:textSize="23dp"/></LinearLayout></TableRow><TextViewandroid:id="@+id/textView3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="23dp" /><TextViewandroid:id="@+id/textView4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20dp" /></TableLayout>
MainActivity.java
package com.hust.radiocheckbuttontest;import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.TextView;public class MainActivity extends Activity {String str=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//從布局文件中獲取radiogroup組件和textview組件RadioGroup rg=(RadioGroup) findViewById(R.id.radioGroup1);final TextView showradio=(TextView) findViewById(R.id.textView3);final TextView showcheck=(TextView) findViewById(R.id.textView4);//獲取復(fù)選框組件final CheckBox red=(CheckBox) findViewById(R.id.checkBox1);final CheckBox blue=(CheckBox) findViewById(R.id.checkBox2);final CheckBox green=(CheckBox) findViewById(R.id.checkBox3);//str.append("喜歡的顏色是:");//單選框的監(jiān)聽(tīng)器,OnCheckedChangeListener是單選框的監(jiān)聽(tīng)接口,為radiogroup組建的Oncheck事件綁定監(jiān)聽(tīng)器rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stub//根據(jù)備選按鈕的Id改變s的值String s=checkedId==R.id.radio0?"您的性別是男人":"您的性別是女人";//文本框顯示文本showradio.setText(s);} });//復(fù)選框的監(jiān)聽(tīng)器,CompoundButton.OnCheckedChangeListener是復(fù)選框的監(jiān)聽(tīng)接口red.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {// TODO Auto-generated method stubif(isChecked){ str=(String) red.getText();showcheck.setText(str);}}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);} }
總結(jié)
以上是生活随笔為你收集整理的UI组件之TextView及其子类(二)RadioButton和CheckBox的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: UI组件之TextView及其子类(一)
- 下一篇: UI组件之TextView及其子类(三)