[安卓] 4、CheckBox、RadioButton和Toast簡單用法

來源:互聯網
上載者:User

標籤:

 

 

 

 

和按鈕類似,這裡採用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簡單用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.