When the soft keyboard appears, it makes my EditText field lose focus(当软键盘出现时,它使我的 EditText 字段失去焦点)
问题描述
我在 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:
- 如果焦点位于不同的视图上,则该视图失去焦点
- 目标获得焦点
- 弹出软键盘.
- 这会导致目标失去焦点
- 代码检测到这种情况并调用 target.requestFocus()
- 由于 Android 的胡说八道,最左侧、最顶部的视图获得焦点
- 由于调用 requestFocus,最左边的视图失去焦点
目标终于获得焦点
- If focus was on a different view, then that view loses focus
- The target gains focus
- Soft keyboard pops up.
- This causes the target to lose focus
- The code detects this situation and calls target.requestFocus()
- The leftmost, topmost view gains focus, due to Android nonsense
- The leftmost view loses focus, due to requestFocus being called
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 字段失去焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当软键盘出现时,它使我的 EditText 字段失去焦点
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01