2016-09-06 59 views
0

我有四個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"/> 

回答

0

可能有幾個方法可以做到這一點,但我可能會刪除在編輯文本1文本限制,用文本觀察器管理長度。

在這裏,如果文本被粘貼到第一個edit1中,那麼文本觀察器會將值分割到其他編輯文本字段中。在更改afterTextChanged回調中的文本時,您需要非常小心,因爲更改將啓動對該方法的另一個調用。由於edit1中的文本長度只是我們處理後的文本長度,所以下一次回調不起作用。

public class TextGroup extends LinearLayout { 


    EditText edit0; 
    EditText edit1; 
    EditText edit2; 
    EditText edit3; 

    public TextGroup(Context context, AttributeSet attrs) { 
     super(context); 

     View view = LayoutInflater.from(context).inflate(R.layout.edit_text_special, null); 
     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 

     edit0 = (EditText) view.findViewById(R.id.edit_text0); 
     edit1 = (EditText) view.findViewById(R.id.edit_text1); 
     edit2 = (EditText) view.findViewById(R.id.edit_text2); 
     edit3 = (EditText) view.findViewById(R.id.edit_text3); 

     edit1.addTextChangedListener(watcher); 
     this.addView(view, lp); 
    } 

    TextWatcher watcher = new TextWatcher() { 

     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

     } 

     @Override 
     public void onTextChanged(CharSequence charSequence, int start, int count, int after) { 

     } 

     @Override 
     public void afterTextChanged(Editable editable) { 
     if (edit1.getText().length() >= 3) { 
      edit3.setText(String.valueOf(edit1.getText().toString().charAt(2))); 
     } 
     if (edit1.getText().length() >= 2) { 
      edit2.setText(String.valueOf(edit1.getText().toString().charAt(1))); 
      edit1.setText(String.valueOf(edit1.getText().toString().charAt(0))); 
     } 



     } 
    }; 

}