1

The following code is used to toggle the CheckBox in order to make the user able to see his password :

passwordCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

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

        if (!isChecked) {
            passwordEditText.setInputType(TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD);
            passwordConfirmEditText.setInputType(TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD);
        } else {
            passwordEditText.setInputType(TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            passwordConfirmEditText.setInputType(TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
    }
});

Here is what I understand from above: there's a CheckBox named passwordCheckBox, I'd set a listener to the CheckBox as soon as I click on it in order to (un)Toggle the CheckBox, if it's not Checked, passwordEditText won't appear as characters, if it's Checked password will appear as characters. If I am mistaken in what I assume, correct me please.

I don't understand the pattern of this code, How can the parameter "isChecked" which should be a new variable(?), be understood by the application where the isChecked is equal to "True" (And at the same time it's understood as it's the user input)

Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
hellohello
  • 65
  • 7
  • 2
    `isChecked` is an argument which will be provided by OS when check listener is fired, is it any clearer? – Oleg Bogdanov Dec 31 '16 at 21:22
  • Could you provide me with a link with source(or explanation) so i could understand any better? – hellohello Dec 31 '16 at 21:23
  • Im not sure im following your question. is its the construct `!isChecked` that you don't understand? its just a short form of `isChecked != true` – Oleg Bogdanov Dec 31 '16 at 21:26
  • What i don't understand is this, i.e: private void example(int a, int b) { // TODO } a, and b are both variables inside the parameters, then how could i use it to know what was the user inputs? similarly, isChecked is the same as (In this case) a, and b so i can't check how did the user toggle the CheckBox or not, so how could i use it in this code and it worked just fine? That's why i am asking for explanation about the pattern of isChecked as variable in parameter – hellohello Dec 31 '16 at 21:33
  • 1
    sorry I still dont undertand :) when user interacts with check box (ui element) Android (an OS) will call your code back with the currect state of the check box, argument `isChecked` is set to current state and passed to you – Oleg Bogdanov Dec 31 '16 at 21:36
  • Link with source, lines 153, 156: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/widget/CompoundButton.java#CompoundButton.setChecked%28boolean%29. – Andy Res Dec 31 '16 at 21:38
  • @hellohello That construct is called an anonymous class. OnCheckedChanged is not called by your code, it's called by Android internal code only when the user interacts with the checkbox. – Christian Strempfer Dec 31 '16 at 21:40
  • Thanks for the comments everyone, i now understand more, i'll look into the code that was linked above to understand even more. Thanks – hellohello Dec 31 '16 at 21:48

1 Answers1

1

This is the answer: @hellohello That construct is called an anonymous class. OnCheckedChanged is not called by your code, it's called by Android internal code only when the user interacts with the checkbox. – Christian Strempfer

All thanks for other comments

hellohello
  • 65
  • 7