我有四個EditText框共同組成一個值。每個盒子應該包含1個數字。當我將數字輸入到一個框中時,焦點應移至下一個框。當文本被改變時,我通過修改焦點來「僞裝」框之間的鏈接。下面的代碼可以工作,但我想讓用戶粘貼一些值,然後將這些值分割到EditText框中。因此,如果我在框[0]中粘貼「123」,框[0]應該包含「1」,框[1]應該包含「2」等。我嘗試將android:maxLength="1"
添加到XML,但是當我嘗試粘貼內容時, maxLength驗證刪除除第一個字符以外的所有字符。用於粘貼的鏈接EditText框
將4個EditText框中的粘貼內容分開的最佳方法是什麼?
EnterNumberLayout.java
public class EnterNumberLayout extends LinearLayout {
EditText[] textBoxes;
public static final int NUMBER_OF_ENTRIES = 4;
public EnterNumberLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.setOrientation(HORIZONTAL);
textBoxes = new EditText[NUMBER_OF_ENTRIES];
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < NUMBER_OF_ENTRIES; i++){
EditText et = (EditText) inflater.inflate(R.layout.number_box, null);
//et.setOnKeyListener(new BackspaceKeyListener(et));
et.addTextChangedListener(new MoveFocusWatcher(et));
et.setTag(i);
textBoxes[i] = et;
this.addView(et, i);
}
}
private class MoveFocusWatcher implements TextWatcher {
private View view;
public MoveFocusWatcher(View view) {
this.view = view;
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if ((int) this.view.getTag() < NUMBER_OF_ENTRIES - 1) {
(textBoxes[(int) this.view.getTag() + 1]).requestFocus();
}
}
public void afterTextChanged(Editable s) {}
}
}
number_box.xml
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number|none"
android:ellipsize="start"
android:gravity="center_horizontal|center_vertical"
android:imeOptions="actionNext"/>