免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項超值服

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

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

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

RadioButton的單選按鈕;

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

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

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

 

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

例子:

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

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("請注意,回答錯誤!");  
  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">哪個城市美女多?</string>  
  4.     <string name="app_name">單選按鈕測試</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個RadioButton包含在RadioGroup中

 

Java代碼  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/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é)果如下:



 假如我們選擇杭州,會提示錯誤的Toast



 再次選中成都后,會提示答案正確



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

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Android從零開始(3)(常用控件+下拉框視圖)(新)
Android RadioButton控件
Android軟件開發(fā)之盤點常用系統(tǒng)控件界面大合集(三)
一步一步android(8):關(guān)于界面控件的學(xué)習(xí)2【edittext、radiogroup、checkbox】
Android學(xué)習(xí)筆記(六):xml和widget
5.2.2 Fragment實例精講
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服