当软键盘出现时,它使我的 EditText 字段失去焦点

时间:2023-05-04
本文介绍了当软键盘出现时,它使我的 EditText 字段失去焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我在 ListView 中有几个 EditText 字段.当我点击其中一个 EditText 字段时,键盘会滑入视图(应该如此),但我点击的 EditText 字段失去焦点.我尝试使用各种 InputMethodManager 方法使键盘在视图中启动(为了解决问题而不是真正解决它),但这不起作用 - 当 Activity 出现时键盘不在视图中.

I've got a few EditText fields in a ListView. When I tap on one of the EditText fields, the keyboard slides into view (as it should), but the EditText field I tapped loses focus. I've tried using various InputMethodManager methods to make the keyboard start out in view (in order to get around the problem rather than truly solve it), but that didn't work - the keyboard was not in view when the Activity appeared.

EditText的类型是number,当键盘滑入的时候是数字键盘,但是当滑完EditText失去焦点后,变成字母键盘(加强了EditText 不再具有焦点的想法).

The EditText's type is number, and when the keyboard is sliding in, it is a number keyboard, but when it finishes sliding and the EditText loses focus, it changes to the alphabetical keyboard (which reinforces the idea that the EditText no longer has focus).

我的问题是:

1) 如何选择我的 EditText 字段以及随后滑入软键盘不会使我的 EditText 失去焦点?

1) How can I make the selection of my EditText field and the subsequent sliding in of the soft keyboard not make my EditText lose focus?

...失败了...

2) 我怎样才能让键盘一开始就在视野中,这样它就不必滑入(从而避免我认为如此令人反感的行为)?

2) How can I make the keyboard start out in view so it never has to slide in (thus avoiding the behavior I find so objectionable)?

我的清单确实包含 android:windowSoftInputMode="stateAlwaysVisible",但在我点击 EditText 之前键盘不会出现.这种对stateAlwaysVisible"属性的忽略似乎只发生在模拟器中——在我配置的设备上,这是很荣幸的,所以上面的问题 2 确实适用于设备......但不适用于模拟器.

My manifest does include android:windowSoftInputMode="stateAlwaysVisible", but the keyboard does not appear until I tap on an EditText. This ignoring of the 'stateAlwaysVisible' attribute seems to only occur in the emulator - on my provisioned device, it is honored so question number 2 above does work on the device... but not in the emulator.

感谢您提供的任何帮助!

Thanks for any help you can provide!

推荐答案

这就是我的做法.onFocusChangeListener() 会在您触摸 EditText 以在其中键入文本时多次调用.顺序是:

Here is how I did it. The onFocusChangeListener() is called several times when you touch a EditText to type text into it. The sequence is:

  1. 如果焦点位于不同的视图上,则该视图失去焦点
  2. 目标获得焦点
  3. 弹出软键盘.
  4. 这会导致目标失去焦点
  5. 代码检测到这种情况并调用 target.requestFocus()
  6. 由于 Android 的胡说八道,最左侧、最顶部的视图获得焦点
  7. 由于调用 requestFocus,最左边的视图失去焦点
  8. 目标终于获得焦点

  1. If focus was on a different view, then that view loses focus
  2. The target gains focus
  3. Soft keyboard pops up.
  4. This causes the target to lose focus
  5. The code detects this situation and calls target.requestFocus()
  6. The leftmost, topmost view gains focus, due to Android nonsense
  7. The leftmost view loses focus, due to requestFocus being called
  8. Target finally gains focus

//////////////////////////////////////////////////////////////////
private final int minDelta = 300;           // threshold in ms
private long focusTime = 0;                 // time of last touch
private View focusTarget = null;

View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        long t = System.currentTimeMillis();
        long delta = t - focusTime;
        if (hasFocus) {     // gained focus
            if (delta > minDelta) {
                focusTime = t;
                focusTarget = view;
            }
        }
        else {              // lost focus
            if (delta <= minDelta  &&  view == focusTarget) {
                focusTarget.post(new Runnable() {   // reset focus to target
                    public void run() {
                        focusTarget.requestFocus();
                    }
                });
            }
        }
    }
};

上面的代码适用于键盘弹出窗口.但是,它不会检测到语音到文本的弹出窗口.

The code above works well for the keyboard pop-ups. However, it does not detect the speech-to-text pop-up.

这篇关于当软键盘出现时,它使我的 EditText 字段失去焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:android如何在edittext中制作文本完全固定的行 下一篇:EditText上的Android键盘下一个按钮问题

相关文章

最新文章