您的位置:首页 > 移动开发 > Android开发

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



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息