Android | RadioButton與Checkbox事件處理


1. RadioButton(單選按鈕)

1.1 RadioButton XML佈局

RadioButton單選按鈕,也就是一組單選按鈕,我們只能選擇其中的一個。RadioButton要包含在RadioGroup中。

看一下佈局效果(圖片有點小):

在這裏插入圖片描述
XML代碼:

    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="20sp" />
        <RadioButton
            android:id="@+id/female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="20sp" />
    </RadioGroup>

1.2 RadioButton三種事件處理方法

1.2.0 事件處理大致步驟

點擊訪問事件處理步驟博客~ (真的只是點擊

1.2.1 demo需求

實現一個單選框,然後用不同的事件處理方法,把我們選中的某個單選按鈕的十六進制Id值文本內容輸出在控制檯上。

1.2.2 onClickListener

onClickListener是老朋友了,也是一個最基本的控件,我們只需要實現onClickListener接口,重寫其中的onClick方法,最後綁定到控件上就OK了。

先來看一下最後的輸出:

在這裏插入圖片描述
下面是代碼,註釋已經標好了:

public class MainActivity extends AppCompatActivity {
    //0、聲明對象
    private RadioGroup rg;
    private RadioButton male, female;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo);
        //1、findViewById獲取對象
        rg = (RadioGroup) findViewById(R.id.rg);
        male = (RadioButton) findViewById(R.id.male);
        female = (RadioButton) findViewById(R.id.female);
        //3、爲控件綁定監聽事件
        RadioOnClick roc = new RadioOnClick();
        male.setOnClickListener(roc);
        female.setOnClickListener(roc);
    }

    //2、實現OnClickListener接口,重寫onClick方法
    class RadioOnClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            RadioButton rb = (RadioButton) v;
            String m = rb.getId() + "-------->" + rb.getText();
            System.out.println(m);
        }
    }
}

1.2.3 RadioGroup.onCheckedChangeListener

用這個onCheckedChangeListener只需要綁定到RadioGroup組件上就好了,需要重寫onCheckedChanged方法,這個方法包含下面兩個參數:

  • RadioGroup group:這個參數就是傳來的RadioGroup對象
  • int CheckedId:這個參數傳來的是被選中的某個按鈕的id屬性,我們用它來判斷誰被選中了。

看一下最後的輸出結果:

在這裏插入圖片描述
代碼:

public class MainActivity extends AppCompatActivity {
    //0、聲明對象
    private RadioGroup rg;
    private RadioButton male, female;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo);
        //1、findViewById獲取對象
        rg = (RadioGroup) findViewById(R.id.rg);
        male = (RadioButton) findViewById(R.id.male);
        female = (RadioButton) findViewById(R.id.female);
        //2、匿名內部類直接懟
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                String m = "OnCheckedChangeListener:";
                if (checkedId == R.id.male) m += male.getId() + "--->" + male.getText();
                else m += female.getId() + "--->" + female.getText();
                System.out.println(m);
            }
        });
    }
}

因爲只設置了兩個單選按鈕,可以直接判斷。如果有的人懟了超級多RadioButton,可以直接使用循環去判斷,從0開始循環到group.getChildCount個,判斷有沒有被選中的就可以了。

1.2.4 CompoundButton.OnCheckedChangeListeren

這個接口跟上面的那個接口重名,但是不屬於同一個包。用這個接口需要實現onCheckedChanged方法,也和上面的那個方法一樣。但是傳遞的參數不一樣。兩個參數分別是:

  • CompoundButton buttonView:傳遞過來的類型
  • boolean isChecked:是否被選中

看一下效果:

在這裏插入圖片描述
代碼:

public class MainActivity extends AppCompatActivity {
    //0、聲明對象
    private RadioGroup rg;
    private RadioButton male, female;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo);
        //1、findViewById獲取對象
        rg = (RadioGroup) findViewById(R.id.rg);
        male = (RadioButton) findViewById(R.id.male);
        female = (RadioButton) findViewById(R.id.female);
        //3、綁定
        RadioOnClick roc = new RadioOnClick();
        male.setOnCheckedChangeListener(roc);
        female.setOnCheckedChangeListener(roc);

    }
    //2、實現接口,重寫方法
    class RadioOnClick implements CompoundButton.OnCheckedChangeListener{
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            String m = "";
            if(buttonView.getId()==R.id.male) m+=buttonView.getText() + "---->";
            else m+=buttonView.getText() + "----->";
            if(isChecked) m+="被選中了!";
            else m+="沒有被選中!";
            System.out.println(m);
        }
    }
}

2. CheckBox(複選框)

2.1 CheckBox XML佈局

CheckBox的佈局和上面的RadioButton佈局很相似,直接寫就好了。

XML代碼:

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/eat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="吃飯"
            android:textSize="18sp" />

        <CheckBox
            android:id="@+id/sleep"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="睡覺"
            android:textSize="18sp" />

        <CheckBox
            android:id="@+id/game"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="王者榮耀"
            android:textSize="18sp" />
    </LinearLayout>

效果展示:
在這裏插入圖片描述

2.2 CheckBox的事件處理

2.2.1 OnClick方法

和上述的RadioButton幾乎一樣,這裏不再展示

2.2.2 CompoundButton.OnCheckedChangeListener

也和上述的一樣,這個方法同樣有兩個參數,因爲上面已經說過了,直接寫個案例,先來看看效果:

在這裏插入圖片描述
java代碼:

public class MainActivity extends AppCompatActivity {

    private CheckBox eat, game, sleep;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo);
        eat = (CheckBox) findViewById(R.id.eat);
        sleep = (CheckBox) findViewById(R.id.sleep);
        game = (CheckBox) findViewById(R.id.game);
        CheckChange cc = new CheckChange();
        eat.setOnCheckedChangeListener(cc);
        sleep.setOnCheckedChangeListener(cc);
        game.setOnCheckedChangeListener(cc);
    }

    class CheckChange implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            String m = "";
            switch (buttonView.getId()) {
                case R.id.eat:
                    m += buttonView.getText();
                    break;
                case R.id.sleep:
                    m += buttonView.getText();
                    break;
                case R.id.game:
                    m += buttonView.getText();
                    break;
            }
            System.out.println(m);
        }
    }
}

ok啦!

如果我們想設置某一個被選中那就直接用setChecked(true)就夠了!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章