Android的ToggleButton、RadioButton和CheckBox

Android的ToggleButton、RadioButton和CheckBox

ToggleButton

ToggleButton:开关按钮,继承于CompoundButton,是一个具有选中和未选中两种状态的按钮,并且可以为其设置不同状态下的显示文本。

常用方法:

1、android:disabledAlpha:设置按钮在禁用时的透明度。

2、android:textOf:未选中时按钮的显示文本

3、android:textOn:选中时按钮的显示文本

4、android:checked:设置按钮是否选中

布局页面XML代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.474"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.088" />

    <ToggleButton
        android:id="@+id/togglebutton"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="84dp"
        android:textOff="还没打开"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.452"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />


</androidx.constraintlayout.widget.ConstraintLayout>

后台代码:

package com.example.checkradio;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {
    
    

    //定义全局变量
    private ToggleButton toggleButton;
    private TextView textView;

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

        //绑定布局文件XML的控件
        textView = findViewById(R.id.textView2);
        toggleButton = findViewById(R.id.togglebutton);

        //设置控件被选中时的显示文本
        toggleButton.setTextOn("点击我,打开我");
        //设置控件未被选中时的显示文本
        toggleButton.setTextOff("关上啦");
        //设置默认选中时的文本
//        toggleButton.setChecked(false);

        //为开关按钮绑定监听事件
        //当选中时 TextView控件内容显示 “抽奖礼盒已打开”
        //当未被选中时 TextView控件内容显示 “被迫关上了,重新打开吧”
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
    
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    
    
                if(isChecked){
    
    
                    textView.setText("抽奖礼盒已打开");
                }else {
    
    
                    textView.setText("被迫关上了,重新打开吧");
                }
            }
        });

    }
}

RadioButton

RadioButton:单选按钮,继承于CompoundButton,是一个具有选中和未选中两种状态的按钮。

单选按钮没有被选中时,用户可按下或点击它,一旦被选中就不能取消。

RadioButton配合RadioGroup使用,表示一个单选按钮组,包含几个单选按钮,只能选择其中一个,选择了其中一个,其他默认取消选择。

布局XML文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="56dp"
        android:text="选择一个你想要的礼物"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <RadioGroup
        android:id="@+id/radioGroup2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="151dp"
        android:layout_marginLeft="151dp"
        android:layout_marginTop="50dp"
        android:layout_marginEnd="151dp"
        android:layout_marginRight="151dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView">

        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="苹果手机" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="笔记本电脑" />

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="豪车" />

        <RadioButton
            android:id="@+id/radioButton4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="房产证" />

        <RadioButton
            android:id="@+id/radioButton5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="五万块" />

    </RadioGroup>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="选择结果"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/radioGroup2" />
</androidx.constraintlayout.widget.ConstraintLayout>

后台代码:

package com.example.checkradio;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity {
    
    

    //定义全局变量
    private TextView textView;
    private RadioGroup radioGroup;

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

        //绑定布局文件XML的控件
        textView = findViewById(R.id.textView2);
        radioGroup = findViewById(R.id.radioGroup2);

        //绑定radioGroup的监听事件
        radioGroup.setOnCheckedChangeListener(new choose());
    }

    //实现监听事件
    private class choose implements RadioGroup.OnCheckedChangeListener {
    
    
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
    
    
            RadioButton choice = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());
            textView.setText("你的选择是" + choice.getText().toString());
        }
    }
    
}

CheckBox

CheckBox:复选框,可以具有选中和不选中两种状态。可以在布局文件定义多个多选按钮然后对每个多选按钮设置事件监听。可以通过isChecked属性来判断选项是否被选中,做出相应的事件响应。

用isChecked()方法检查一个CheckBox是否被选中。

