checkbox click event is not working in android 4.4

2019-09-10 08:47发布

问题:

CheckBox click event is not working in android 4.4.2

holder.addcheck.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

    Toast.makeText(getApplicationContext(), "Testing", Toast.LENGTH_SHORT).show();
                }
            });

回答1:

You can also use SetOnClickListener

addcheck= (CheckBox) findViewById(R.id.addcheck);

holder.addcheck.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {
            //is addcheckchecked?
    if (((CheckBox) v).isChecked()) {
        Toast.makeText(MyAndroidAppActivity.this,
           "Testing this checkbox", Toast.LENGTH_LONG).show();
    }

  }
});

And also you can use onCheckedChanged

check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

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

    Toast.makeText(MyAndroidAppActivity.this,
           "Testing this checkbox", Toast.LENGTH_LONG).show();

    }
});


回答2:

Try this;

checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

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

                Toast.makeText(getApplicationContext(), "Testing", Toast.LENGTH_SHORT).show();

            }
        });


回答3:

holder.addcheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                     Toast.makeText(getApplicationContext(), "Checked", Toast.LENGTH_SHORT).show();
                } else {
                     Toast.makeText(getApplicationContext(), "Unchecked", Toast.LENGTH_SHORT).show();
                }
}