Android中的EditText在输入达到最大长度时如何自动聚焦到下一个EditText?

13

我有一个用户名和密码的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方法中使用它来处理我的登录信息。如果有任何想法或建议,将不胜感激。


当您尝试运行此代码时会发生什么? - Hiral Vadodaria
6个回答

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) {
   }
});

0
尝试在afterTextChanged方法中执行此操作。 在请求pwd的焦点之前,您还可以调用usr.clearFocus()。

0

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


0

使用log.d()或其他方式打印出你的"s",这样你就可以看到"s"的样子。
也许它不是你期望的样子。
在这种情况下,你可以粗略地编码
创建自己的TextWatcher,并在构造函数中传入"usr",以实现TextWatcher,这样你就可以通过usr.getText()获取总长度。


0

从您的EditText 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) {
    }
});

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) {
            }
        });
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接