Android實現(xiàn)單選按鈕
本文實例為大家分享了Android實現(xiàn)單選按鈕的具體代碼,供大家參考,具體內(nèi)容如下
單選按鈕
在默認(rèn)情況下,單選按鈕顯示為一個圓形圖標(biāo),可以在圖標(biāo)旁放一些說明文字。通常情況下RadioButton組件需要與RadioGroup組件一起使用,組成一個單選按鈕組。RadioGroup是可以容納多個RadioButton的容器。
<LinearLayout ? ? android:layout_width="match_parent" ? ? android:layout_height="60dp" ? ? android:orientation="horizontal"> ? ? <TextView ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="match_parent" ? ? ? ? android:text="選擇性別:" ? ? ? ? android:textSize="25sp" ? ? ? ? android:gravity="center" ? ? ? ? android:textColor="@color/black"/> ? ? <RadioGroup ? ? ? ? android:id="@+id/radioGroup" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" ? ? ? ? android:orientation="horizontal" ? ? ? ? android:gravity="center"> ? ? ? ? <RadioButton ? ? ? ? ? ? android:id="@+id/radio_man" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="男" ? ? ? ? ? ? android:textColor="@color/black" ? ? ? ? ? ? android:textSize="25sp" ? ? ? ? ? ? android:checked="true"/> ? ? ? ? <RadioButton ? ? ? ? ? ? android:id="@+id/radio_female" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="女" ? ? ? ? ? ? android:textColor="@color/black" ? ? ? ? ? ? android:layout_marginLeft="100dp" ? ? ? ? ? ? android:textSize="25sp"/> ? ? </RadioGroup> </LinearLayout> <Button ? ? android:id="@+id/bt_submit" ? ? android:layout_width="100dp" ? ? android:layout_height="50dp" ? ? android:text="提交" ? ? android:textSize="20sp" ? ? android:layout_marginTop="10dp" ? ? android:layout_gravity="center"/>
布局效果顯示:
RadioButton組件的android:checked屬性用來指定選中的狀態(tài),android:checked="true"時,表示選中;android:checked="false"時,表示取消選中。
獲得選中的值有三種方法:
第一種是為RadioButton設(shè)置一個事件監(jiān)聽器setOnCheckChangeListener。
public class MainActivity extends AppCompatActivity { ? ? RadioGroup radioGroup; ? ?? ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? //初始化控件 ? ? ? ? initView(); ? ? ? ? //點擊事件 ? ? ? ? clickEvent(); ? ? } ? ? private void initView() { ? ? ? ? radioGroup = findViewById(R.id.radioGroup); ? ? } ? ? private void clickEvent() { ? ? ? ? //給RadioGroup綁定監(jiān)視器 ? ? ? ? radioGroup.setOnCheckedChangeListener(new MyRadioButtonListener()); ? ? } ? ? //單選按鈕監(jiān)聽 ? ? private class MyRadioButtonListener implements RadioGroup.OnCheckedChangeListener { ? ? ? ? @Override ? ? ? ? public void onCheckedChanged(RadioGroup group, int checkedId) { ? ? ? ? ? ? RadioButton r = (RadioButton) findViewById(checkedId);//獲取被選中的Id ? ? ? ? ? ? Log.i("單選按鈕監(jiān)聽", "選擇性別為:" + r.getText().toString()); ? ? ? ? } ? ? } }
單選按鈕監(jiān)聽日志效果:
第二種通過單擊其他按鈕獲取選中單選按鈕的值。
public class MainActivity extends AppCompatActivity implements View.OnClickListener { ? ? RadioGroup radioGroup; ? ? //提交 ? ? Button bt_submit; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? //初始化控件 ? ? ? ? initView(); ? ? ? ? //點擊事件 ? ? ? ? clickEvent(); ? ? } ? ? private void initView() { ? ? ? ? radioGroup = findViewById(R.id.radioGroup); ? ? ? ? bt_submit = findViewById(R.id.bt_submit); ? ? } ? ? private void clickEvent() { ? ? ? ? //提交 ? ? ? ? bt_submit.setOnClickListener(this); ? ? } ? ? @Override ? ? public void onClick(View v) { ? ? ? ? switch (v.getId()) { ? ? ? ? ? ? case R.id.bt_submit: ? ? ? ? ? ? ? ? for (int i = 0; i < radioGroup.getChildCount(); i++) { ? ? ? ? ? ? ? ? ? ? RadioButton r = (RadioButton) radioGroup.getChildAt(i); ? ? ? ? ? ? ? ? ? ? if (r.isChecked()) { ? ? ? ? ? ? ? ? ? ? ? ? Log.i("單擊其他按鈕時獲取", "選擇性別為:" + r.getText()); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? } }
判斷單擊其他按鈕獲取選中單選按鈕的值的日志效果展示:
第三種判斷被點擊的id是哪一個單選按鈕的id,通過id去獲取值。
public class MainActivity extends AppCompatActivity { ? ? RadioGroup radioGroup; ? ? //男 ? ? RadioButton radio_man; ? ? //女 ? ? RadioButton radio_female; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? //初始化控件 ? ? ? ? initView(); ? ? ? ? //點擊事件 ? ? ? ? clickEvent(); ? ? } ? ? private void initView() { ? ? ? ? radioGroup = findViewById(R.id.radioGroup); ? ? ? ? radio_man = findViewById(R.id.radio_man); ? ? ? ? radio_female = findViewById(R.id.radio_female); ? ? } ? ? private void clickEvent() { ? ? ? ? //給RadioGroup綁定監(jiān)視器 ? ? ? ? radioGroup.setOnCheckedChangeListener(new MyRadioButtonListener()); ? ? } ? ? //單選按鈕監(jiān)聽 ? ? private class MyRadioButtonListener implements RadioGroup.OnCheckedChangeListener { ? ? ? ? @Override ? ? ? ? public void onCheckedChanged(RadioGroup group, int checkedId) { ? ? ? ? ? ? // 選中狀態(tài)改變時被觸發(fā) ? ? ? ? ? ? switch (checkedId) { ? ? ? ? ? ? ? ? case R.id.radio_female: ? ? ? ? ? ? ? ? ? ? // 當(dāng)用戶選擇女性時 ? ? ? ? ? ? ? ? ? ? Log.i("判斷點擊Id的單選按鈕", "選擇性別為:" + radio_female.getText().toString()); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case R.id.radio_man: ? ? ? ? ? ? ? ? ? ? // 當(dāng)用戶選擇男性時 ? ? ? ? ? ? ? ? ? ? Log.i("判斷點擊Id的單選按鈕", "選擇性別為:"+radio_man.getText().toString()); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? } ? ? } }
判斷點擊的單選按鈕日志效果展示:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解android項目由Gradle 2.2 切換到 3.0的坑
本篇文章主要介紹了詳解android項目由Gradle 2.2 切換到 3.0的坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02Android編程實現(xiàn)向SD卡寫入數(shù)據(jù)的方法
這篇文章主要介紹了Android編程實現(xiàn)向SD卡寫入數(shù)據(jù)的方法,涉及Android針對SD卡狀態(tài)判斷,文件及權(quán)限操作等相關(guān)技巧,需要的朋友可以參考下2016-04-04Android編程之創(chuàng)建自己的內(nèi)容提供器實現(xiàn)方法
這篇文章主要介紹了Android編程之創(chuàng)建自己的內(nèi)容提供器實現(xiàn)方法,結(jié)合具體實例形式分析了Android創(chuàng)建內(nèi)容提供器的原理、步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-08-08Android?動態(tài)加載?so實現(xiàn)示例詳解
這篇文章主要為大家介紹了Android?動態(tài)加載?so實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09支付寶咻一咻怎么用 Android幫你實現(xiàn)咻一咻
Android幫你實現(xiàn)咻一咻,這篇文章主要為大家介紹了支付寶咻一咻的幾種思路,感興趣的朋友可以參考一下2016-02-02