类似支付宝的6个密码输入框
来源:互联网 发布:深圳知路科技有限公司 编辑:程序博客网 时间:2024/05/22 00:14
重写一个横向的线性布局,6个输入框,平均每个权重1
设置密码输入属性:
tv.setInputType(InputType.TYPE_CLASS_TEXT| InputType.TYPE_TEXT_VARIATION_PASSWORD);
tv.setKeyListener(new NumberKeyListener() {protected char[] getAcceptedChars() {char[] numberChars = { '1', '2', '3', '4', '5', '6', '7','8', '9', '0' };return numberChars;}@Overridepublic int getInputType() {return InputType.TYPE_CLASS_NUMBER;}});输入或删除后,让他调到下一个edittext即可。
import java.util.ArrayList;import java.util.List;import android.annotation.SuppressLint;import android.content.Context;import android.text.Editable;import android.text.InputType;import android.text.TextWatcher;import android.text.method.NumberKeyListener;import android.util.AttributeSet;import android.view.Gravity;import android.view.KeyEvent;import android.view.View;import android.widget.EditText;import android.widget.LinearLayout;@SuppressLint("NewApi")public class PassworldTextView extends LinearLayout {private Context mcontext;private static final int PWDLENGTH = 6;// 密码长度private static List<EditText> views;public PassworldTextView(Context paramContext) {super(paramContext);init(paramContext);}public PassworldTextView(Context paramContext,AttributeSet paramAttributeSet) {super(paramContext, paramAttributeSet);init(paramContext);}public PassworldTextView(Context paramContext,AttributeSet paramAttributeSet, int paramInt) {super(paramContext, paramAttributeSet, paramInt);init(paramContext);}public String getText() { StringBuffer sb = new StringBuffer(); for (EditText edt : views) { <span style="white-space:pre"></span>sb.append(edt.getText().toString()); } return sb.toString();}private void init(Context context) {mcontext = context;this.setOrientation(LinearLayout.HORIZONTAL);removeAllViews();views = new ArrayList<EditText>();for (int i = 0; i < PWDLENGTH; i++) {EditText tv = new EditText(mcontext);tv.setInputType(InputType.TYPE_CLASS_TEXT| InputType.TYPE_TEXT_VARIATION_PASSWORD);// tv.setInputType(InputType.TYPE_CLASS_NUMBER// | InputType.TYPE_TEXT_VARIATION_PASSWORD);tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT, 1));tv.setGravity(Gravity.CENTER);tv.setBackgroundResource(R.drawable.bg_edittext_normal);// tv.setKeyListener(new// // DigitsKeyListener(sign, decimal));// DigitsKeyListener(false, true));tv.setKeyListener(new NumberKeyListener() {protected char[] getAcceptedChars() {char[] numberChars = { '1', '2', '3', '4', '5', '6', '7','8', '9', '0' };return numberChars;}@Overridepublic int getInputType() {return InputType.TYPE_CLASS_NUMBER;}});views.add(tv);final int index = i;tv.addTextChangedListener(new TextWatcher() {@Overridepublic void onTextChanged(CharSequence chars, int arg1,int arg2, int arg3) {}@Overridepublic void beforeTextChanged(CharSequence arg0, int arg1,int arg2, int arg3) {}@Overridepublic void afterTextChanged(Editable editable) {if (editable.toString().equals("")) {// 删完,光标左移if (index > 0) {EditText edt = views.get(index - 1);reSet(edt);}} else if (editable.length() == 1) {// 写好一个,往后移if (editable.length() == 1 && index < views.size() - 1) {EditText edt = views.get(index + 1);// edt.setText("*");reSet(edt);}} else {EditText edt = null;for (int j = 0; j < editable.length(); j++) {if ((j + index) < views.size()) {// j!=0 &&edt = views.get(index + j);edt.setText(editable.subSequence(j, j + 1));}}if (edt != null) {reSet(edt);}}}});tv.setOnKeyListener(new OnKeyListener() {@Overridepublic boolean onKey(View arg0, int key, KeyEvent arg2) {EditText edt = (EditText) arg0;if (edt.getText().length() == 0 && index > 0) {edt = views.get(index - 1);reSet(edt);}return false;}});this.addView(tv);}}private void reSet(EditText edt) {edt.setFocusable(true);edt.requestFocus();edt.setSelection(edt.length());}}
0 0
- 类似支付宝的6个密码输入框
- 类似支付宝的密码输入框
- GridPasswordView 类似支付宝支付密码输入框
- 带格子的密码输入框,类似微信,支付宝支付时候的输入密码页面效果
- Android自定义类似支付宝密码输入的控件
- Android自定义类似支付宝密码输入的控件
- 类似解锁或者支付密码输入框
- android中6个EditText输入验证码(仿支付宝支付输入密码框)
- 类似支付宝支付密码的edittext
- 类似支付宝或微信支付输入密码
- iOS开发进阶 - 实现类似微信和支付宝的密码输入框(UIKeyInput协议)
- 支付的密码输入框
- iOS简单的密码输入组件XLPasswordView,UI类似支付宝
- 仿支付宝支付密码输入框
- Android 仿支付宝密码框的EditText输入框
- 仿微信/支付宝的密码输入框效果 android
- 仿微信/支付宝的密码输入框效果 android
- 仿微信/支付宝的密码输入框效果 android
- ios 制作自已的framework
- Swift笔记
- Pat 1040
- DOM对象和JQuery对象的区别
- 【设计模式学习笔记十七】【行为模式】【迭代器模式(Interpreter)】
- 类似支付宝的6个密码输入框
- centos 6.5 无线网卡配置(hp mini)
- 算法训练 最大最小公倍数 简单贪心算法
- android 多类型listview
- centos安装nginx配置webpy
- ubuntu下eclipse配置python环境
- 黑马程序员 JAVA基础---API常用类
- poj1006(中国剩余定理)
- Daemon and service