2016-01-21 64 views
0

我有一个活动中的4个编辑文本,输入限制为3个字符。
我想实现行为,因为用户在以前的编辑文本中键入3个字符时 - 下一次编辑应该自动获得焦点。 相反:当用户从上次编辑文本的末尾开始删除时 - 用户可以删除所有先前字段中的字符,而无需手动选择每个编辑文本。
实现这个最简单的方法是什么?Android连续EditTexts

我只能想到为每个EditText使用keyListenners,当它等于最大值时,检查文本长度和开关焦点。

这是一种正确的方式还是有其他解决方案?

+0

显示你与现在的工作,包括布局,请代码。 –

+0

使用TextWatcher,你也可以在这里结算计数解决方案http://stackoverflow.com/questions/4310525/counting-chars-in-edittext-changed-listener –

+0

谢谢。以及如何更好地切换到另一个edittext? – Androider

回答

1

我认为这也是最好的方法。

我会尝试一些像:

firstText.addTextChangedListener(new TextWatcher(){ 
    public void afterTextChanged(CharSequence s) {} 
    public void beforeTextChanged(CharSequence s, int start, int count, int after){} 
    public void onTextChanged(CharSequence s, int start, int before, int count){ 
     if(s.toString().length() == 3){ 
      sedoncText.setFocusableInTouchMode(true); 
      sedoncText.requestFocus(); 
     } 
    } 
}); 

我没有测试它,也许有些功能不完全正确拼写。

希望它有帮助。

1

您可以使用TextWatcher进行此项工作。 短代码片段:

editTextA.addTextChangedListener(new TextWatcher() { 
       @Override 
       public void onTextChanged(CharSequence s, int start, int before, int count) { 

        // implement your logic here 
        // e.g. Set cursor to the end of another Textfield: 

        if (s.toString().length() > 2){ 
         int position = editTextB.length(); 
         editTextB.setSelection(position); 
        } 
       } 

       @Override 
       public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

        // TODO Auto-generated method stub 
       } 

       @Override 
       public void afterTextChanged(Editable s) { 

        // TODO Auto-generated method stub 
       } 
}); 

对于你的任务,每一个需要的EditText自己TextWatcher(包括specifig逻辑)。

UPDATE:

短(未经测试)知道如何删除当开关的EditText:

private boolean isEditTextVirgin; 
isEditTextVirgin = true; 
editTextB.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 

      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
       // Check if the TextView is filled before editing 
       // if yes then save this information for later 
       if (s.toString().length() > 0){ 
        isEditTextVirgin = false; 
       } 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       // Check if the EditText is empty after a edit and if it was filled before 
       // if both yes then jump the the previous EditText 
       if (s.toString().length() == 0 && isEditTextVirgin == false){ 
        int position = editTextA.length(); 
        editTextA.setSelection(position); 
        // if you want you can reset your variable 
        // isEditTextVirgin = true; 
       } 
      } 
}); 

要保存在一个成员变量状态(如果曾经的EditText充满)。

+0

以及如何管理字符删除,所以当我移动到前一个字段时删除最后一个字段中的所有字符? – Androider

1

在EditText上添加

android:maxLength="3" 

这将不允许超过3个字符输入。

并把重点放在未来的EditText请参考以下链接

Link: Move focus to next edittext