Android學習筆記之RadioButton(RadioGroup)

來源:互聯網
上載者:User

RadioButton(選項按鈕)在Androi發中應用的非常廣泛,比如一些選擇項的時候,會用到選項按鈕。它是一種單個圓形單選框雙狀態的按鈕,可以選擇或不選擇。在RadioButton沒有被選中時,使用者能夠按下或點擊來選中它。但是,與複選框相反,使用者一旦選中就不能夠取消選中。
    實現RadioButton由兩部分組成,也就是RadioButton和RadioGroup配合使用.RadioGroup是單選組合框,可以容納多個RadioButton的容器.在沒有RadioGroup的情況下,RadioButton可以全部都選中;當多個RadioButton被RadioGroup包含的情況下,RadioButton只可以選擇一個。並用setOnCheckedChangeListener來對選項按鈕進行監聽
main.xml
 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <TextView 
        android:id="@+id/sex" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/sex" /> 
    <RadioGroup  
        android:id="@+id/radiogroup" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical" 
        > 
        <RadioButton  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:id="@+id/female" 
            android:text="@string/female" 
            ></RadioButton> 
        <RadioButton  
            android:id="@+id/male" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="@string/male" 
            android:checked="true" 
            ></RadioButton> 
    </RadioGroup> 
 
</LinearLayout> 
 
 
RadioGroupActivity.java
通過控制項的ID來得到代表控制項的對象
然後為RadioGroup設定監聽器
 
package Android.Activity; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.Toast; 
 
public class RadioGroupActivity extends Activity { 
    /** Called when the activity is first created. */ 
    private RadioButton maleButton = null; 
    private RadioButton femaleButton = null; 
    private RadioGroup radiogroup =null; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        //通過控制項的ID來得到代表控制項的對象 
        maleButton = (RadioButton)findViewById(R.id.male); 
        femaleButton = (RadioButton)findViewById(R.id.female); 
        radiogroup = (RadioGroup)findViewById(R.id.radiogroup); 
        //為RadioGroup設定監聽器,需要注意的是,這裡的監聽器和Button控制項的監聽器有所不同 
        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
             
            public void onCheckedChanged(RadioGroup group, int checkedId) { 
                // TODO Auto-generated method stub 
                if(femaleButton.getId() == checkedId){ 
                    Toast.makeText(RadioGroupActivity.this, "female", Toast.LENGTH_SHORT).show(); 
                } 
                else if(maleButton.getId() == checkedId){ 
                    Toast.makeText(RadioGroupActivity.this, "male", Toast.LENGTH_SHORT).show(); 
                } 
            } 
        }); 
    } 
}   

摘自 落日小屋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.