CompoundButton.OnCheckedChangeListener与RadioGroup.OnCheckedChangeListener冲突

本文探讨了在Android开发中,当RadioGroup与CompoundButton同时存在且需要监听事件时,如何避免OnCheckedChangeListener之间的冲突。通过在事件中加入具体名称的方法解决了问题。

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

<span style="font-size:18px;">当RadioGroup与CompoundButton同时存在且都要监听事件时CompoundButton.OnCheckedChangeListener
与RadioGroup.OnCheckedChangeListener就会出现冲突,
导入任何一个包import android.widget.RadioGroup.OnCheckedChangeListener;会出现如下错误:
 
The method setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener)in the type 
RadioGroupisnot applicable for the arguments (newCompoundButton.OnCheckedChangeListener(){})
 
或import android.widget.RadioGroup.OnCheckedChangeListener collides with another import statement
 
又不能同时导入,因此又要使用两者,网上给出个方法:
 
不要导入任何事件,在事件中加入具体名,红色部分:
 
howHotRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()...
 
friendlyCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()...</span>

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
03-13
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、付费专栏及课程。

余额充值