【问题标题】:Softkeyboard skipping typing event with multiple EditText带有多个 EditText 的软键盘跳过键入事件
【发布时间】:2018-12-28 02:54:56
【问题描述】:

对于其中一个功能,我正在编写一种 OTP 类型的小部件,它使用多个(比如 4 个)编辑文本。一旦你输入任何一个,它会将焦点重置到下一个 EditText (requestFocus())。

在带有计算机键盘的模拟器上,无论快多么快,我都很高兴看到这个空白。

但是当我用软键盘检查真实设备时,它实际上跳过了该事件,看起来它没有获得焦点。它需要再次点击数字才能输入。

观察:可能是因为 requestFocus()。

你看到有什么改进可以解决它吗!!

【问题讨论】:

    标签: android android-edittext focus android-softkeyboard


    【解决方案1】:

    试试这个..

        Edit_otp_number1 = findViewById(R.id.edit_otp_number1);
        Edit_otp_number2 = findViewById(R.id.edit_otp_number2);
        Edit_otp_number3 = findViewById(R.id.edit_otp_number3);
        Edit_otp_number4 = findViewById(R.id.edit_otp_number4);
    
        Edit_otp_number1.addTextChangedListener(watcher);
        Edit_otp_number2.addTextChangedListener(watcher);
        Edit_otp_number3.addTextChangedListener(watcher);
        Edit_otp_number4.addTextChangedListener(watcher);
    

    然后像这样写一个文本观察器..

     private final TextWatcher watcher = 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 (Edit_otp_number1.hasFocus()) {
                Edit_otp_number2.requestFocus();
            }
            if ((Edit_otp_number2.hasFocus()) && (Edit_otp_number2.getText().length() == 1)) {
                Edit_otp_number3.requestFocus();
            }
            if (Edit_otp_number3.hasFocus() && (Edit_otp_number3.getText().length() == 1)) {
                Edit_otp_number4.requestFocus();
            }
        }
    };
    

    【讨论】:

      猜你喜欢
      • 2011-07-31
      • 1970-01-01
      • 2011-09-28
      • 2014-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-21
      • 2011-10-17
      相关资源
      最近更新 更多