Android 中文 API (30) —— CompoundButton.OnCheckedChangeListener

本文介绍了Android中CompoundButton.OnCheckedChangeListener接口的作用与使用方法。此接口用于监听CompoundButton(如CheckBox、RadioButton)的状态变化,并提供了onCheckedChanged方法来实现状态改变时的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

  本章内容是android.widget.CompoundButton.OnCheckedChangeListener,翻译来自德罗德,再次感谢德罗德 !期待你一起参与Android API 的中文翻译,联系我over140@gmail.com。
 

 

声明

  欢迎转载,但请保留文章原始出处:)
 

    农民伯伯:http://over140.blog.51cto.com/

    Android中文翻译组:http://www.cnblogs.com/over140/  

 

正文

  一、结构

    public static interface CompoundButton.OnCheckedChangeListener

 

  二、公共方法
 

 

         public abstract void onCheckedChanged(CompoundButton buttonView, boolean isChecked)

         在按钮选中状态发生改变时被调用

                   参数

                            buttonView     选中状态发生改变的那个按钮

                            isChecked        按钮新的状态

 

 

结束
 

  本章为CompoundButton的内部接口,内容较少,示例请参考CompoundButton的三个子类。
 

package com.example.apollo; import android.bluetooth.BluetoothAdapter; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.ButtonBarLayout; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; public class aaajisuanqi extends AppCompatActivity { private CheckBox checkBox, checkBox2, checkBox3,checkBox4; private EditText editText; private ImageButton imageButton; private String infor = ""; private TextView textView; private CompoundButton.OnCheckedChangeListener checkBoxListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.caidanbeixuan); checkBox=findViewById(R.id.cBx); checkBox2=findViewById(R.id.cB2); checkBox3=findViewById(R.id.cB3); checkBox4=findViewById(R.id.cB4); textView = findViewById(R.id.txV2); editText = findViewById(R.id.editTextText); imageButton=findViewById(R.id.imageButton); //设置监听器 class CheckBoxListener implements CompoundButton.OnCheckedChangeListener{ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { infor +=" " + buttonView.getText().toStr
最新发布
03-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值