Android EditText密碼的隱藏和顯示功能
實(shí)現(xiàn)效果圖:


實(shí)現(xiàn)代碼:
首先在xml里創(chuàng)建兩個(gè)控件 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默認(rèn)光標(biāo)在最左端,這里控制光標(biāo)在最右端 }else { dt1.setTransformationMethod(PasswordTransformationMethod.getInstance()); //如果沒選中CheckBox則隱藏密碼 dt1.setSelection(dt1.getText().length()); } } );
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!如有疑問請(qǐng)留言或者到本站社區(qū)討論交流,共同進(jìn)步!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注