問題描述
我知道這里有很多類似的問題但我無法在一個簡單的示例應用程序中獲得任何提供的解決方案.
I know there are a lot of similar questions out here but I couldn't get any of the provided solutions working in a simple sample app.
第一次顯示軟鍵盤時會出現問題.一旦顯示出來,只有再次按下 editText 才能使其可編輯.
The problem occurs when the softkeyboard is shown for the first time. As soon as it is shown, only by pressing the editText again makes it editable.
嘗試了以下方法:
android:windowSoftInputMode="adjustPan|adjustResize"
這并不能解決任何問題.在彈出軟鍵盤后,似乎必須強制調整活動的大小.不幸的是,它還會導致任何 EditTexts 失去焦點.這可能是 ListView 本身在調整大小過程后獲得焦點.所以我嘗試了以下解決方法:
This is not solving any issues. It seems that this line is mandatory to have the activity resized after the softkeyboard is popping up. Unfortunately, it's also causing any EditTexts to lose focus. This is probably to the ListView itself gaining focus after the resizing process. So I tried the following workaround:
listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
這總是會導致 ListView
包含的第一個 可見 EditText 獲得焦點,這是不希望的.第二行中的第二個 EditText 應該在按下時獲得焦點,這沒有發生.此外,如果我最終設法將焦點集中在顯示的第一個之外的另一個 EditText(例如,通過按 softkeyboard
上的下一步"),第一個可見的將在鍵盤被關閉并且 ListView 后獲得焦點再次調整為完整大小.
This always causes the first visible EditText that the ListView
contains to gain focus, which is undesirable. The second EditText in the second row should instead gain focus when pressed, which is not happening. Also, if I eventually managed to focus another EditText other then the first one shown (e.g. by pressing 'Next' on the softkeyboard
), the first visible one will receive focus after the keyboard is dismissed and the ListView being resized to its full size again.
我嘗試了其他一些方法,例如攔截 ListView 的 onFocusChange()
事件,同時知道哪個 EditText 被它的 TouchListener
按下.再次請求某個 EditText 的焦點也沒有成功.
I tried several other things like intercepting onFocusChange()
events for the ListView, while knowing which EditText was pressed by its TouchListener
. Requesting the focus for that certain EditText again did not lead to any success either.
使用 ScrollView
而不是其他用戶建議的 ListView
也不是相關項目的選擇.
Using a ScrollView
instead of a ListView
as suggested by other users is not an option either for the concerned project.
推薦答案
針對這種情況的經典技巧是使用處理程序和 postDelayed()
.在您的適配器中:
A classic hack for situations like this is to use a handler and postDelayed()
. In your adapter:
private int lastFocussedPosition = -1;
private Handler handler = new Handler();
public View getView(final int position, View convertView, ViewGroup parent) {
// ...
edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (lastFocussedPosition == -1 || lastFocussedPosition == position) {
lastFocussedPosition = position;
edittext.requestFocus();
}
}
}, 200);
} else {
lastFocussedPosition = -1;
}
}
});
return convertView;
}
這適用于我的設備,但不要將此代碼用于生產.如果焦點錯誤在不同的 android 版本或 rom 中表現不同,我也不會感到驚訝.
This works on my device, but keep this code out of production. I also wouldn't be surprised if the focus bug manifests itself differently in different android versions or roms.
在 ListView
中嵌入 EditText
還存在許多其他問題,這些問題的解決方案感覺就像是 hack.查看所有 其他 人們在苦苦掙扎.
There are also many other problems with embedding an EditText
within a ListView
that have solutions that feel like a hack. See all of the other people struggling.
這樣的事情也很容易發生:
It's also very easy to have something like this happen:
.
在我自己多次走類似的道路后,我大多放棄了嘗試覆蓋任何默認鍵盤行為或怪癖.如果可能,我建議您嘗試在您的應用中找到替代解決方案.
After having gone down similar paths many times myself, I've mostly given up on trying to override any of the default keyboard behaviours or quirks. I would recommend trying to find alternative solution in your app if possible.
您是否考慮過讓 ListView
行只是一個樣式化的 TextView
,然后顯示一個帶有 EditText
Dialog> 單擊一行時,是否根據需要更新 TextView?
Have you considered having the ListView
rows be just a styled TextView
and then displaying a Dialog
with an EditText
when a row is clicked, updating the TextView as necessary?
這篇關于在 Android 4.x 上按下時 Listview 中的 EditText 失去焦點的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!