問題描述
我在 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 字段失去焦點的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!