Android EditText在maxlength时自动聚焦到下一个EditText

时间:2011-11-29 22:22:28

标签: android focus android-edittext

我有一个用户名和密码XML段

<EditText 
    android:id="@+id/username"
    android:hint="@string/username_hint"
    android:layout_width="match_parent"
    android:maxLength="9"
    android:nextFocusDown="@+id/pswd"
    android:layout_height="wrap_content">
</EditText>
<EditText 
    android:inputType="textPassword" 
    android:hint="@string/password_hint"
    android:id="@+id/pswd" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content">
</EditText>

我希望当用户名字段到达第9个字符时,光标焦点会自动跳转到密码字段

我在看Focus next textview automatically并正在尝试:

final EditText usr = (EditText) findViewById (R.id.username);
final EditText pswd = (EditText) findViewById (R.id.pswd);
usr.addTextChangedListener(new TextWatcher() {
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
        if (s.length() == 9) { 
            pswd.requestFocus(); 
        }
    }
    @Override
    public void afterTextChanged(Editable arg0) {
    }

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

没有太大的成功。我在我处理登录信息的oncreate方法中使用它。任何想法或建议将不胜感激。

6 个答案:

答案 0 :(得分:1)

尝试这样可以帮助你。

当填充第一个用户名框到达第9个字符时,

/ ** addTextChangedListener非常有用          *请求将自动聚焦到下一个框* /

usr.addTextChangedListener(new TextWatcher() {
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
         if (count == 9) { 
             pwd.requestFocus(); 
             usr.setTransformationMethod(PasswordTransformationMethod.getInstance());
         }
    }
   @Override
   public void afterTextChanged(Editable arg0) {
   }
   @Override
   public void beforeTextChanged(CharSequence s, int start,
           int count, int after) {
   }
});

答案 1 :(得分:0)

使用"s"或其他内容打印log.d(),以便了解"s"的外观。
也许不是你的期望 在这种情况下,您可以code it dirty。制作自己的TextWatcher 实现TextWatcher ,并在构造函数中提供"usr"
所以你可以用usr.getText()

获得总长度

答案 2 :(得分:0)

你好我现在试试,但是我成功了,我们的代码是一样的,我的xml很简单只有两个editText我的手机是魅族版本是4.1

答案 3 :(得分:0)

尝试在afterTextChanged方法上执行此操作。 你也可以在请求关注pwd之前调用usr.clearFocus()。

答案 4 :(得分:0)

从编辑文本xml中删除nextfocusdown

final EditText usr = (EditText) findViewById (R.id.username);

final EditText pswd = (EditText) findViewById (R.id.pswd);

usr.addTextChangedListener(new TextWatcher() 
{

public void onTextChanged(CharSequence s, int start, int before, int count) 
    { 

        if (usr.getText.toString().length() == 9) (Use usr.getText.toString() instead of s) 
        { 
            pswd.requestFocus(); 
        }
    }
    @Override
    public void afterTextChanged(Editable arg0) {
    }

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

答案 5 :(得分:0)

usr .addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before, int count) 
            { 
                String tusr = usr.getText().toString();
                if (tusr.length()==2) //max lenght 2bit
                { 
                    pswd.requestFocus(); 
                }
            }
            @Override
            public void afterTextChanged(Editable arg0) {
            }

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