標籤:
和按鈕類似,這裡採用cb1.setOnCheckedChangeListener(this);方法分別對3個CheckBox進行CheckChange事件綁定,然後在onCheckedChanged抽象函數中對點擊CheckBox的狀態進行擷取並用Toast顯示。
1 //使用狀態改變檢查監聽器 2 public class MainActivity extends Activity implements OnCheckedChangeListener { 3 private CheckBox cb1, cb2, cb3;//建立3個CheckBox對象 4 5 @Override 6 public void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.main); 9 //執行個體化3個CheckBox10 cb1 = (CheckBox) findViewById(R.id.cb1);11 cb2 = (CheckBox) findViewById(R.id.cb2);12 cb3 = (CheckBox) findViewById(R.id.cb3);13 cb1.setOnCheckedChangeListener(this);14 cb2.setOnCheckedChangeListener(this);15 cb3.setOnCheckedChangeListener(this);16 }17 18 //重寫監聽器的抽象函數19 @Override20 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {21 //buttonView 選中狀態發生改變的那個按鈕22 //isChecked 表示按鈕新的狀態(true/false)23 if (cb1 == buttonView || cb2 == buttonView || cb3 == buttonView) {24 if (isChecked) {25 //顯示一個提示資訊26 toastDisplay(buttonView.getText() + "選中");27 28 } else {29 toastDisplay(buttonView.getText() + "取消選中");30 }31 }32 }33 34 public void toastDisplay(String str) {35 Toast.makeText(this, str, Toast.LENGTH_SHORT).show();36 }37 }
瞭解上述用法之後,我們來看一下radioButton的用法:注意這裡不同的地方是第13、14行,沒有像CheckBox一樣每一個控制項綁定CheckChange監聽器,而是將RadioGroup進行綁定。
1 //使用狀態改變監聽器 2 public class MainActivity extends Activity implements OnCheckedChangeListener { 3 private RadioButton rb1, rb2, rb3; 4 private RadioGroup rg; 5 6 @Override 7 public void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.main);10 rb1 = (RadioButton) findViewById(R.id.rb1);11 rb2 = (RadioButton) findViewById(R.id.rb2);12 rb3 = (RadioButton) findViewById(R.id.rb3);13 rg = (RadioGroup) findViewById(R.id.radGrp);14 rg.setOnCheckedChangeListener(this);//將單選組綁定監聽器15 }16 17 //重寫監聽器函數18 /** 19 * @param group:指單選組20 * @param group:指單選組中發生狀態改變的RadioButton的記憶體ID!21 */22 @Override23 public void onCheckedChanged(RadioGroup group, int checkedId) { 24 if (group == rg) {//因為當前程式中只有一個RadioGroup,此步可以不進行判定25 String rbName = null; 26 if (checkedId == rb1.getId()) {27 rbName = rb1.getText().toString();28 } else if (checkedId == rb2.getId()) {29 rbName = rb2.getText().toString();30 } else if (checkedId == rb3.getId()) {31 rbName = rb3.getText().toString();32 }33 Toast.makeText(this, "選擇了下標為“" + rbName + "”的選項按鈕", 34 Toast.LENGTH_LONG).show();35 }36 }37 }
那這個RadioGroup是怎麼和RadioButton結合在一起呈現多選一的效果的呢?我們來看看xml就知道了:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <RadioGroup 8 android:id="@+id/radGrp" 9 android:layout_width="wrap_content"10 android:layout_height="wrap_content">11 <RadioButton 12 android:layout_width="fill_parent" 13 android:layout_height="wrap_content" 14 android:text="RadioButton1"15 android:id="@+id/rb1"16 />17 <RadioButton 18 android:layout_width="fill_parent" 19 android:layout_height="wrap_content" 20 android:text="RadioButton2"21 android:id="@+id/rb2"22 />23 <RadioButton 24 android:layout_width="fill_parent" 25 android:layout_height="wrap_content" 26 android:text="RadioButton3"27 android:id="@+id/rb3"28 />29 </RadioGroup>30 </LinearLayout>
本文連結:http://www.cnblogs.com/zjutlitao/p/4229767.html
更多精彩:http://www.cnblogs.com/zjutlitao/
[安卓] 4、CheckBox、RadioButton和Toast簡單用法