目录

1、复选框CheckBox 

 2、开关按钮(Switch)

3  单选按钮RadioButton


包括:如何使用复选框CheckBox及其勾选监听器、如何使用开关按钮Switch、如何借助状态列表图形实现仿iOS的开关按钮、如何使用单选按钮RadioButton和单选组RadioGroup及其选中监听器。

 

1、复选框CheckBox 

package com.example.chapter05;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class CheckboxActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
    CheckBox ck_system;
    CheckBox ck_custom;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);
         ck_custom = findViewById(R.id.ck_custom);
         ck_system=findViewById(R.id.ck_system);
         first one=new first();

        ck_custom.setOnCheckedChangeListener(one);
        ck_system.setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        String desc=String.format("您%s了这个checkbox",b?"勾选":"取消勾选");
        ck_system.setText(desc);
    }
    class first implements CompoundButton.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            ck_custom.setText("yemeisha");
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">

    <CheckBox
        android:id="@+id/ck_system"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="这是系统的CheckBox"/>

    <CheckBox
        android:id="@+id/ck_custom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:button="@drawable/checkbox_selector"
        android:padding="5dp"
        android:checked="true"
        android:text="这个换了图标"/>


</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/check_choose"/>
    <item android:drawable="@drawable/check_unchoose"/>
</selector>

 

 

CompoundButton在XML文件中主要使用下面两个属性

 checked:指定按钮的勾选状态,true表示勾选,false表示未勾选。默认未勾选。 button:指定左侧勾选图标的图形资源。如果不指定就使用系统的默认图标。 CompoundButton在Java代码中主要使用下列4种方法。 setChecked:设置按钮的勾选状态。

setButtonDrawable:设置左侧勾选图标的图形资源。

setOnCheckedChangeListener:设置勾选状态变化的监听器。

isChecked:判断按钮是否勾选。

 2、开关按钮(Switch)

Switch是开关按钮,它在选中与取消选中时可展现的界面元素比复选框丰富。

Switch控件新添加的XML属性说明如下。

textOn:设置右侧开启时的文本。

textOff:设置左侧关闭时的文本。

track:设置开关轨道的背景。

thumb:设置开关标识的图标。

 

 

仿iOS的开关按钮

借助状态列表图形StateListDrawable,分别定义已选中时候的“开”图形,以及未选中时候的“关”图形。 状态列表图形的XML文件如下:

<selector xmlns:android="http://schemas.android.com/apk/res/android">    

<item android:state_checked="true" android:drawable="@drawable/switch_on"/>  

  <item android:drawable="@drawable/switch_off"/>

</selector>

然后把CheckBox控件的background属性设置为该状态图形。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

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

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="left|center_vertical"
            android:text="Switch开关:"
            android:textColor="@color/black"
            android:textSize="17sp" />

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

            <Switch
                android:id="@+id/sw_status"
                android:layout_width="80dp"
                android:layout_height="30dp" />
        </LinearLayout>
    </LinearLayout>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="left"
        android:textColor="@color/black"
        android:textSize="17sp" />

</LinearLayout>

 

package com.example.chapter05;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class SwitchIOSActivity extends AppCompatActivity implements OnCheckedChangeListener {
    private CheckBox ck_status; // 声明一个复选框对象
    private TextView tv_result; // 声明一个文本视图对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_switch_ios);
        // 从布局文件中获取名叫sw_status的开关按钮
        ck_status = findViewById(R.id.ck_status);
        // 从布局文件中获取名叫tv_result的文本视图
        tv_result = findViewById(R.id.tv_result);
        // 给开关按钮设置选择监听器,一旦用户点击它,就触发监听器的onCheckedChanged方法
        ck_status.setOnCheckedChangeListener(this);
        refreshResult(); // 刷新仿iOS按钮的开关说明
    }

    // 刷新仿iOS按钮的开关说明
    private void refreshResult() {
        String result = String.format("仿iOS开关的状态是%s",
                (ck_status.isChecked()) ? "开" : "关");
        tv_result.setText(result);
    }

    // 选择事件的处理方法
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        refreshResult();
    }

}
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true"
        android:drawable="@drawable/switch_on"/>
    <item android:state_checked="false"
        android:drawable="@drawable/switch_off"/>

</selector>

3  单选按钮RadioButton

单选按钮要在一组按钮中选择其中一项,并且不能多选,这要求有个容器确定这组按钮的范围,这个容器便是单选组RadioGroup。

RadioGroup实质上是个布局,同一组RadioButton都要放在同一个RadioGroup节点下。

除了RadioButton,也允许放置其他控件。

单选组与线性布局相比,它们主要有以下两个区别:

(1)单选组多了管理单选按钮的功能,而线性布局不具备该功能;

(2)如果不指定orientation属性,那么单选组默认垂直排列,而线性布局默认水平排列

 

判断选中了哪个单选按钮,通常不是监听某个单选按钮,而是监听单选组的选中事件。
下面是RadioGroup常用的3个方法。
check:选中指定资源编号的单选按钮。
getCheckedRadioButtonId:获取选中状态单选按钮的资源编号。
setOnCheckedChangeListener:设置单选按钮勾选变化的监听器。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