安卓学习笔记-----监听EditText的内容变化

TextWatcher,实现该类需实现三个方法:

1.内容变化前

public void beforeTextChanged(CharSequence s, int start,int count, int after);   

2.内容变化中

public void onTextChanged(CharSequence s, int start, int before, int count);

3.内容变化后

public void afterTextChanged(Editable s);
实现EditText的密码可见与不可见
public class MainActivity extends AppCompatActivity {

    private EditText edit_pawd;
    private Button btnChange;
    private boolean flag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edit_pawd = (EditText) findViewById(R.id.edit_pawd);
        btnChange = (Button) findViewById(R.id.btnChange);
        edit_pawd.setHorizontallyScrolling(true);    //设置EditText不换行
        btnChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(flag == true){
                    edit_pawd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    flag = false;
                    btnChange.setText("密码不可见");
                }else{
                    edit_pawd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    flag = true;
                    btnChange.setText("密码可见");
                }
            }
        });
    }
}

你可能感兴趣的:(安卓学习笔记-----监听EditText的内容变化)