直接上代码
private EditText edt; private CheckBox btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edt = (EditText) findViewById(R.id.edt); btn = (CheckBox) findViewById(R.id.btn); btn.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if (isChecked) { // 设置EditText文本为可见的 edt.setTransformationMethod(HideReturnsTransformationMethod .getInstance()); } else { // 设置EditText文本为隐藏的 edt.setTransformationMethod(PasswordTransformationMethod .getInstance()); } edt.postInvalidate(); // 切换后将EditText光标置于末尾 CharSequence charSequence = edt.getText(); if (charSequence instanceof Spannable) { Spannable spanText = (Spannable) charSequence; Selection.setSelection(spanText, charSequence.length()); } } }); }