重写一个横向的线性布局,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; } @Override public 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; } @Override public int getInputType() { return InputType.TYPE_CLASS_NUMBER; } }); views.add(tv); final int index = i; tv.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence chars, int arg1, int arg2, int arg3) { } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } @Override public 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() { @Override public 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()); } }