![](/img/trans.png)
[英]Android edittext in listview loses focus on calling notifydatachanged
[英]EditText in Listview loses focus when pressed on Android 4.x
我知道這里有很多類似的問題,但我無法在簡單的示例應用程序中獲得任何提供的解決方案。
首次顯示軟鍵盤時會出現此問題。 一旦顯示,只有再次按下editText 才能使其可編輯。
嘗試了以下方法:
android:windowSoftInputMode="adjustPan|adjustResize"
這並不能解決任何問題。 在彈出軟鍵盤后,此行似乎是強制調整活動大小的必要條件。 不幸的是,它也會導致任何EditTexts失去焦點。 這可能是因為在調整大小過程后ListView本身獲得了焦點。 所以我嘗試了以下解決方法:
listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
這總是導致ListView
包含的第一個可見 EditText獲得焦點,這是不可取的。 第二行中的第二個 EditText 應該在按下時獲得焦點,而這並沒有發生。 此外,如果我最終設法將另一個 EditText 聚焦而不是顯示的第一個(例如通過按softkeyboard
鍵盤上的“Next”),第一個可見的將在鍵盤關閉並且 ListView 再次調整到其完整大小后獲得焦點.
我嘗試了其他一些事情,比如攔截 ListView 的onFocusChange()
事件,同時知道它的TouchListener
按下了哪個 EditText 。 再次請求該特定 EditText 的焦點也沒有取得任何成功。
使用ScrollView
而不是其他用戶建議的ListView
也不是相關項目的選項。
對於這種情況,一個經典的技巧是使用處理程序和postDelayed()
。 在您的適配器中:
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 中以不同的方式表現出來,我也不會感到驚訝。
在ListView
中嵌入EditText
還存在許多其他問題,這些問題的解決方案感覺像是黑客。 查看所有的其他人掙扎。
發生這樣的事情也很容易:
.
在我自己多次走上類似的道路之后,我基本上已經放棄嘗試覆蓋任何默認的鍵盤行為或怪癖。 如果可能,我建議嘗試在您的應用中找到替代解決方案。
您是否考慮過讓ListView
行只是一個樣式化的TextView
,然后在單擊一行時顯示一個帶有EditText
的Dialog
並根據需要更新 TextView?
當我按下位於 ListView 行內的 EditText 時,我遇到了 ActionBar“竊取”焦點的問題。 上述解決方案不起作用,但以下解決方案對我有用:
http://www.mysamplecode.com/2013/02/android-edittext-listview-loses-focus.html
基本上我把它添加到我的 ListView 中:
android:descendantFocusability="beforeDescendants"
並將其添加到我的活動中:
android:windowSoftInputMode="adjustPan"
修改您的清單 xml 以在您的活動中添加 windowSoftInputMode:
<activity
android:name=".YourActivity"
android:windowSoftInputMode="adjustPan">
</activity>
我知道它是一個非常古老的線程,但這個答案可能對某人有幫助,所以這里是:
切換到RecyclerView就不用擔心 ListView 的這些煩人的問題了。 它不是創建新視圖,而是回收和重用舊視圖。
我在使用recyclerView
時遇到了同樣的問題,並嘗試了所有建議的解決方案。
最后,我的案例的問題是recyclerView
意外地將wrap_content
作為我的 XML 上的高度值; 將其更改為match_parent
並按預期開始工作,未設置focusable
值並使用android:windowSoftInputMode="adjustResize"
使用 Recycler View,這解決了 list 和gridview
幾個問題。 您甚至可以使用交錯的gridviews
。 您可以輕松地開始使用它。
當列表足夠長以覆蓋軟鍵盤時,在 Android 4.x 上按下時Listview
的EditText
會失去焦點。
一種解決方案是將Listview
包裝在一個高度為屏幕一半的線性布局中。
每當Listview
不包括softkeyboard,一切都很好。
有同樣的問題。 使用 inputMode、focusability 等搜索所有此類解決方案。 最佳解決方案,遷移到回收站視圖。
就我而言,我在適配器中添加了本地值 currentFocusedRow。 在 getView() 方法中,我將這些代碼添加到每個 editText:
if (currentlyFocusedRow == position) {
editText.requestFocus();
}
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
if (currentlyFocusedRow == -1) {
currentlyFocusedRow = position;
}
} else {
currentlyFocusedRow = -1;
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.