android單選框被選中的變更監聽事件
阿新 • • 發佈:2019-02-20
RadioButton和CheckBox的區別:
1、單個RadioButton在選中後,通過點選無法變為未選中
單個CheckBox在選中後,通過點選可以變為未選中
2、一組RadioButton,只能同時選中一個
一組CheckBox,能同時選中多個
3、RadioButton在大部分UI框架中預設都以圓形表示
CheckBox在大部分UI框架中預設都以矩形表示
RadioButton和RadioGroup的關係:
1、RadioButton表示單個圓形單選框,而RadioGroup是可以容納多個RadioButton的容器
2、每個RadioGroup中的RadioButton同時只能有一個被選中
3、不同的RadioGroup中的RadioButton互不相干,即如果組A中有一個選中了,組B中依然可以有一個被選中
4、大部分場合下,一個RadioGroup中至少有2個RadioButton
5、大部分場合下,一個RadioGroup中的RadioButton預設會有一個被選中,並建議您將它放在RadioGroup中的起始位置
XML佈局:
選中項變更的事件監聽:
當RadioGroup中的選中項變更後,您可能需要做一些相應,比如上述例子中,性別選擇“女”後下面的本文也相應改變,又或者選擇不同的性別後,出現符合該性別的頭像列表進行更新,女生不會喜歡使用大鬍子作為自己的頭像。
如果您對監聽器不熟悉,可以閱讀Android控制元件系列之Button以及Android監聽器。
後臺程式碼如下:
1、單個RadioButton在選中後,通過點選無法變為未選中
單個CheckBox在選中後,通過點選可以變為未選中
2、一組RadioButton,只能同時選中一個
一組CheckBox,能同時選中多個
3、RadioButton在大部分UI框架中預設都以圓形表示
CheckBox在大部分UI框架中預設都以矩形表示
RadioButton和RadioGroup的關係:
1、RadioButton表示單個圓形單選框,而RadioGroup是可以容納多個RadioButton的容器
2、每個RadioGroup中的RadioButton同時只能有一個被選中
3、不同的RadioGroup中的RadioButton互不相干,即如果組A中有一個選中了,組B中依然可以有一個被選中
4、大部分場合下,一個RadioGroup中至少有2個RadioButton
5、大部分場合下,一個RadioGroup中的RadioButton預設會有一個被選中,並建議您將它放在RadioGroup中的起始位置
XML佈局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="請選擇您的性別:" android:textSize="9pt" /> <RadioGroup android:id="@+id/radioGroup" android:contentDescription="性別" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radioMale" android:text="男" android:checked="true"></RadioButton> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radioFemale" android:text="女"></RadioButton> </RadioGroup> <TextView android:id="@+id/tvSex" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="您的性別是:男" android:textSize="9pt" /> </LinearLayout>
選中項變更的事件監聽:
當RadioGroup中的選中項變更後,您可能需要做一些相應,比如上述例子中,性別選擇“女”後下面的本文也相應改變,又或者選擇不同的性別後,出現符合該性別的頭像列表進行更新,女生不會喜歡使用大鬍子作為自己的頭像。
如果您對監聽器不熟悉,可以閱讀Android控制元件系列之Button以及Android監聽器。
後臺程式碼如下:
TextView tv = null;//根據不同選項所要變更的文字控制元件 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //根據ID找到該文字控制元件 tv = (TextView)this.findViewById(R.id.tvSex); //根據ID找到RadioGroup例項 RadioGroup group = (RadioGroup)this.findViewById(R.id.radioGroup); //繫結一個匿名監聽器 group.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup arg0, int arg1) { // TODO Auto-generated method stub //獲取變更後的選中項的ID int radioButtonId = arg0.getCheckedRadioButtonId(); //根據ID獲取RadioButton的例項 RadioButton rb = (RadioButton)MyActiviy.this.findViewById(radioButtonId); //更新文字內容,以符合選中項 tv.setText("您的性別是:" + rb.getText()); } }); }
效果如下: