Radiobutton、Checkbox

-、區別:  RadioButton只能單選 ,他外面要包含一個 radioButton控件。 而checkbox 能多選擇。

                   RadioButton一般的形狀是圓形的 而checkbox是正方形的。

                  RadioButton單選按鈕是一種雙狀態的按鈕,可以選擇或不選中。在單選按鈕 沒有被選中時,用戶能夠按下或點擊來選中它。但是,與複選框相反,用戶一旦選中就                   不能夠通過界面取消選中,但是可以通過代碼來取消選中狀態。

                 

 

     <?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" >

    <RadioGroup 
        android:id="@+id/rgsex"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioButton 
            android:checked="true" 
            android:id="@+id/rbman"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/strman"
            />
        <RadioButton 
            android:id="@+id/rbwoman"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/strwoman"
            />
    </RadioGroup>
    <CheckBox
         android:id="@+id/cbrun"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="@string/strrun"/>
    <CheckBox
         android:id="@+id/cbswim"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="@string/strswim"/>
    <CheckBox
         android:id="@+id/cbread"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="@string/strread"/>
</LinearLayout>

  

然後就是添加事件了,RadioButton的事件添加在RadioGroup上面

  rgsex=(RadioGroup)findViewById(R.id.rgsex);
        rgsex.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId==R.id.rbman) {
                    makeToast(Radiobutton_Checkbox_MenuActivity.this,"你選擇了男");
                }else if (checkedId==R.id.rbwoman) {
                    makeToast(Radiobutton_Checkbox_MenuActivity.this,"你選擇了女");
                }
            }
        });


 裏面用到了一個makeToast方法,是我在下面定義的,就是彈出Toast提示。其他代碼很簡單,不做過多的說明了。注意它的OnCheckedChangeListener,因爲下面也有一個。


然後添加CheckBox的事件,三個控件要依次添加方法,爲了簡便,實現以下這個藉口。如下

//處理checkbox事件
        cbrun=(CheckBox)findViewById(R.id.cbrun);
        cbswim=(CheckBox)findViewById(R.id.cbswim);
        cbread=(CheckBox)findViewById(R.id.cbread);
        /*因爲CompoundButton是checkbox的父類,這裏用CompoundButton.OnCheckedChangeListener來綁定事件,
         * 否則會和上面的radiogroup的事件衝突*/
        CompoundButton.OnCheckedChangeListener listener=new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                String str=buttonView.getText().toString();
                if (isChecked) {
                    makeToast(Radiobutton_Checkbox_MenuActivity.this,str+"被選中");
                }else {
                    makeToast(Radiobutton_Checkbox_MenuActivity.this,str+"被取消");
                }
            }
        };
        cbrun.setOnCheckedChangeListener(listener);
        cbswim.setOnCheckedChangeListener(listener);
        cbread.setOnCheckedChangeListener(listener);

自定義radiobutton樣式並去掉默認的樣式

 android:button=“@null”

android:backgroup="@null"


在按鈕的左邊添加圖片

 android:drawableLeft="@drawable/male"



在按鈕的右邊添加圖片 
 android:drawableRight="@drawable/radio_selector"


 按鈕與描述文字之間的距離

 android:drawablePadding="10dp"



  

發佈了21 篇原創文章 · 獲贊 6 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章