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

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

布局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,总结完毕,回家吃饭!




更多相关文章

  1. Eclipse 导入安卓项目 No projects are found to import解决方案
  2. Android(安卓)VNDK限制下的解决方案
  3. unity3d引用android第三方sdk
  4. Android(安卓)Studio的快捷键
  5. Android(安卓)实现密码键盘的相关知识点
  6. apk签名不一致终极解决方案
  7. Android(安卓)源码分析实战 - 授权时拦截 QQ 用户名和密码
  8. Android实现几种推送方式解决方案
  9. Android中实现「类方法指令抽取方式」加固方案原理解析

随机推荐

  1. 在Android上跑TensorFlow之接入TensorFlo
  2. Android 如何将定制的Launcher成为系统中
  3. android探索之UID u0_axxx的由来
  4. [转]Android xmlns 的作用及其自定义
  5. 浅析 Android 生命周期(一)
  6. Android(三)显示控件使用
  7. ObjectHttp功能介绍篇
  8. Android Canvas练习(4)自已绘折线图
  9. android以后台service的方式获取GPRS数据
  10. Android问答