让密码“ 可见”

         一个关于password 的细节,就是在登陆的时候,如果用户名、密码都OK,我们点击提交或者登陆就万事大吉了!但有时候我们会遇到“你的用户名或者密码有误”的情况,相当郁闷啊,要重新输入一次。如果输入的密码“可见”,可以少做这种无用功,用户体验也会好些!

很简单的代码:

<RelativeLayout 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" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentTop="true"
        android:layout_marginTop="33dp"
        android:text="@string/userName" />

    <EditText
        android:id="@+id/etPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="33dp"
        android:ems="10"
        android:inputType="textPassword" >

        <requestFocus />
    </EditText>

    <CheckBox
        android:id="@+id/cbShowPwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/etPassword"
        android:layout_below="@+id/etPassword"
        android:layout_marginTop="20dp"
        android:text="@string/show_pwd" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="20dp"
        android:text="@string/pwd"
        tools:context=".MainActivity" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/etPassword"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="15dp"
        android:ems="10" />

</RelativeLayout>

public class MainActivity extends Activity {

	private EditText pwd;
	private CheckBox checkPwd;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		pwd=(EditText) findViewById(R.id.etPassword);
	    checkPwd=(CheckBox) findViewById(R.id.cbShowPwd);
	    
	    checkPwd.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
			{
				 if (!isChecked) {
                     // show password
             	pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
             } else {
                     // hide password
             	pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
             }
				
			}
		});
		
	}
效果图示:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值