日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

單選按鈕 RadioButton 的應(yīng)用

 小小770 2013-12-16

原文 http://hualang./blog/964507

單選按鈕RadioButton在Android平臺(tái)上也應(yīng)用的非常多,比如一些選擇項(xiàng)的時(shí)候,會(huì)用到單選按鈕,實(shí)現(xiàn)單選按鈕由兩部分組成,也就是RadioButton和RadioGroup配合使用

RadioButton的單選按鈕;

RadioGroup是單選組合框,用于將RadioButton框起來;

在沒有RadioGroup的情況下,RadioButton可以全部都選中;

當(dāng)多個(gè)RadioButton被RadioGroup包含的情況下,RadioButton只可以選擇一個(gè);

 

注意:?jiǎn)芜x按鈕的事件監(jiān)聽用setOnCheckedChangeListener來對(duì)單選按鈕進(jìn)行監(jiān)聽

例子:

一道選擇題,選擇哪個(gè)城市美女最多,當(dāng)然,這個(gè)就是為了測(cè)試

RadioTest.java

 

Java代碼  收藏代碼
  1. package org.loulijun.radio;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Gravity;  
  6. import android.widget.RadioButton;  
  7. import android.widget.RadioGroup;  
  8. import android.widget.TextView;  
  9. import android.widget.Toast;  
  10.   
  11. public class RadioTest extends Activity {  
  12.     /** Called when the activity is first created. */  
  13.     TextView textview;  
  14.     RadioGroup radiogroup;  
  15.     RadioButton radio1,radio2,radio3,radio4;  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         textview=(TextView)findViewById(R.id.textview1);  
  21.         radiogroup=(RadioGroup)findViewById(R.id.radiogroup1);  
  22.         radio1=(RadioButton)findViewById(R.id.radiobutton1);  
  23.         radio2=(RadioButton)findViewById(R.id.radiobutton2);  
  24.         radio3=(RadioButton)findViewById(R.id.radiobutton3);  
  25.         radio4=(RadioButton)findViewById(R.id.radiobutton4);  
  26.           
  27.         radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
  28.               
  29.             @Override  
  30.             public void onCheckedChanged(RadioGroup group, int checkedId) {  
  31.                 // TODO Auto-generated method stub  
  32.                 if(checkedId==radio2.getId())  
  33.                 {  
  34.                     DisplayToast("正確答案:"+radio2.getText()+",恭喜你,回答正確!");  
  35.                 }else  
  36.                 {  
  37.                     DisplayToast("請(qǐng)注意,回答錯(cuò)誤!");  
  38.                 }  
  39.             }  
  40.         });  
  41.     }  
  42.     public void DisplayToast(String str)  
  43.     {  
  44.         Toast toast=Toast.makeText(this, str, Toast.LENGTH_LONG);  
  45.         toast.setGravity(Gravity.TOP,0,220);  
  46.         toast.show();  
  47.     }  
  48. }  

 

 strings.xml文件

 

Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">哪個(gè)城市美女多?</string>  
  4.     <string name="app_name">單選按鈕測(cè)試</string>  
  5.     <string name="radiobutton1">杭州</string>  
  6.     <string name="radiobutton2">成都</string>  
  7.     <string name="radiobutton3">重慶</string>  
  8.     <string name="radiobutton4">蘇州</string>  
  9. </resources>  

 main.xml文件:注意,這里面,4個(gè)RadioButton包含在RadioGroup中

 

Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas./apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     android:id="@+id/textview1"  
  12.     />  
  13.     <RadioGroup  
  14.         android:id="@+id/radiogroup1"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:orientation="vertical"  
  18.         android:layout_x="3px"  
  19.     >  
  20.         <RadioButton  
  21.             android:id="@+id/radiobutton1"  
  22.             android:layout_width="wrap_content"  
  23.             android:layout_height="wrap_content"  
  24.             android:text="@string/radiobutton1"  
  25.         />  
  26.         <RadioButton  
  27.             android:id="@+id/radiobutton2"  
  28.             android:layout_width="wrap_content"  
  29.             android:layout_height="wrap_content"  
  30.             android:text="@string/radiobutton2"  
  31.         />  
  32.         <RadioButton  
  33.             android:id="@+id/radiobutton3"  
  34.             android:layout_width="wrap_content"  
  35.             android:layout_height="wrap_content"  
  36.             android:text="@string/radiobutton3"  
  37.         />  
  38.         <RadioButton  
  39.             android:id="@+id/radiobutton4"  
  40.             android:layout_width="wrap_content"  
  41.             android:layout_height="wrap_content"  
  42.             android:text="@string/radiobutton4"  
  43.         />  
  44.     </RadioGroup>  
  45. </LinearLayout>  

 運(yùn)行結(jié)果如下:



 假如我們選擇杭州,會(huì)提示錯(cuò)誤的Toast



 再次選中成都后,會(huì)提示答案正確



 這里就可以看到,單選按鈕的使用效果,如果只是使用RadioButton的話,把配置文件中RadioGroup去掉,當(dāng)然,要重新為每個(gè)單選按鈕設(shè)置監(jiān)聽,這樣,這個(gè)RadioButton就跟Button沒有什么區(qū)別了,我們可以選中多個(gè),所以要注意,單選按鈕要和RadioGroup一起使用,才能夠?qū)崿F(xiàn)單選的功能

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多