Android RadioGroup和RadioButton控件簡單用法示例
本文實(shí)例講述了Android RadioGroup和RadioButton控件簡單用法。分享給大家供大家參考,具體如下:
RadioGroup和RadioButton代表的是Android中單選按鈕的一種控件,寫個(gè)簡單的代碼熟悉一下:
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)文章
Kotlin圖文并茂講解續(xù)體與續(xù)體攔截器和調(diào)度器
這篇文章主要介紹了Kotlin開發(fā)中續(xù)體與續(xù)體攔截器和調(diào)度器的相關(guān)使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Android 中Popwindow彈出菜單的兩種方法實(shí)例
這篇文章主要介紹了Android 中Popwindow彈出菜單的兩種方法實(shí)例的相關(guān)資料,這里提供了兩種實(shí)現(xiàn)的方法,并附有實(shí)例代碼,需要的朋友可以參考下2017-03-03
Android入門之使用eclipse進(jìn)行源碼開發(fā)的方法
這篇文章主要介紹了Android入門之使用eclipse進(jìn)行源碼開發(fā)的方法,較為詳細(xì)的分析了使用eclipse進(jìn)行Android源碼開發(fā)的具體步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-02-02
Android Studio添加第三方庫的注意事項(xiàng)
這篇文章給大家介紹的是Android Studio添加第三方庫遇到的一些坑,以及對應(yīng)的解決辦法,有需要的可以參考借鑒。2016-09-09
Android中仿IOS提示框的實(shí)現(xiàn)方法
下面小編就為大家分享一篇Android中仿IOS提示框的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01

