如何在取消选中复选框时传递值

时间:2015-10-20 12:37:40

标签: android checkbox

我有3个复选框..我为每个复选框添加了点击监听器..基于复选框单击我已设置单击监听器内的值...添加复选框单击后,将有可能在更新复选框时取消选中它...

点击我设置的复选框isSelected = 1 ...就像我需要设置的方式isSelected = 0而取消选中...怎么可能..请帮我找出

这是我的复选框,单击listener1

checkBox_onEventDay.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                if (((CheckBox) v).isChecked()) {
                    response = 1;
                    try {
                        //Here we are getting the date from btn_Date(date picker)
                        //date and time format changed here
                        String eventDate = btn_Date.getText().toString();
                        DateFormat date = new SimpleDateFormat("dd-M-yyyy");
                        Date date1 = date.parse(eventDate);
                        DateFormat convertDate = new SimpleDateFormat(" dd.MM.yyyy");
                        eventDate = convertDate.format(date1);
                        eventMO.setEventDate(eventDate);
                        //here we are setting event date as reminder date..
                        //Because it is on event day checkbox
                        reminderDate = eventDate;
                        eventReminderDaysDetails(response, reminderDate);
                        Toast.makeText(OccasionActivity.this,
                                "Checked", Toast.LENGTH_LONG).show();


                    } catch (ParseException pExp) {
                        pExp.printStackTrace();
                    }
                }

            }
        });

2 个答案:

答案 0 :(得分:2)

我猜你开始时有一个未经检查的复选框。如果没有,您可以相应地初始化isSelected的值。您需要像这样处理onClick

final boolean isSelected = false;
        checkBox_onEventDay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isSelected) {
                    isSelected = false;
                    // now check box is unchecked
                } else {
                    isSelected = true;
                    // now the checkbox is checked
                }
            }
        }); 

答案 1 :(得分:1)

您可以使用以下代码:

  if(cb.isChecked()) {
    isSelected  = 1 ;
    Log.e("Checkbox is checked" , ""+isSelected);
  } else {
    isSelected  = 0 ;
    Log.e("Checkbox is unchecked" , ""+isSelected);
  }

就这么简单。