复选框单击事件在android 4.4.2及更高版本中不起作用

时间:2014-09-04 06:14:45

标签: android checkbox click

CheckBox点击事件在android 4.4.2

中无效
holder.addcheck.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

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

3 个答案:

答案 0 :(得分:1)

您也可以使用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();
    }

  }
});

你也可以使用onCheckedChanged

check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

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

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

    }
});

答案 1 :(得分:1)

试试这个;

checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

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

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

            }
        });

答案 2 :(得分:0)

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();
                }
}