Android編程單選項(xiàng)框RadioGroup綜合應(yīng)用示例
本文實(shí)例講述了Android編程單選項(xiàng)框RadioGroup用法。分享給大家供大家參考,具體如下:
今天介紹的是RadioGroup 的組事件.RadioGroup 可將各自不同的RadioButton ,設(shè)限于同一個(gè)Radio 按鈕組,同一個(gè)RadioGroup 組里的按鈕,只能做出單一選擇(單選題).
首先,我們先設(shè)計(jì)一個(gè)TextView Widget ,以及一個(gè)RadioGroup ,并將該RadioGroup 內(nèi)放置兩個(gè)RadioButton ,默認(rèn)為都不選擇,在程序運(yùn)行階段,利用onCheckedChanged 作為啟動(dòng)事件裝置,讓User選擇其中一個(gè)按鈕,顯示被選擇的內(nèi)容,最的將RadioButton 的選項(xiàng)文字顯示于TextView 當(dāng)中.
下面我們看一下效果圖:
下面是涉及的相關(guān)代碼:
string.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, RadioGroupDemo</string> <string name="app_name">RadioGroupDemo</string> <string name="tr_radio_op1">帥哥</string> <string name="tr_radio_op2">美女</string> <string name="str_radio_question1">請(qǐng)問你是?</string> </resources>
主布局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" > <!--第一個(gè)TextView --> <TextView android:id="@+id/myTextView" android:layout_width="228px" android:layout_height="49px" android:text="@string/str_radio_question1" android:textSize="30sp" /> <!--建立一個(gè)RadioGroup --> <RadioGroup android:id="@+id/myRadioGroup" android:layout_width="137px" android:layout_height="216px" android:orientation="vertical" > <!--第一個(gè)RadioButton --> <RadioButton android:id="@+id/myRadioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tr_radio_op1" /> <!--第二個(gè)RadioButton --> <RadioButton android:id="@+id/myRadioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tr_radio_op2" /> </RadioGroup> </LinearLayout>
最后是主控制程序RadioGroupDemo.Java:
package com.android.test; import android.app.Activity; import android.os.Bundle; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; public class RadioGroupDemo extends Activity { public TextView mTextView1; public RadioGroup mRadioGroup1; public RadioButton mRadio1,mRadio2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /*取得 TextView、RadioGroup、RadioButton對(duì)象*/ mTextView1 = (TextView) findViewById(R.id.myTextView); mRadioGroup1 = (RadioGroup) findViewById(R.id.myRadioGroup); mRadio1 = (RadioButton) findViewById(R.id.myRadioButton1); mRadio2 = (RadioButton) findViewById(R.id.myRadioButton2); /*RadioGroup用OnCheckedChangeListener來運(yùn)行*/ mRadioGroup1.setOnCheckedChangeListener(mChangeRadio); } private RadioGroup.OnCheckedChangeListener mChangeRadio = new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if(checkedId==mRadio1.getId()) { /*把mRadio1的內(nèi)容傳到mTextView1*/ mTextView1.setText(mRadio1.getText()); } else if(checkedId==mRadio2.getId()) { /*把mRadio2的內(nèi)容傳到mTextView1*/ mTextView1.setText(mRadio2.getText()); } } }; }
運(yùn)行RadioGroupDemo.java ,將得到以上效果。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android控件系列之RadioButton與RadioGroup使用方法
- android RadioGroup的使用方法
- android自定義RadioGroup可以添加多種布局的實(shí)現(xiàn)方法
- Android程序開發(fā)中單選按鈕(RadioGroup)的使用詳解
- Android RadioGroup和RadioButton控件簡(jiǎn)單用法示例
- Android RadioGroup 設(shè)置某一個(gè)選中或者不可選中的方法
- Android編程開發(fā)之RadioGroup用法實(shí)例
- 讓Android中RadioGroup不顯示在輸入法上面的辦法
- Android ViewPager與radiogroup實(shí)現(xiàn)關(guān)聯(lián)示例
- Android開發(fā)之RadioGroup的簡(jiǎn)單使用與監(jiān)聽示例
相關(guān)文章
Android應(yīng)用內(nèi)懸浮窗Activity的簡(jiǎn)單實(shí)現(xiàn)
懸浮窗相信大家應(yīng)該都不陌生,下面這篇文章主要給大家介紹了關(guān)于Android應(yīng)用內(nèi)懸浮窗Activity簡(jiǎn)單實(shí)現(xiàn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01淺談Android開發(fā)Webview的Loading使用效果
這篇文章主要為大家介紹了淺談Android開發(fā)Webview的Loading使用效果詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Android自定義view實(shí)現(xiàn)動(dòng)態(tài)柱狀圖
這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)動(dòng)態(tài)柱狀圖的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08android實(shí)現(xiàn)播放網(wǎng)絡(luò)視頻
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)播放網(wǎng)絡(luò)視頻,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04Android ProgressDialog進(jìn)度條使用詳解
這篇文章主要對(duì)Android開發(fā)之ProgressDialog讀取文件進(jìn)度進(jìn)行解析,感興趣的朋友可以參考一下2016-02-02Android項(xiàng)目中g(shù)radle的執(zhí)行流程
大家好,本篇文章主要講的是Android項(xiàng)目中g(shù)radle的執(zhí)行流程,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01Android實(shí)現(xiàn)不同apk間共享數(shù)據(jù)的方法(2種方法)
這篇文章主要介紹了Android實(shí)現(xiàn)不同apk間共享數(shù)據(jù)的方法,介紹了apk自定義借口實(shí)現(xiàn)數(shù)據(jù)共享與基于User id的數(shù)據(jù)共享,并重點(diǎn)介紹了基于User id的數(shù)據(jù)共享實(shí)現(xiàn)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2016-01-01Android 2.3 撥號(hào)上網(wǎng)流程從源碼角度進(jìn)行分析
SIM卡實(shí)現(xiàn)撥號(hào)上網(wǎng)功能之前需要設(shè)置一番,這些設(shè)置步驟究竟做了哪些事情呢?我們現(xiàn)在就從源碼的角度進(jìn)行分析2013-01-01