【问题标题】:Android EditText AutoFocus to next EditText when at maxlengthAndroid EditText 在 maxlength 时自动聚焦到下一个 EditText
【发布时间】:2012-01-09 06:06:31
【问题描述】:

我有一个用户名和一个密码 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 方法中使用它。任何想法或建议将不胜感激。

【问题讨论】:

  • 尝试这段代码会发生什么?

标签: android focus android-edittext


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    这样尝试可能会对您有所帮助。

    /** addTextChangedListener 在第一个用户名框填充到第 9 个字符时很有用 * 请求会自动聚焦到下一个框*/

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

    【讨论】:

      【解决方案3】:

      从您的编辑文本 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) {
          }
      });
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

            log.d() 或其他东西打印出你的"s",这样你就可以看到"s" 的样子。
            也许它不是你所期望的。
            在这种情况下,您可以code it dirty
            创建自己的TextWatcher实现TextWatcher,并在构造函数中提供"usr"
            所以你可以用usr.getText()得到总长度

            【讨论】:

              猜你喜欢
              • 2016-08-14
              • 2015-01-10
              • 1970-01-01
              • 1970-01-01
              • 2021-11-02
              • 2021-10-03
              • 1970-01-01
              • 2019-10-23
              • 1970-01-01
              相关资源
              最近更新 更多