user3299430
user3299430

Reputation: 49

How to checkbox unchecked without action followed by checking?

I want to ask how to unchceck checkbox without action followed by checking checkbox*(i.e. w/o triggering the onCheckChangedListener())*? I have oncheck action, but i want to disable this action for unchecking. Is it possible ?

Here is my code:

 chb_czy_zamowic.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(
                        final CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub
                    final Dialog d1 = new Dialog(context);
                    d1.setContentView(R.layout.ilosc);
                    d1.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
                    d1.setTitle("Wybierz ilość");

                    final EditText et_Ilosc;
                    Button b_Ok;
                    Button b_Odejmij;
                    Button b_Dodaj;

                    if (buttonView.isChecked()) {

                        et_Ilosc = (EditText) d1
                                .findViewById(R.id.et_Ilosc);

                        et_Ilosc.setText(String.valueOf(i));



                        b_Dodaj = (Button) d1.findViewById(R.id.b_Dodaj);
                        b_Dodaj.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                String zmienna_pom = et_Ilosc.getText()
                                        .toString();
                                i = Integer.valueOf(zmienna_pom);
                                setI(i);
                                int k = getI();
                                k++;

                                setI(k);
                                et_Ilosc.setText(String.valueOf(i));

                            }
                        });
                        b_Odejmij = (Button) d1
                                .findViewById(R.id.b_Odejmij);
                        b_Odejmij.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                String zmienna_pom = et_Ilosc.getText()
                                        .toString();
                                i = Integer.valueOf(zmienna_pom);
                                setI(i);
                                i--;
                                setI(i);
                                et_Ilosc.setText(String.valueOf(i));

                            }
                        });
                        b_Ok = (Button) d1.findViewById(R.id.b_Ok);
                        b_Ok.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                String zmiennna_pom = et_Ilosc.getText()
                                        .toString();
                                int k = Integer.valueOf(zmiennna_pom);

                                if (k<=0 || k>=100) {

                                                                Toast t = Toast.makeText(getContext(),
                                            "Niepoprawna wartość" ,
                                            Toast.LENGTH_SHORT);
                                    t.show();
                                    view.chb_czy_zamowic.setChecked(false);
                                    d1.cancel();
                                }
                                else if( et_Ilosc.getText().toString().equals(""))
                                {
                                    Toast t = Toast.makeText(getContext(),
                                            "Uzupełnij pole ilość" ,
                                            Toast.LENGTH_SHORT);
                                    t.show();
                                    view.chb_czy_zamowic.setChecked(false);
                                    d1.cancel();
                                }
                                else
                                    view.chb_czy_zamowic.setEnabled(false);
                                    view.chb_czy_zamowic.setChecked(false); 


                                    items.get(position).Tow_ilosc -= k;
                                    Towar checkedObject = new Towar();
                                    checkedObject.Tow_ilosc = k;
                                    checkedObject.Kat_id = items
                                            .get(position).Kat_id;
                                    checkedObject.kategoria = items
                                            .get(position).kategoria;
                                    checkedObject.Tow_cena = items
                                            .get(position).Tow_cena;
                                    checkedObject.Tow_id = items
                                            .get(position).Tow_id;
                                    checkedObject.Tow_nazwa = items
                                            .get(position).Tow_nazwa;
                                    MainActivity.lista_wybranych_towarow
                                            .add(checkedObject);
                                    k = 0;
                                    setI(0);
                                    // et_Ilosc.setText("");
                                    if (MainActivity.lista_wybranych_towarow
                                            .size() > 0) {
                                        b_zatwierdz
                                                .setVisibility(View.VISIBLE);

                                        TowarZamowienie.towary_zamowione = new ArrayList<Towar>(MainActivity.lista_wybranych_towarow);

                                        Collections
                                                .copy(TowarZamowienie.towary_zamowione,
                                                        MainActivity.lista_wybranych_towarow);

                                    } else {
                                        Toast t = Toast.makeText(
                                                getContext(),
                                                "Proszę wybrać ilość",
                                                Toast.LENGTH_LONG);
                                        t.show();
                                    }



                                d1.dismiss();

                            }
                        });

                    }
                    ;
                    d1.show();

                }

            });

Upvotes: 1

Views: 122

Answers (2)

Audrius Meškauskas
Audrius Meškauskas

Reputation: 21748

Define some boolean field that you could set to true when your are tweaking the check states manually and false when you are not and expecting the user input instead. Now the listener can check the value of this field and ignore "software triggered checks" when the flag is true. The optimal location of the flag (method field, static field, etc) depends on the overall program design.

Be sure your code always sets this flag back to false when you finish tweaking the check boxes manually, so not to stay in the permanently locked state. This is one of the cases when finally statement may be appropriate.

If you only check the boxes programmatically on initialization, such cluttering may be avoided by setting the listeners only after the states are set.

Upvotes: 0

Atul O Holic
Atul O Holic

Reputation: 6792

Theres a property, setClickable associated with views which I believe can be used. In the onCheckedChangeListener, see when it gets unchecked and then set this to false. Hope this does the trick for you.

Upvotes: 1

Related Questions