菜鸟在android中密码框的纠结过程

密码框的问题总算解决了,将过程记下来算是笔记吧!

问题:密码框配合显示隐藏按钮,点击显示或隐藏已经输入的密码

布局xml,


 ……

<EditText
    android:id="@+id/txt_password"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:background="@null"
    android:textColor="#545454"
    android:singleLine="true"
    android:password="true"    
    android:paddingLeft="5dp"
    android:maxLength="16"
    android:hint="@string/enter_pass"/>
         
<Button
    android:id="@id/bt_showPass"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@null"
    android:gravity="left|center"
    android:layout_marginRight="20dp"
    android:text="@string/show_pass"
    android:textColor="@color/show_button"
    android:clickable="true"
    android:onClick="showPass"
    android:textSize="18sp" />

……



方案一: 使用setInputType()的方式来处理,如下:



private boolean mbDisplayFlg = false;

public void showPass(View view){
	EditText txtPass = (EditText)findViewById(R.id.txt_password);
	TextView btPass = (TextView)view;
	if(mbDisplayFlg){
		txtPass.setInputType(0x90);
		btPass.setText(R.string.hide_pass);
	}else{
		txtPass.setInputType(0x81);
		btPass.setText(R.string.show_pass);
	}
	mbDisplayFlg = !mbDisplayFlg;  
}
	

发现问题:输入密码时,在隐藏密码状态下,输入无误,在显示密码状态下,竟然可以输入中文,可怕的中文。于是想到了第二套方案,上度娘一查,很多网友都在使用这种方案.


方案二:使用setTransformationMethod()方法来处理,代码如下:



private boolean mbDisplayFlg = false;

public void showPass(View view){
	EditText txtPass = (EditText)findViewById(R.id.txt_password);
	TextView btPass = (TextView)view;
	if (!mbDisplayFlg) {  
		txtPass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());            	
		btPass.setText(R.string.hide_pass);
	} else {  
		txtPass.setTransformationMethod(PasswordTransformationMethod.getInstance());  
		btPass.setText(R.string.show_pass);
	}
	mbDisplayFlg = !mbDisplayFlg; 
}


使用之后很完美,唯一感觉不爽的问题就是被点击按钮后,光标都跑到EditText的最前边去了,想控制光标的位置:

解决办法是在处理函数中,处理完显示后加上一个光标定位的语句,如下:

 txtPass.setSelection(newPass.getText().length());

这回感觉满意了!

对于光标控制,是通过EditText自带的函数setSelection()来操作的,它调用的是android.text.Selection包提供的方法来进行操作的!此工具类还提供了一些别的好用的方法,简单说明一下:



final static int  getSelectionEnd(CharSequence text)
Return the offset of the selection edge or cursor, or -1 if there is no selection or cursor.

final static int  getSelectionStart(CharSequence text)
Return the offset of the selection anchor or cursor, or -1 if there is no selection or cursor.

final static void  removeSelection(Spannable text)
Remove the selection or cursor, if any, from the text.

final static void  selectAll(Spannable text)
Select the entire text.

final static void  setSelection(Spannable text, int index)
Move the cursor to offset index.

static void  setSelection(Spannable text, int start, int stop)
Set the selection anchor to start and the selection edge to stop.

所以也可以直接调用Selection类的方法进行光标的定位,如下:

Editable ea = txtPass.getText();
Selection.setSelection(ea, ea.length());

OK,总结完毕,回家吃饭!




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值