Search code examples
javaandroidlisteneroncheckedchanged

I don't understand the pattern of CheckBox click with the new parameter


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)


Solution

  • 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