Android开发:CompoundButton.onCheckedChangeListener和RadioGroup.onCheckedChangeListener冲突问题

本文介绍了解决ToggleButton和RadioGroup监听事件冲突的方法,通过直接指定事件名称而非导入监听接口,成功避免了两者之间的冲突。

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

在今天的开发工作当中,要同时用到ToggleButton和RadioGroup的监听事件,ToggleButton的监听事件需要导入CompoundButton.onCheckedChangeListener,RadioGroup的监听事件需要导入RadioGroup.onCheckedChangeListener,但是这两个导入是冲突的,而且这两个事件是必须用到的。怎么办呢?不要导入任何事件,在给两个控件设置监听器的时候加入事件的具体名字。如下:

//设置RadioGroup监听事件
rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
    Toast.makeText(getBaseContext(), tipString, Toast.LENGTH_SHORT).show();
}
});


//设置ToggleButton监听事件
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
    //.....这里是具体实现的功能代码
}
});

这样问题就解决了。(Switch对象的监听事件和ToggleButton的是一样的,也是用到CompoundButton.onCheckedChangeListener)

06-10 14:27:52.220 16648 16648 W System.err: java.io.FileNotFoundException: /sys/devices/platform/fe8a0000.usb2-phy/otg_mode: open failed: EACCES (Permission denied) 06-10 14:27:52.221 16648 16648 W System.err: at libcore.io.IoBridge.open(IoBridge.java:492) 06-10 14:27:52.221 16648 16648 W System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:236) 06-10 14:27:52.222 16648 16648 W System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:125) 06-10 14:27:52.222 16648 16648 W System.err: at com.kgzn.developer.dialog.CommonToolHolder.switchOTG(CommonToolHolder.java:36) 06-10 14:27:52.222 16648 16648 W System.err: at com.kgzn.developer.dialog.SwitchDialog$16.onClick(SwitchDialog.java:292) 06-10 14:27:52.222 16648 16648 W System.err: at android.view.View.performClick(View.java:7448) 06-10 14:27:52.223 16648 16648 W System.err: at android.widget.CompoundButton.performClick(CompoundButton.java:144) 06-10 14:27:52.223 16648 16648 W System.err: at android.view.View.performClickInternal(View.java:7425) 06-10 14:27:52.223 16648 16648 W System.err: at android.view.View.access$3600(View.java:810) 06-10 14:27:52.223 16648 16648 W System.err: at android.view.View$PerformClick.run(View.java:28305) 06-10 14:27:52.223 16648 16648 W System.err: at android.os.Handler.handleCallback(Handler.java:938) 06-10 14:27:52.224 16648 16648 W System.err: at android.os.Handler.dispatchMessage(Handler.java:99) 06-10 14:27:52.224 16648 16648 W System.err: at android.os.Looper.loop(Looper.java:223) 06-10 14:27:52.224 16648 16648 W System.err: at android.app.ActivityThread.main(ActivityThread.java:7660) 06-10 14:27:52.224 16648 16648 W System.err: at java.lang.reflect.Method.invoke(Native Method) 06-10 14:27:52.225 16648 16648 W System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 06-10 14:27:52.226 16648 16648 W System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 06-10 14:27:52.227 16648 16648 W System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied) 06-10 14:27:52.227 16648 16648 W System.err: at libcore.io.Linux.open(Native Method) 06-10 14:27:52.227 16648 16648 W System.err: at libcore.io.ForwardingOs.open(ForwardingOs.java:166) 06-10 14:27:52.227 16648 16648 W System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254) 06-10 14:27:52.228 16648 16648 W System.err: at libcore.io.ForwardingOs.open(ForwardingOs.java:166) 06-10 14:27:52.228 16648 16648 W System.err: at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7546) 06-10 14:27:52.228 16648 16648 W System.err: at libcore.io.IoBridge.open(IoBridge.java:478)
最新发布
06-11
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.Checkable; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.RadioButton; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1 = (Button)findViewById(R.id.b1); t1 = (TextView)findViewById(R.id.t1); t2 = (TextView)findViewById(R.id.t2); t3 = (TextView)findViewById(R.id.t3); t4 = (TextView)findViewById(R.id.t4); c1 = (CheckBox)findViewById(R.id.c1); c2 = (CheckBox)findViewById(R.id.c2); r1 = (RadioButton)findViewById(R.id.r1); r2 = (RadioButton)findViewById(R.id.r2); c1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { t2.setText("C1 已被选中"); } else { t2.setText("C1 未被选中"); } } }); c2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { t3.setText("C2 已被选中"); } else { t3.setText("C2 未被选中"); } } }); r1.setOnCheckedChangeListener((group, checkedId) -> { switch (checkedId) { case R.id.r1: t4.setText("You selected Option 1"); break; 修改
04-03
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、付费专栏及课程。

余额充值