简体   繁体   中英

EditText input type text password not changing

I want to change the input type of an edit text to visible when a switch is checked.

Switch a =(Switch) findViewById(R.id.switch1);
    a.setText("visible");
    a.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                EditText pass = (EditText) findViewById(R.id.pass);
                pass.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            }else{      
                EditText pass = (EditText) findViewById(R.id.pass);
                pass.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
            }
        }
    });

It works when I turn on the switch and makes the password visible but when I turn it off it remains visible and doesn't change.

pass.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)

Try this in your else statement for showing the password type variation. The reason is because it needs to specify that the input class is of the type text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM