問題描述
我在 ListView 中有幾個(gè) EditText 字段.當(dāng)我點(diǎn)擊其中一個(gè) EditText 字段時(shí),鍵盤會(huì)滑入視圖(應(yīng)該如此),但我點(diǎn)擊的 EditText 字段失去焦點(diǎn).我嘗試使用各種 InputMethodManager 方法使鍵盤在視圖中啟動(dòng)(為了解決問題而不是真正解決它),但這不起作用 - 當(dāng) Activity 出現(xiàn)時(shí)鍵盤不在視圖中.
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
,當(dāng)鍵盤滑入的時(shí)候是數(shù)字鍵盤,但是當(dāng)滑完EditText失去焦點(diǎn)后,變成字母鍵盤(加強(qiáng)了EditText 不再具有焦點(diǎn)的想法).
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 字段以及隨后滑入軟鍵盤不會(huì)使我的 EditText 失去焦點(diǎn)?
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) 我怎樣才能讓鍵盤一開始就在視野中,這樣它就不必滑入(從而避免我認(rèn)為如此令人反感的行為)?
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)?
我的清單確實(shí)包含 android:windowSoftInputMode="stateAlwaysVisible"
,但在我點(diǎn)擊 EditText 之前鍵盤不會(huì)出現(xiàn).這種對stateAlwaysVisible"屬性的忽略似乎只發(fā)生在模擬器中——在我配置的設(shè)備上,這是很榮幸的,所以上面的問題 2 確實(shí)適用于設(shè)備......但不適用于模擬器.
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()
會(huì)在您觸摸 EditText
以在其中鍵入文本時(shí)多次調(diào)用.順序是:
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:
- 如果焦點(diǎn)位于不同的視圖上,則該視圖失去焦點(diǎn)
- 目標(biāo)獲得焦點(diǎn)
- 彈出軟鍵盤.
- 這會(huì)導(dǎo)致目標(biāo)失去焦點(diǎn)
- 代碼檢測到這種情況并調(diào)用 target.requestFocus()
- 由于 Android 的胡說八道,最左側(cè)、最頂部的視圖獲得焦點(diǎn)
- 由于調(diào)用 requestFocus,最左邊的視圖失去焦點(diǎn)
目標(biāo)終于獲得焦點(diǎn)
- 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();
}
});
}
}
}
};
上面的代碼適用于鍵盤彈出窗口.但是,它不會(huì)檢測到語音到文本的彈出窗口.
The code above works well for the keyboard pop-ups. However, it does not detect the speech-to-text pop-up.
這篇關(guān)于當(dāng)軟鍵盤出現(xiàn)時(shí),它使我的 EditText 字段失去焦點(diǎn)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!