第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

全部開發(fā)者教程

Android 入門教程

菜單類控件
菜單:Menu
并發(fā)編程
多線程

選擇框 RadioButton/Check

在學完 Button 之后,我們已經(jīng)可以和用戶產(chǎn)生一定的互動了,但僅僅這些還遠遠不夠,很多時候我們需要給用戶提供一些選項,比如“記住密碼”、“自動登錄”、“投票”等場景,我們需要提供一個或者多個選項給用戶勾選。這種場景下就可以使用 RadioButton 和 Checkbox ,這二者的區(qū)別就是前者是單選,而后者支持多選。

1. RadioButton

RadioButton 和 Checkbox 的屬性和用法大體相同,我們先來看看單選框的實現(xiàn)方式。

1.1 RadioButton 的基本用法

當你的 App 需要提供幾個選項讓用戶做單選的時候,RadioButton 毫無疑問是最佳選擇。 RadioButton 需要配合 RadioGroup 一起使用, RadioGroup 可以包含一個或若干個 RadioButton ,每個 RadioButton 對應一個選項可供用戶點擊,而一個 RadioGroup 中只有一個 RadioButton 可以進入點擊態(tài)。比如我們做一個二選一的單選框,布局代碼如下:

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp">

    <RadioButton
        android:id="@+id/rb_male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

    <RadioButton
        android:id="@+id/rb_female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</RadioGroup>

在代碼中我們設置了一個 RadioGroup ,里面包含兩個 RadioButton 分別表示“男”、“女”兩個選項,最終用戶只能選擇其一,效果如下:

RadioButton基本使用

注意:類似的功能我們還可以用 Spinner View 實現(xiàn),與之不同的是 Spiner View 只會展示當前選中的選項,其他選項會被收集到下拉列表中,具體的使用我們會在后面的教程中詳細介紹。

1.2 RadioButton 的屬性

如果你運行過上面例子中的代碼, 會發(fā)現(xiàn)默認狀態(tài)下兩個選項都沒有選中,但是一旦用戶選擇其中之一就無法在撤銷選擇,只能更改選項,也就是 RadioButton 沒有給我們提供默認選項。我們可以通過android:checked屬性來實現(xiàn)默認選擇,即 RadioButton 中的android:checked屬性為 true 的選項會默認被選中,如果有多個 true,那么只有最后一個被選中。

android:checked="true"

1.3 RadioButton 的排列方式

通過剛剛的例子,我們知道 RadioButton 是需要放到 RadioGroup 當中使用的,所以可以才想 RadioGroup 也是一個 ViewGroup 。沒錯, RadioGroup 是繼承自 LinearLayout 的,所以可以推測 RadioGroup 也有線性布局的特點,即可以選擇橫向或者縱向。我們按照 LinearLayout 的寫法修改代碼,在 ViewGroup 中添加方向屬性:

android:orientation="horizontal"

最后運行效果如下:

RadioGroup橫向樣式

1.4 獲取 RadioButton 的選中結果

與 EditText 一樣,我們不僅要通過布局樣式輸出給用戶,還需要得到用戶的輸入數(shù)據(jù),對于 RadioButton 而言就是用戶的選項。與 Button 的setOnClickListener類似,我們通過 RadioGroup 的setOnCheckedChangeListener接口注冊一個選項變更監(jiān)聽器,依舊采用上面的布局,在 Activity 的onCreate()中增加 Java 代碼如下:

package com.emercy.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RadioGroup radioGroup = findViewById(R.id.group);
        radioGroup.setOnCheckedChangeListener(this);

    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        String btText = "";
        switch (checkedId) {
            case R.id.rb_female:
                btText = "性別女";
                break;
            case R.id.rb_male:
                btText = "性別男";
                break;
            default:
                btText = "未選中";
                break;
        }

        Toast.makeText(this, "您選擇了" + btText, Toast.LENGTH_SHORT).show();
    }
}

在切換選擇的時候,監(jiān)聽器就會收到回調,參數(shù)是被選擇的 RadioGroup 及被選擇的 View id,也就是在 xml 中設置的 id。編譯運行,在選項被選擇之后打印出當前的選項,如下:

獲取RadioButton選中狀態(tài)

有兩點需要注意:

  • 監(jiān)聽器是注冊在 RadioGroup 之上的,所以我們需要通過findViewById獲取 RadioGroup 而不是 RadioButton 。
  • 監(jiān)聽器傳入的回調是 RadioGroup 類當中的OnCheckedChangeListener接口,所以 Activity 實現(xiàn)的是RadioGroup.OnCheckedChangeListener接口,與之對應的還有 RadioButton / Checkbox 的父類 CompoundButton 里也有一個OnCheckedChangeListener接口,這個會在 Checkbox 的部分講到,注意區(qū)分。

2. Checkbox

在學完 RadioButton 之后,Checkbox 就比較好理解了,它可以支持多個選項同時處于選擇狀態(tài),其常用屬性和 RadioButton 一樣,同樣我們可以設置默認被勾選的選項。

2.1 Checkbox 的基本用法

由于不限制選中數(shù)量,Checkbox 控件不存在類似 RadioGroup 的父容器,我們可以直接在布局文件中寫<Checkbox/>標簽,如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/cb_android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Android" />

    <CheckBox
        android:id="@+id/cb_ios"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="iOS" />

</LinearLayout>

Checkbox基本用法

我們看到可以有兩個選項同時被選中,和 RadioButton 一樣,通過android:checked屬性設置默認選中的選項。

2.2 獲取 Checkbox 的選中結果

Checkbox 的選中結果和它的兄弟控件 RadioButton 非常類似,通過 CheckBox 的setOnCheckedChangeListener設置監(jiān)聽器,在選項被選中或者取消的時候會回調監(jiān)聽器的onCheckedChanged方法,我們基于以上布局,直接編寫 Activity 代碼:

package com.emercy.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CheckBox btAndroid = findViewById(R.id.cb_android);
        CheckBox btIOS = findViewById(R.id.cb_ios);
        btAndroid.setOnCheckedChangeListener(this);
        btIOS.setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        String text = isChecked ? "被選中了!" : "被取消了!";
        Toast.makeText(this, buttonView.getText() + text, Toast.LENGTH_SHORT).show();
    }
}

與 RadioButon 不同的是setOnCheckedChangeListener接口傳入的是 CompoundButton 類中的OnCheckedChangeListener,所以 Activity 需要實現(xiàn)CompoundButton.OnCheckedChangeListener,此方法傳入被選中 / 取消的 Checkbox 對象以及選中的狀態(tài)。編譯之后切換選擇,效果如下:

獲取Checkbox的選中結果

3. 小結

本節(jié)我們學習了兩個非常有趣又實用的控件——RadioButton / Checkbox,我們學習了如何編寫編寫樣式及獲取用戶的選擇結果,并完成了一個簡單的小練習。它們都是派生自 Button,相比 Button 來講使用場景更具體,所以今后在遇到需要選擇的場景,不要忘了這兩個非常好用的控件。