Android EditText密碼的隱藏和顯示功能
實現效果圖:


實現代碼:
首先在xml里創建兩個控件 EditText和CheckBox
然后就很簡單了
dt1=(EditText)findViewById(R.id.password); cb1=(CheckBox)findViewById(R.id.checkbox_1); cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(cb1.isChecked()){ dt1.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); //如果被選中則顯示密碼 dt1.setSelection(dt1.getText().length()); //TextView默認光標在最左端,這里控制光標在最右端 }else { dt1.setTransformationMethod(PasswordTransformationMethod.getInstance()); //如果沒選中CheckBox則隱藏密碼 dt1.setSelection(dt1.getText().length()); } } ); 感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!如有疑問請留言或者到本站社區討論交流,共同進步!
新聞熱點
疑難解答