本期先來學習Button的兩個子控件,不管是單選仍是複選,在實際開發中都是使用的較多的控件,相信經過本期的學習便可輕鬆掌握。html
CheckBox(複選框)是Android中的複選框,主要有兩種狀態:選中和未選中。經過isChecked方法來判斷是否被選中,當用戶單擊時能夠在這兩種狀態間進行切換,會觸發一個OnCheckedChange事件。java
接下來經過一個簡單的示例程序來學習CheckBox的使用用法。android
一樣使用WidgetSample工程,在app/main/res/layout/目錄下建立一個checkbox_layout.xml文件,而後在其中填充以下代碼片斷:c#
<?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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="選擇喜歡的城市"/> <CheckBox android:id="@+id/shanghai_cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上海" android:checked="true"/> <CheckBox android:id="@+id/beijing_cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="北京"/> <CheckBox android: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; // 重慶複選框 @Override protected 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() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { // 提示用戶選擇的城市 showSelectCity(compoundButton); } });// 爲北京複選框綁定OnCheckedChangeListener監聽器 mBeijingCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { // 提示用戶選擇的城市 showSelectCity(compoundButton); } });// 爲重慶複選框綁定OnCheckedChangeListener監聽器 mChongqingCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public 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(); } } }
運行程序,當選擇重慶複選框時或者反選上海複選框時,能夠看到下圖所示界面效果。架構
思考:app
從上面的Java代碼能夠看到,有很大一部分代碼都是冗餘的,你們能夠思考一下是否能夠有其餘辦法來處理這個問題呢?ide
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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="請選擇性別"/> <RadioGroup android:id="@+id/sex_rg" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/male_rb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:checked="true"/> <RadioButton android: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; // 性別單選按鈕組 @Override protected 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() { @Override public 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)——鑫鱻全部,若轉載請備註出處,特此聲明!