Android: 复选框监听器

我想把一个监听器放在一个 CheckBox。我寻找信息,它是这样的:

satView = (CheckBox)findViewById(R.id.sateliteCheckBox);


satView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (isChecked){
// perform logic
}
}


});

问题是 Eclipse 认为它是 RadioGroupOnCheckedChangeListener,但是它不工作。我该怎么补救?

246060 次浏览

你可以这样做:

satView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {


@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {


}
}
);

CompoundButton buttonView更改 RadioGroup group,然后按 Ctrl + Shift + O 修复导入。

试试这个

satView.setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (isChecked){
// perform logic
}
}


});

试试这个:

satView = (CheckBox) findViewById(R.id.sateliteCheckBox);


satView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isChecked()) {
// checked
}
else
{
// not checked
}
}


});

希望这个能帮上忙。

你也可以选择简单的 View.OnClickListener:

satView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(((CompoundButton) view).isChecked()){
System.out.println("Checked");
} else {
System.out.println("Un-Checked");
}
}
});

您得到了错误,因为您导入了错误的包。您应该 import android.widget.CompoundButton.OnCheckedChangeListener;

所以回调应该是:

checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
// Perform logic
}
});

你可以用这个密码。

public class MySampleActivity extends Activity {
CheckBox cb1, cb2, cb3, cb4;
LinearLayout l1, l2, l3, l4;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
cb4 = (CheckBox) findViewById(R.id.cb4);
l1 = (LinearLayout) findViewById(R.id.l1);
l2 = (LinearLayout) findViewById(R.id.l2);
l3 = (LinearLayout) findViewById(R.id.l3);
l4 = (LinearLayout) findViewById(R.id.l4);
}


@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(1));
cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(2));
cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(3));
cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(4));
}


public class MyCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
int position;


public MyCheckedChangeListener(int position) {
this.position = position;
}


private void changeVisibility(LinearLayout layout, boolean isChecked) {
if (isChecked) {
l1.setVisibility(View.VISIBLE);
} else {
l1.setVisibility(View.GONE);
}


}


@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (position) {
case 1:
changeVisibility(l1, isChecked);
break;
case 2:
changeVisibility(l2, isChecked);
break;
case 3:
changeVisibility(l3, isChecked);
break;
case 4:
changeVisibility(l4, isChecked);
break;
}
}
}
}
h.chk.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
CheckBox chk=(CheckBox)view; // important line and code work
if(chk.isChecked())
{
Message.message(a,"Clicked at"+position);
}
else
{
Message.message(a,"UnClick");
}
}
});

将克里斯的回答翻译成 Kotlin 语:

val checkBox: CheckBox = findViewById(R.id.chk)
checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
// Code here
}

如果您希望在 Kotlin 使用接口实现来实现这一点,请使用。

class MainActivity: AppCompatActivity(),CompoundButton.OnCheckedChangeListener{


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)


val yourCheckBox = findViewById<CheckBox>(R.id.check_box)
yourCheckBox.setOnCheckedChangeListener(this)


}


override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
when(buttonView?.id){
R.id.check_box -> Log.d("Checked: ","$isChecked")
}
}


}