Android RadioGroup和RadioButton控件簡(jiǎn)單用法示例
本文實(shí)例講述了Android RadioGroup和RadioButton控件簡(jiǎn)單用法。分享給大家供大家參考,具體如下:
RadioGroup和RadioButton代表的是Android中單選按鈕的一種控件,寫(xiě)個(gè)簡(jiǎn)單的代碼熟悉一下:
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);
//通過(guò)ID找到TextView
myTextView = (TextView) findViewById(R.id.myTextView);
//通過(guò)ID找到RadioButton
chinaBtn = (RadioButton) findViewById(R.id.china_Button);
ukBtn = (RadioButton) findViewById(R.id.uk_Button);
usaBtn = (RadioButton) findViewById(R.id.usa_Button);
//通過(guò)ID找到RadioGroup
rg = (RadioGroup) findViewById(R.id.rBtnGroup);
//只要對(duì)RadioGroup進(jìn)行監(jiān)聽(tīng)
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("您選擇的國(guó)家是:" + chinaBtn.getText().toString());
}
else if(R.id.uk_Button == checkedId){
myTextView.setText("您選擇的國(guó)家是:" + ukBtn.getText().toString());
}
else if(R.id.usa_Button == checkedId){
myTextView.setText("您選擇的國(guó)家是:" + 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="中國(guó)"
/>
<RadioButton
android:id="@+id/uk_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="英國(guó)"
/>
<RadioButton
android:id="@+id/usa_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="美國(guó)"
/>
</RadioGroup>
</LinearLayout>
效果如下:

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android控件用法總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開(kāi)發(fā)之SD卡操作方法匯總》、《Android資源操作技巧匯總》及《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android單選按鈕RadioButton的使用詳解
- Android控件RadioButton實(shí)現(xiàn)多選一功能
- Android開(kāi)發(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í)進(jìn)度條
這篇文章主要介紹了如何通過(guò)Android模擬實(shí)現(xiàn)華為在系統(tǒng)升級(jí)時(shí)顯示的進(jìn)度條。文中的實(shí)現(xiàn)過(guò)程講解詳細(xì),感興趣的小伙伴可以動(dòng)手試一試2022-01-01
Android?實(shí)現(xiàn)卡片堆疊錢(qián)包管理動(dòng)畫(huà)效果
這篇文章主要介紹了Android?實(shí)現(xiàn)卡片堆疊錢(qián)包管理動(dòng)畫(huà)效果,實(shí)現(xiàn)思路是在動(dòng)畫(huà)回調(diào)中requestLayout?實(shí)現(xiàn)動(dòng)畫(huà)效果,用Bounds?對(duì)象記錄每一個(gè)CardView?對(duì)象的初始位置,當(dāng)前位置,運(yùn)動(dòng)目標(biāo)位置,需要的朋友可以參考下2022-07-07
Android編程實(shí)現(xiàn)使用SoundPool播放音樂(lè)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)使用SoundPool播放音樂(lè)的方法,較為詳細(xì)的分析說(shuō)明了SoundPool對(duì)象的使用技巧,需要的朋友可以參考下2016-01-01
Android中檢查、監(jiān)聽(tīng)電量和充電狀態(tài)的方法
這篇文章主要介紹了Android中檢查、監(jiān)聽(tīng)電量和充電狀態(tài)的方法,如判斷當(dāng)前充電狀態(tài)、監(jiān)聽(tīng)充電狀態(tài)的改變、判斷當(dāng)前剩余電量等,需要的朋友可以參考下2014-06-06
android中soap協(xié)議使用(ksoap調(diào)用webservice)
kSOAP是如何調(diào)用ebservice的呢,首先要使用SoapObject,這是一個(gè)高度抽象化的類(lèi),完成SOAP調(diào)用。可以調(diào)用它的addProperty方法填寫(xiě)要調(diào)用的webservice方法的參數(shù)2014-02-02
Android編程實(shí)現(xiàn)的身份證、車(chē)牌號(hào)正則驗(yàn)證工具類(lèi)實(shí)例
這篇文章主要介紹了Android編程實(shí)現(xiàn)的身份證、車(chē)牌號(hào)正則驗(yàn)證工具類(lèi),結(jié)合實(shí)例形式分析了java針對(duì)身份證、車(chē)牌號(hào)數(shù)字字符串正則驗(yàn)證操作技巧,需要的朋友可以參考下2017-12-12
Android判斷App前臺(tái)運(yùn)行還是后臺(tái)運(yùn)行(運(yùn)行狀態(tài))
這篇文章主要介紹了Android判斷App前臺(tái)運(yùn)行還是后臺(tái)運(yùn)行的相關(guān)資料,需要的朋友可以參考下2016-04-04
Android實(shí)現(xiàn)卡片翻轉(zhuǎn)動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)卡片翻轉(zhuǎn)動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android接入支付寶實(shí)現(xiàn)支付功能實(shí)例
這篇文章主要介紹了Android接入支付寶實(shí)現(xiàn)支付功能實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

