中级控件——复选框CheckBox——系统控件CheckBox

 

 

 

CompoundButton类是抽象的复合按钮,由它派生而来的子类包括:复选框CheckBox、单选按钮RadioButton以及开关按钮Switch。

 

下图描述了复合按钮的继承关系:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

CompoundButton在XML文件中主要使用下面两个属性。
(1)、checked:指定按钮的勾选状态,true表示勾选,false表示未勾选。默认未勾选。
(2)、button:指定左侧勾选图标的图形资源。如果不指定就使用系统的默认图标。

 


CompoundButton在Java代码中主要使用下列4种方法。
(1)、setChecked:设置按钮的勾选状态。
(2)、setButtonDrawable:设置左侧勾选图标的图形资源。
(3)、setOnCheckedChangeListener:设置勾选状态变化的监听器。
(4)、isChecked:判断按钮是否勾选。

 

 

 

 

 

 

 

 

 

// 该页面实现了接口OnCheckedChangeListener,意味着要重写勾选监听器的onCheckedChanged方法
public class CheckBoxActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); setContentView(R.layout.activity_check_box);
CheckBox ck_system
= findViewById(R.id.ck_system); // 从布局文件中获取名叫ck_system的复选框
// 给ck_system设置勾选监听器,一旦用户点击复选框,就触发监听器的onCheckedChanged方法 ck_system.setOnCheckedChangeListener(this); } public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ String desc
= String.format("您%s了这个CheckBox", isChecked ? "勾选" : "取消勾选"); buttonView.setText(desc); } }

 

 

 

 

 

 

 

===================================================================================================================

 

 

 

 

 

我们以系统自带的CheckBox为示例:

 

 

 

<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:checked="false"
        android:text="这是系统的CheckBox"
        android:textColor="@color/black"
        android:textSize="17sp" />

<!--    <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="false"-->
<!--        android:text="这个CheckBox换了图标"-->
<!--        android:textColor="@color/black"-->
<!--        android:textSize="17sp" />-->

</LinearLayout>

 

 

 

 

 

 

 

 

 

 

 

package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("DefaultLocale")
// 该页面实现了接口OnCheckedChangeListener,意味着要重写勾选监听器的onCheckedChanged方法
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener
{

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


        // 从布局文件中获取名叫ck_system的复选框
        CheckBox ck_system = findViewById(R.id.ck_system);

        // 从布局文件中获取名叫ck_custom的复选框
//        CheckBox ck_custom = findViewById(R.id.ck_custom);

        // 给ck_system设置勾选监听器,一旦用户点击复选框,就触发监听器的onCheckedChanged方法
        ck_system.setOnCheckedChangeListener(this);

        // 给ck_custom设置勾选监听器,一旦用户点击复选框,就触发监听器的onCheckedChanged方法
//        ck_custom.setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        String desc = String.format("您%s了这个CheckBox", isChecked ? "勾选" : "取消勾选");
        buttonView.setText(desc);
    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2022-07-09 16:05  小白龙白龙马  阅读(180)  评论(0编辑  收藏  举报