Android RadioGroup和RadioButton控件簡單用法示例
本文實(shí)例講述了Android RadioGroup和RadioButton控件簡單用法。分享給大家供大家參考,具體如下:
RadioGroup和RadioButton代表的是Android中單選按鈕的一種控件,寫個簡單的代碼熟悉一下:
import android.app.Activity; import android.os.Bundle; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.TextView; public class Radio extends Activity { private TextView myTextView; private RadioButton chinaBtn; private RadioButton ukBtn; private RadioButton usaBtn; private RadioGroup rg; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //通過ID找到TextView myTextView = (TextView) findViewById(R.id.myTextView); //通過ID找到RadioButton chinaBtn = (RadioButton) findViewById(R.id.china_Button); ukBtn = (RadioButton) findViewById(R.id.uk_Button); usaBtn = (RadioButton) findViewById(R.id.usa_Button); //通過ID找到RadioGroup rg = (RadioGroup) findViewById(R.id.rBtnGroup); //只要對RadioGroup進(jìn)行監(jiān)聽 rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if(R.id.china_Button == checkedId){ myTextView.setText("您選擇的國家是:" + chinaBtn.getText().toString()); } else if(R.id.uk_Button == checkedId){ myTextView.setText("您選擇的國家是:" + ukBtn.getText().toString()); } else if(R.id.usa_Button == checkedId){ myTextView.setText("您選擇的國家是:" + usaBtn.getText().toString()); } } }); } }
main.xml文件如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/myTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello" /> <RadioGroup android:id="@+id/rBtnGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/china_Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="中國" /> <RadioButton android:id="@+id/uk_Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="英國" /> <RadioButton android:id="@+id/usa_Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="美國" /> </RadioGroup> </LinearLayout>
效果如下:
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android資源操作技巧匯總》及《Android開發(fā)入門與進(jìn)階教程》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
- Android單選按鈕RadioButton的使用詳解
- Android控件RadioButton實(shí)現(xiàn)多選一功能
- Android開發(fā)設(shè)置RadioButton點(diǎn)擊效果的方法
- Android編程實(shí)現(xiàn)自定義PopupMenu樣式示例【顯示圖標(biāo)與設(shè)置RadioButton圖標(biāo)】
- Android RadioButton 圖片位置與大小實(shí)例詳解
- Android中設(shè)置RadioButton在文字右邊的方法實(shí)例
- android RadioButton和CheckBox組件的使用方法
- Android RadioButton單選框的使用方法
- Android定制RadioButton樣式三種實(shí)現(xiàn)方法
- Android控件系列之RadioButton與RadioGroup使用方法
- Android控件RadioButton的使用方法
相關(guān)文章
Android模擬實(shí)現(xiàn)華為系統(tǒng)升級進(jìn)度條
這篇文章主要介紹了如何通過Android模擬實(shí)現(xiàn)華為在系統(tǒng)升級時(shí)顯示的進(jìn)度條。文中的實(shí)現(xiàn)過程講解詳細(xì),感興趣的小伙伴可以動手試一試2022-01-01Android?實(shí)現(xiàn)卡片堆疊錢包管理動畫效果
這篇文章主要介紹了Android?實(shí)現(xiàn)卡片堆疊錢包管理動畫效果,實(shí)現(xiàn)思路是在動畫回調(diào)中requestLayout?實(shí)現(xiàn)動畫效果,用Bounds?對象記錄每一個CardView?對象的初始位置,當(dāng)前位置,運(yùn)動目標(biāo)位置,需要的朋友可以參考下2022-07-07Android編程實(shí)現(xiàn)使用SoundPool播放音樂的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)使用SoundPool播放音樂的方法,較為詳細(xì)的分析說明了SoundPool對象的使用技巧,需要的朋友可以參考下2016-01-01Android中檢查、監(jiān)聽電量和充電狀態(tài)的方法
這篇文章主要介紹了Android中檢查、監(jiān)聽電量和充電狀態(tài)的方法,如判斷當(dāng)前充電狀態(tài)、監(jiān)聽充電狀態(tài)的改變、判斷當(dāng)前剩余電量等,需要的朋友可以參考下2014-06-06android中soap協(xié)議使用(ksoap調(diào)用webservice)
kSOAP是如何調(diào)用ebservice的呢,首先要使用SoapObject,這是一個高度抽象化的類,完成SOAP調(diào)用。可以調(diào)用它的addProperty方法填寫要調(diào)用的webservice方法的參數(shù)2014-02-02Android編程實(shí)現(xiàn)的身份證、車牌號正則驗(yàn)證工具類實(shí)例
這篇文章主要介紹了Android編程實(shí)現(xiàn)的身份證、車牌號正則驗(yàn)證工具類,結(jié)合實(shí)例形式分析了java針對身份證、車牌號數(shù)字字符串正則驗(yàn)證操作技巧,需要的朋友可以參考下2017-12-12Android判斷App前臺運(yùn)行還是后臺運(yùn)行(運(yùn)行狀態(tài))
這篇文章主要介紹了Android判斷App前臺運(yùn)行還是后臺運(yùn)行的相關(guān)資料,需要的朋友可以參考下2016-04-04Android實(shí)現(xiàn)卡片翻轉(zhuǎn)動畫
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)卡片翻轉(zhuǎn)動畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android接入支付寶實(shí)現(xiàn)支付功能實(shí)例
這篇文章主要介紹了Android接入支付寶實(shí)現(xiàn)支付功能實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06