Android零基础入门第20节:CheckBox和RadioButton使用大全
?本期先來學習Button的兩個子控件,無論是單選還是復選,在實際開發中都是使用的較多的控件,相信通過本期的學習即可輕松掌握。
?
一、CheckBox
?
CheckBox(復選框)是Android中的復選框,主要有兩種狀態:選中和未選中。通過isChecked方法來判斷是否被選中,當用戶單擊時可以在這兩種狀態間進行切換,會觸發一個OnCheckedChange事件。
接下來通過一個簡單的示例程序來學習CheckBox的使用用法。
?同樣使用WidgetSample工程,在app/main/res/layout/目錄下創建一個checkbox_layout.xml文件,然后在其中填充如下代碼片段:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="選擇喜歡的城市"/><CheckBoxandroid:id="@+id/shanghai_cb"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="上海"android:checked="true"/><CheckBoxandroid:id="@+id/beijing_cb"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="北京"/><CheckBoxandroid:id="@+id/chongqing_cb"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="重慶"/> </LinearLayout>然后修改一下app/src/java/MainActivity.java文件中加載的布局文件為新建的checkbox_layout.xml文件。為了監聽三個復選框的操作事件,在Java代碼中分別為其添加事件監聽器,具體代碼如下:
package com.jinyu.cqkxzsxy.android.widgetsample;import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.Toast;public class MainActivity extends AppCompatActivity {private CheckBox mShanghaiCb = null; // 上海復選框private CheckBox mBeijingCb = null; // 北京復選框private CheckBox mChongqingCb = null; // 重慶復選框 @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.checkbox_layout);// 獲取界面組件mShanghaiCb = (CheckBox) findViewById(R.id.shanghai_cb);mBeijingCb = (CheckBox) findViewById(R.id.beijing_cb);mChongqingCb = (CheckBox) findViewById(R.id.chongqing_cb);// 為上海復選框綁定OnCheckedChangeListener監聽器mShanghaiCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {// 提示用戶選擇的城市 showSelectCity(compoundButton);}});// 為北京復選框綁定OnCheckedChangeListener監聽器mBeijingCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {// 提示用戶選擇的城市 showSelectCity(compoundButton);}});// 為重慶復選框綁定OnCheckedChangeListener監聽器mChongqingCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {// 提示用戶選擇的城市 showSelectCity(compoundButton);}});}/*** 提示用戶選擇的城市* @param compoundButton*/private void showSelectCity(CompoundButton compoundButton){// 獲取復選框的文字提示String city = compoundButton.getText().toString();// 根據復選框的選中狀態進行相應提示if(compoundButton.isChecked()) {Toast.makeText(MainActivity.this, "選中" + city, Toast.LENGTH_SHORT).show();} else {Toast.makeText(MainActivity.this, "取消選中" + city, Toast.LENGTH_SHORT).show();}} }運行程序,當選擇重慶復選框時或者反選上海復選框時,可以看到下圖所示界面效果。
思考:
從上面的Java代碼可以看到,有很大一部分代碼都是冗余的,大家可以思考一下是否可以有其他辦法來處理這個問題呢?
?
?
二、RadioButton
?
RadioButton(單選按鈕)在Android開發中應用的非常廣泛,比如一些選擇項的時候,會用到單選按鈕。它是一種單個圓形單選框雙狀態的按鈕,可以選擇或不選擇。在RadioButton沒有被選中時,用戶能夠按下或點擊來選中它。但是,與復選框相反,用戶一旦選中就不能夠取消選中。當用戶選中的時候會觸發一個OnCheckedChange事件。
實現RadioButton由兩部分組成,也就是RadioButton和RadioGroup配合使用。RadioGroup是單選組合框,可以容納多個RadioButton的容器。在沒有RadioGroup的情況下,RadioButton可以全部都選中;當多個RadioButton被RadioGroup包含的情況下,RadioButton只可以選擇一個。
?接下來通過一個簡單的示例程序來學習RadioButton的使用用法。
?同樣使用WidgetSample工程,在app/main/res/layout/目錄下創建一個radiobutton_layout.xml文件,然后在其中填充如下代碼片段:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="請選擇性別"/><RadioGroupandroid:id="@+id/sex_rg"android:layout_width="wrap_content"android:layout_height="wrap_content" ><RadioButtonandroid:id="@+id/male_rb"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男"android:checked="true"/><RadioButtonandroid:id="@+id/female_rb"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女"/></RadioGroup> </LinearLayout>然后修改一下app/src/java/MainActivity.java文件中加載的布局文件為新建的radiobutton_layout.xml文件。為了監聽單選按鈕組的選中事件,在Java代碼中為其添加選擇事件監聽器,具體代碼如下:
package com.jinyu.cqkxzsxy.android.widgetsample;import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast;public class MainActivity extends AppCompatActivity {private RadioButton mMaleRb = null; // 性別男單選按鈕private RadioButton mFemaleRb = null; // 性別女單選按鈕private RadioGroup mSexRg = null; // 性別單選按鈕組 @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.radiobutton_layout);// 獲取界面組件mMaleRb = (RadioButton) findViewById(R.id.male_rb);mFemaleRb = (RadioButton) findViewById(R.id.female_rb);mSexRg = (RadioGroup) findViewById(R.id.sex_rg);// 為單選按鈕組綁定OnCheckedChangeListener監聽器mSexRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup radioGroup, int checkedId) {// 獲取用戶選中的性別String sex = "";switch (checkedId) {case R.id.male_rb:sex = mMaleRb.getText().toString();break;case R.id.female_rb:sex = mFemaleRb.getText().toString();break;default:break;}// 消息提示Toast.makeText(MainActivity.this,"選擇的性別是:" + sex, Toast.LENGTH_SHORT).show();}});} }運行程序,默認選中性別男,當點擊性別女的時候可以看到下圖所示界面效果。
到此,最常用的兩個Button子組件CheckBox和RadioButton已經學習完成,你都掌握了嗎?
-------------------------------------------------
今天就先到這里,下一期開始UI組件的學習。如果有問題歡迎留言一起探討,也歡迎加入Android零基礎入門技術討論微信群,共同成長!
往期總結分享:
?
Android零基礎入門第1節:Android的前世今生
Android零基礎入門第2節:Android 系統架構和應用組件那些事
Android零基礎入門第3節:帶你一起來聊一聊Android開發環境
Android零基礎入門第4節:正確安裝和配置JDK, 高富帥養成第一招
Android零基礎入門第5節:善用ADT Bundle, 輕松邂逅女神
Android零基礎入門第6節:配置優化SDK Manager, 正式約會女神
Android零基礎入門第7節:搞定Android模擬器,開啟甜蜜之旅
Android零基礎入門第8節:HelloWorld,我的第一趟旅程出發點
Android零基礎入門第9節:Android應用實戰,不懂代碼也可以開發
Android零基礎入門第10節:開發IDE大升級,終于迎來了Android Studio
Android零基礎入門第11節:簡單幾步帶你飛,運行Android Studio工程
Android零基礎入門第12節:熟悉Android Studio界面,開始裝逼賣萌
Android零基礎入門第13節:Android Studio配置優化,打造開發利器
Android零基礎入門第14節:使用高速Genymotion,跨入火箭時代
Android零基礎入門第15節:掌握Android Studio項目結構,揚帆起航
Android零基礎入門第16節:Android用戶界面開發概述
Android零基礎入門第17節:TextView屬性和方法大全
Android零基礎入門第18節:EditText的屬性和使用方法
Android零基礎入門第19節:Button使用詳解
此文章版權為微信公眾號分享達人秀(ShareExpert)——鑫鱻所有,若轉載請備注出處,特此聲明!
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Android零基础入门第20节:CheckBox和RadioButton使用大全的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: es5安装常见错误
- 下一篇: Spring bean - scope详