Android UI控制項系列:RadioButton(選項按鈕)

來源:互聯網
上載者:User
選項按鈕RadioButton在Android平台上也應用的非常多,比如一些選擇項的時候,會用到選項按鈕,實現選項按鈕由兩部分組成,也就是RadioButton和RadioGroup配合使用。

RadioButton的選項按鈕;

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

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

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

注意:選項按鈕的事件監聽用setOnCheckedChangeListener來對選項按鈕進行監聽

例子:

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

RadioTest.java

package org.loulijun.radio;import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.TextView;import android.widget.Toast;public class RadioTest extends Activity {    /** Called when the activity is first created. */        TextView textview;        RadioGroup radiogroup;        RadioButton radio1,radio2,radio3,radio4;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        textview=(TextView)findViewById(R.id.textview1);        radiogroup=(RadioGroup)findViewById(R.id.radiogroup1);        radio1=(RadioButton)findViewById(R.id.radiobutton1);        radio2=(RadioButton)findViewById(R.id.radiobutton2);        radio3=(RadioButton)findViewById(R.id.radiobutton3);        radio4=(RadioButton)findViewById(R.id.radiobutton4);        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {                        @Override                        public void onCheckedChanged(RadioGroup group, int checkedId) {                                // TODO Auto-generated method stub                                if(checkedId==radio2.getId())                                {                                        DisplayToast("正確答案:"+radio2.getText()+",恭喜你,回答正確!");                                }else                                {                                        DisplayToast("請注意,回答錯誤!");                                }                        }                });    }    public void DisplayToast(String str)    {            Toast toast=Toast.makeText(this, str, Toast.LENGTH_LONG);            toast.setGravity(Gravity.TOP,0,220);            toast.show();    }}

strings.xml檔案

<?xml version="1.0" encoding="utf-8"?>  <resources>      <string name="hello">哪個城市美女多?</string>      <string name="app_name">選項按鈕測試</string>      <string name="radiobutton1">杭州</string>      <string name="radiobutton2">成都</string>      <string name="radiobutton3">重慶</string>      <string name="radiobutton4">蘇州</string>  </resources>

main.xml檔案:注意,這裡面,4個RadioButton包含在RadioGroup中

<?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:layout_width="fill_parent"    android:layout_height="wrap_content"    android:text="@string/hello"    android:id="@+id/textview1"    />    <RadioGroup            android:id="@+id/radiogroup1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:orientation="vertical"            android:layout_x="3px"    >            <RadioButton                    android:id="@+id/radiobutton1"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="@string/radiobutton1"            />            <RadioButton                    android:id="@+id/radiobutton2"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="@string/radiobutton2"            />            <RadioButton                    android:id="@+id/radiobutton3"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="@string/radiobutton3"            />            <RadioButton                    android:id="@+id/radiobutton4"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="@string/radiobutton4"            />    </RadioGroup></LinearLayout>

運行結果如下:

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

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


這裡就可以看到,選項按鈕的使用效果,如果只是使用RadioButton的話,把設定檔中RadioGroup去掉,當然,要重新為每個選項按鈕設定監聽,這樣,這個RadioButton就跟Button沒有什麼區別了,我們可以選中多個,所以要注意,選項按鈕要和RadioGroup一起使用,才能夠實現單選的功能。

以上就是Android UI控制項系列:RadioButton(選項按鈕)的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!


  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.