欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中EditText显示明文与密码的两种方式

程序员文章站 2024-03-05 18:27:25
效果图如下所述: 布局

效果图如下所述:

Android中EditText显示明文与密码的两种方式

布局

<?xml version="1.0" encoding="utf-8"?>
<linearlayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingbottom="@dimen/activity_vertical_margin"
android:paddingleft="@dimen/activity_horizontal_margin"
android:paddingright="@dimen/activity_horizontal_margin"
android:paddingtop="@dimen/activity_vertical_margin"
tools:context="liu.basedemo.mainactivity">
<edittext
android:id="@+id/etusername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="请输入用户名"
android:textcolor="#000000"
android:textcolorhint="#55000000"
android:textsize="20sp"/>
<relativelayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<edittext
android:id="@+id/etpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="请输入密码"
android:inputtype="textpassword"
android:textcolor="#000000"
android:textcolorhint="#55000000"
android:textsize="20sp"/>
<checkbox
android:checked="false"
android:id="@+id/cbdisplaypassword"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignparentright="true"
android:layout_centervertical="true"
android:button="@drawable/selector_password"/>
</relativelayout>
</linearlayout>
selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/cb_checked" android:state_checked="true"/>
<item android:drawable="@mipmap/cb_normaled" android:state_checked="false"/>
</selector>

edittext显示明文与密码的两种方式如下所述:

第一种方式

private void initlistener() {
mcbdisplaypassword.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {
@override
public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {
log.d(tag, "oncheckedchanged: "+ischecked);
if(ischecked){
//选择状态 显示明文--设置为可见的密码
metpassword.setinputtype(inputtype.type_text_variation_visible_password);
}else {
//默认状态显示密码--设置文本 要一起写才能起作用 inputtype.type_class_text | inputtype.type_text_variation_password
metpassword.setinputtype(inputtype.type_class_text | inputtype.type_text_variation_password);
}
}
});
}

第二种方式

private void initlistener() {
mcbdisplaypassword.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {
@override
public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {
log.d(tag, "oncheckedchanged: "+ischecked);
if(ischecked){
//选择状态 显示明文--设置为可见的密码
//metpassword.setinputtype(inputtype.type_text_variation_visible_password);
/**
* 第二种
*/
metpassword.settransformationmethod(hidereturnstransformationmethod.getinstance());
}else {
//默认状态显示密码--设置文本 要一起写才能起作用 inputtype.type_class_text | inputtype.type_text_variation_password
//metpassword.setinputtype(inputtype.type_class_text | inputtype.type_text_variation_password);
/**
* 第二种
*/
metpassword.settransformationmethod(passwordtransformationmethod.getinstance());
}
}
});
}

以上所述是小编给大家介绍的android中edittext显示明文与密码的两种方式,希望对大家有所帮助,如果大家想了解更多内容敬请关注!