【问题标题】:How to change the focus when user validate the EditText用户验证 EditText 时如何更改焦点
【发布时间】:2020-08-18 16:07:34
【问题描述】:

在我的情况下,用户必须给出小时和分钟,但我找不到如何将焦点放在分钟上,因为我用一个字符输入小时。小时可以是一两个字符,所以即使我只给出一个字符并使用键盘验证,我也想这样做。 有我的代码:

ed_nbHeure.addTextChangedListener(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) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(ed_nbHeure.length()>1){
                    ed_nbMin.requestFocus();
                }


            }
        });

我想知道如何用键盘调用验证动作来改变焦点。

谢谢

【问题讨论】:

    标签: android android-edittext focus


    【解决方案1】:

    试试这个:

    ed_nbHeure.addTextChangedListener(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) {
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
                    if(s.length()==1){
                        ed_nbMin.requestFocus();
                    }
    
    
                }
            });
    

    afterTextChanged中添加这个条件

    if(s.length()==1)
    {
         ed_nbMin.requestFocus();
    }
    

    【讨论】:

    • 如果我这样做我不能输入两个字符
    • @NeoKerd 只是改变输入字符长度的条件
    • 我想知道如何用键盘调用验证动作来改变焦点
    【解决方案2】:

    就我而言,我只需要像这样使用 .setOnEditorActionListener() :

    ed_nbHeure.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId== EditorInfo.IME_ACTION_DONE){
                        ed_nbMin.requestFocus();
    
                    }
                    return false;
                }
    
            });
    

    感谢您的帮助

    【讨论】:

      猜你喜欢
      • 2020-02-19
      • 1970-01-01
      • 2014-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-25
      相关资源
      最近更新 更多