Android开发之密码明文密文切换显示

时间:2022-09-09 16:24:20

Android开发之密码明文密文切换显示

Android开发之密码明文密文切换显示


.xml文件
<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"
    tools:context=".MainActivity" 
    android:orientation="vertical">

    <TextView
        android:id="@+id/msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用户密码" />
    <EditText 
        android:id="@+id/edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:password="true"/>
    
    <CheckBox 
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示密码"/>

</LinearLayout>

.java文件
public class MainActivity extends Activity {
private EditText passwd=null;
private CheckBox show=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.passwd=(EditText) super.findViewById(R.id.edit);
this.show=(CheckBox) super.findViewById(R.id.show);
this.show.setOnClickListener(new OnclickListenerImp());
}

private class OnclickListenerImp implements OnClickListener{

public void onClick(View v) {
if(MainActivity.this.show.isChecked()){
//设置为明文显示
MainActivity.this.passwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
else{
//设置为秘闻显示
MainActivity.this.passwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
}

}