2017-06-02 39 views
0

所以,我希望用戶輸入他/她的車的他/她的車牌號,我希望它是通過分離所有字符喜歡這幅畫相當時尚:enter image description here文字跨越了多個EditText上,Android的

現在它由6個帶TextWatchers的EditTexts組成,它們可以改變每個輸入的焦點。這部分工作正常,但我的問題是刪除och字符。 當用戶想要編輯一個字段時,他/她只能點擊視圖並刪除eand替換。雖然整件事情都是錯誤的,但不可能一次刪除所有內容,但必須單擊每個視圖並手動刪除。

所以我需要的是,當用戶在空視圖上點擊退格時,它應該將焦點改變爲之前的那個並刪除該字符等等。因此,所有的EditTexts都將被連接起來並作爲一個整體工作。我已經嘗試了用於監聽退格鍵的KeyListeners,但只能用於硬件鍵盤而不適用於手機上的軟鍵盤。

我也很高興,如果有人能指出我比另一個解決方案更好的方向。

TextWatcher:

registrationPlateEditTexts是所有的EditText的爲了一個列表。

private TextWatcher regPlateTextWatcher = new TextWatcher() { 
     @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 
     @Override public void onTextChanged(CharSequence s, int start, int before, int count) { 
      for (int i=registrationPlateEditTexts.size()-1; i>=0; i--){ 
       if (registrationPlateEditTexts.get(i).getText().length()==1 && i!=registrationPlateEditTexts.size()-1){ 
        registrationPlateEditTexts.get(i+1).requestFocus(); 
        return; 
       } 
       else if (registrationPlateEditTexts.get(i).getText().length()==1){ 
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); 
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
       } 
      } 
     } 
     @Override public void afterTextChanged(Editable s) {} 
    }; 

      else if (registrationPlateEditTexts.get(i).getText().length()==1){ 
       InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
      } 
     } 
    } 
    @Override public void afterTextChanged(Editable s) {} 
};` 

回答

0

我花了4自定義編輯文本與選擇得到財產時按回。這裏有聽衆和CustomEditText元素

mCodeFourEt.setOnEditorActionListener(new EditText.OnEditorActionListener() { 
     @Override 
     public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) { 
      if (actionId == EditorInfo.IME_ACTION_DONE) { 
       mConfirmBtn.performClick(); 
       return true; 
      } 
      return false; 
     } 


    }); 


    mCodeTwoEt.setOnKeyListener(new View.OnKeyListener() { 
     @Override 
     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      if (keyCode == KeyEvent.KEYCODE_DEL) { 
       String text = mCodeTwoEt.getText().toString(); 
       if (text.length() == 0) { 
        mCodeOneEt.requestFocus(); 
        mCodeOneEt.selectAll(); 
        return true; 
       } 
      } 

      return false; 
     } 
    }); 

    mCodeThreeEt.setOnKeyListener(new View.OnKeyListener() { 
     @Override 
     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      if (keyCode == KeyEvent.KEYCODE_DEL) { 
       String text = mCodeThreeEt.getText().toString(); 
       if (text.length() == 0) { 
        mCodeTwoEt.requestFocus(); 
        mCodeTwoEt.selectAll(); 
        return true; 
       } 
      } 

      return false; 
     } 
    }); 

    mCodeFourEt.setOnKeyListener(new View.OnKeyListener() { 
     @Override 
     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      if (keyCode == KeyEvent.KEYCODE_DEL) { 
       String text = mCodeFourEt.getText().toString(); 
       if (text.length() == 0) { 
        mCodeThreeEt.requestFocus(); 
        mCodeThreeEt.selectAll(); 
        return true; 
       } 

      } 

      return false; 
     } 
    }); 

    mCodeOneEt.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

     } 

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

     } 

     @Override 
     public void afterTextChanged(Editable editable) { 
      if (mCodeOneEt.getText().toString().length() > 0) { 
       mCodeTwoEt.requestFocus(); 
      } 

     } 
    }); 

    mCodeTwoEt.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

     } 

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

     } 

     @Override 
     public void afterTextChanged(Editable editable) { 
      if (mCodeTwoEt.getText().toString().length() > 0) { 
       mCodeThreeEt.requestFocus(); 
      } 

     } 
    }); 

    mCodeThreeEt.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

     } 

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

     } 

     @Override 
     public void afterTextChanged(Editable editable) { 
      if (mCodeThreeEt.getText().toString().length() > 0) { 
       mCodeFourEt.requestFocus(); 
      } 

     } 
    }); 

    mCodeFourEt.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

     } 

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

     } 

     @Override 
     public void afterTextChanged(Editable editable) { 

     } 
    }); 

現在這裏是定製的EditText類

public class CustomEditText extends android.support.v7.widget.AppCompatEditText { 


public CustomEditText(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

public CustomEditText(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public CustomEditText(Context context) { 
    super(context); 
} 


@Override 
public InputConnection onCreateInputConnection(EditorInfo outAttrs) { 
    return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), 
      true); 
} 

private class ZanyInputConnection extends InputConnectionWrapper { 

    public ZanyInputConnection(InputConnection target, boolean mutable) { 
     super(target, mutable); 
    } 

    @Override 
    public boolean sendKeyEvent(KeyEvent event) { 
     if (event.getAction() == KeyEvent.ACTION_DOWN 
       && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { 
      // Un-comment if you wish to cancel the backspace: 
      // return false; 
     } 
     return super.sendKeyEvent(event); 
    } 


    @Override 
    public boolean deleteSurroundingText(int beforeLength, int afterLength) { 
     // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace 
     if (beforeLength == 1 && afterLength == 0) { 
      // backspace 
      return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) 
        && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); 
     } 

     return super.deleteSurroundingText(beforeLength, afterLength); 
    } 

} 
} 

只需使用你的XML這個自定義類和見上這是4編輯文本的例子。