布局XML代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity3">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="56dp"
        android:text="以下哪些是你喜欢的做的事情"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView">

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="看电视" />

        <CheckBox
            android:id="@+id/checkBox2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="玩王者荣耀" />

        <CheckBox
            android:id="@+id/checkBox3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="玩各种网游游戏" />

        <CheckBox
            android:id="@+id/checkBox4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="和好友逛街" />

        <CheckBox
            android:id="@+id/checkBox5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="去K歌" />

        <CheckBox
            android:id="@+id/checkBox6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="去户外运动" />
    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="316dp"
        android:text="提交"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.08"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="372dp"
        android:text=""
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.467"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

后台代码:

package com.example.checkradio;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class MainActivity3 extends AppCompatActivity implements View.OnClickListener {
    
    

    //定义全局变量
    private TextView textView;
    private CheckBox cb1,cb2,cb3,cb4,cb5,cb6;
    private Button button;

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

        //绑定XML布局控件
        cb1 = findViewById(R.id.checkBox1);
        cb2 = findViewById(R.id.checkBox2);
        cb3 = findViewById(R.id.checkBox3);
        cb4 = findViewById(R.id.checkBox4);
        cb5 = findViewById(R.id.checkBox5);
        cb6 = findViewById(R.id.checkBox6);

        textView = findViewById(R.id.textView2);
        button = findViewById(R.id.button);

        //绑定事件监听
        button.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
    
    

        String str = "你的选择是:";
        //isChecked()方法检查一个CheckBox是否被选中
        if (cb1.isChecked()){
    
    
            str = str + " " + cb1.getText();
        }
        if (cb2.isChecked()){
    
    
            str = str + " " + cb2.getText();
        }
        if (cb3.isChecked()){
    
    
            str = str + " " + cb3.getText();
        }
        if (cb4.isChecked()){
    
    
            str = str + " " + cb4.getText();
        }
        if (cb5.isChecked()){
    
    
            str = str + " " + cb5.getText();
        }
        if (cb6.isChecked()){
    
    
            str = str + " " + cb6.getText();
        }
        textView.setText(str);

    }
}

以下这个监听单个CheckBox的选中与未选中,同时在外面获得所有CheckBox的状态,即将所有的CheckBox放到列表里,监听单个CheckBox用setOnCheckedChangeListener(),布局页面一样,后台代码如下:

package com.example.checkradio;

import androidx.appcompat.app.AppCompatActivity;

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

import java.util.ArrayList;
import java.util.List;

public class MainActivity3 extends AppCompatActivity  implements View.OnClickListener {
    
    

    //定义全局变量
    private TextView textView;
    private CheckBox cb1,cb2,cb3,cb4,cb5,cb6;
    private Button button;
    private List<CheckBox> cblist = new ArrayList<CheckBox>();

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

        //绑定XML布局控件
        cb1 = findViewById(R.id.checkBox1);
        cb2 = findViewById(R.id.checkBox2);
        cb3 = findViewById(R.id.checkBox3);
        cb4 = findViewById(R.id.checkBox4);
        cb5 = findViewById(R.id.checkBox5);
        cb6 = findViewById(R.id.checkBox6);

        textView = findViewById(R.id.textView2);
        button = findViewById(R.id.button);

        //将CheckBox放入列表
        cblist.add(cb1);
        cblist.add(cb2);
        cblist.add(cb3);
        cblist.add(cb4);
        cblist.add(cb5);
        cblist.add(cb6);

        //设置事件监听
        button.setOnClickListener(this);

        //监听单个CheckBox的选中状态
        for (final CheckBox cbx:cblist){
    
    
            cbx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
    
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    
    
                    if(cbx.isChecked()){
    
    
                        Toast.makeText(MainActivity3.this,"选中",Toast.LENGTH_SHORT).show();
                    }else {
    
    
                        Toast.makeText(MainActivity3.this,"未选中",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }

    }

    //监听所有CheckBox的选中状态
    @Override
    public void onClick(View v) {
    
    
        String str = "你的选择是:";
        for (CheckBox cbx:cblist){
    
    
            if (cbx.isChecked()){
    
    
                //字符串拼接
                str = str.concat(cbx.getText().toString()+ " ");
            }
        }
        textView.setText(str);
    }
}

猜你喜欢

转载自blog.csdn.net/leilei__66/article/details/109451580