Android自定义密码输入框
2017-04-12 14:24
267 查看
效果图如图所示:
实现原理是:自定义一个类PassWordInputView继承FramLayout。将布局文件所获取的View添加到这个自定义View中,布局文件是由一个EditText控件和6个TextView组成还有若干个View(边框)组成,EditText主要是用于获取Focus,以及对文本的监听,TextView用于对输入密码的显示。下面看看代码的实现:
1.设置此控件的参数
post(new Runnable() { @Override public void run() { ViewGroup.LayoutParams params = getLayoutParams(); if (params != null) { params.height = (getWidth() - getPaddingLeft() - getPaddingRight()) / 6 + getPaddingBottom() + getPaddingTop(); params.width = getWidth() + getPaddingLeft() + getPaddingRight(); setLayoutParams(params); } } });2.获取和去除焦点
public void onFocus() { //获取焦点 if (listener != null) { listener.onFocusChange(this, true); } contentView.setBackgroundResource(contentOnFocusId); line1.setBackgroundColor(onFocusColor); line2.setBackgroundColor(onFocusColor); line3.setBackgroundColor(onFocusColor); line4.setBackgroundColor(onFocusColor); line5.setBackgroundColor(onFocusColor); edit_pwd.requestFocus(); edit_pwd.setEnabled(true); imm.showSoftInput(edit_pwd, 0); }
3.布局文件
<EditText android:id="@+id/pwd_edit" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="3dp" android:background="@null" android:cursorVisible="false" android:enabled="false" android:inputType="number" android:maxLength="6" android:textColor="@android:color/transparent" /> <LinearLayout android:id="@+id/content_view" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/password_input_nomal_shape" android:clickable="true" android:orientation="horizontal" > <TextView android:id="@+id/pwd1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:maxLength="1" android:singleLine="true" android:textColor="#333333" android:textSize="30sp" /> <View android:id="@+id/line1" android:layout_width="1dp" android:layout_height="match_parent" android:background="#d9d9d9" /> <TextView android:id="@+id/pwd2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:maxLength="1" android:singleLine="true" android:textColor="#333333" android:textSize="30sp" /> <View android:id="@+id/line2" android:layout_width="1dp" android:layout_height="match_parent" android:background="#d9d9d9" /> <TextView android:id="@+id/pwd3" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:maxLength="1" android:singleLine="true" android:textColor="#333333" android:textSize="30sp" /> <View android:id="@+id/line3" android:layout_width="1dp" android:layout_height="match_parent" android:background="#d9d9d9" /> <TextView android:id="@+id/pwd4" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:maxLength="1" android:singleLine="true" android:textColor="#333333" android:textSize="30sp" /> <View android:id="@+id/line4" android:layout_width="1dp" android:layout_height="match_parent" android:background="#d9d9d9" /> <TextView android:id="@+id/pwd5" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:maxLength="1" android:singleLine="true" android:textColor="#333333" android:textSize="30sp" /> <View android:id="@+id/line5" android:layout_width="1dp" android:layout_height="match_parent" android:background="#d9d9d9" /> <TextView android:id="@+id/pwd6" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:maxLength="1" android:singleLine="true" android:textColor="#333333" android:textSize="30sp" /> </LinearLayout>
源码下载地址:http://download.csdn.net/detail/qq_17470165/9811706
相关文章推荐
- Android自定义View模仿密码输入框
- Android仿支付宝淘宝 - 自定义密码输入框和键盘
- Android自定义view,PasswordView,交易密码输入框
- Android仿支付宝淘宝 - 自定义密码输入框和键盘
- Android 自定义密码输入框实现代码
- Android自定义密码输入框——(仿支付宝等)
- Android仿支付宝自定义密码输入框及安全键盘(密码键盘)
- Android仿支付宝淘宝 - 自定义密码输入框和键盘
- Android 重写EditText自定义密码输入框
- Android自定义密码输入框和数字键盘
- Android自定义View之密码输入框
- android 仿支付宝密码输入框
- 【Android源码解析】 自定义可清除的输入框
- android自定义密码键盘
- 学习笔记-自定义密码输入框和自定义数字密码软键盘
- Android 验证码/密码 输入框
- Android登录界面账号密码输入框选中时,选中背景变换的实现方法
- Android完美解决输入框EditText隐藏密码打勾显示密码问题
- android仿支付宝密码输入框效果
- Android自定义UI手势密码改进版