在 Android 4.x 上按下时,Listview 中的 EditText 失去焦点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20406472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EditText in Listview loses focus when pressed on Android 4.x
提问by Display name
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.
我知道这里有很多类似的问题,但我无法在简单的示例应用程序中获得任何提供的解决方案。
The problem occurs when the softkeyboardis shown for the first time. As soon as it is shown, only by pressing the editTextagain makes it editable.
首次显示软键盘时会出现此问题。一旦显示,只有再次按下editText 才能使其可编辑。
Tried the following:
尝试了以下方法:
android:windowSoftInputMode="adjustPan|adjustResize"
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 EditTextsto lose focus. This is probably to the ListViewitself gaining focus after the resizing process. So I tried the following workaround:
这并不能解决任何问题。在弹出软键盘后,此行似乎是强制调整活动大小的必要条件。不幸的是,它也会导致任何EditTexts失去焦点。这可能是因为在调整大小过程后ListView本身获得了焦点。所以我尝试了以下解决方法:
listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
This always causes the first visible EditTextthat 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.
这总是导致第一可见的EditText了ListView
包含获得焦点,这是不可取的。第二行中的第二个 EditText 应该在按下时获得焦点,而这并没有发生。此外,如果我最终设法聚焦另一个 EditText 而不是显示的第一个(例如通过在 上按“Next” softkeyboard
),第一个可见的将在键盘关闭并且 ListView 再次调整到其完整大小后获得焦点。
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.
我尝试了其他一些事情,比如拦截onFocusChange()
ListView 的事件,同时知道它的TouchListener
. 再次请求该特定 EditText 的焦点也没有取得任何成功。
Using a ScrollView
instead of a ListView
as suggested by other users is not an option either for the concerned project.
使用 aScrollView
而不是ListView
其他用户建议的 a也不是相关项目的选项。
采纳答案by Kyle Ivey
A classic hack for situations like this is to use a handler and postDelayed()
. In your adapter:
对于这种情况,一个经典的技巧是使用处理程序和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;
}
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.
这适用于我的设备,但不要生产此代码。如果焦点错误在不同的 android 版本或 rom 中以不同的方式表现出来,我也不会感到惊讶。
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 otherpeoplestruggling.
将 an 嵌入EditText
到 a中还存在许多其他问题,这些问题的ListView
解决方案感觉像是一个黑客。查看所有的其他人挣扎。
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.
在我自己多次走上类似的道路之后,我基本上已经放弃尝试覆盖任何默认的键盘行为或怪癖。如果可能,我建议尝试在您的应用中找到替代解决方案。
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?
您是否考虑过让ListView
行只是一个样式TextView
,然后在单击一行时显示一个Dialog
with EditText
,并根据需要更新 TextView?
回答by gonzobrains
I was having problems with the ActionBar "stealing" focus when I pressed on an EditText located within a ListView row. The above solutions did not work, but the following solution worked for me:
当我按下位于 ListView 行内的 EditText 时,我遇到了 ActionBar“窃取”焦点的问题。上述解决方案不起作用,但以下解决方案对我有用:
http://www.mysamplecode.com/2013/02/android-edittext-listview-loses-focus.html
http://www.mysamplecode.com/2013/02/android-edittext-listview-loses-focus.html
Basically I added this to my ListView:
基本上我把它添加到我的 ListView 中:
android:descendantFocusability="beforeDescendants"
and added this to my activity:
并将其添加到我的活动中:
android:windowSoftInputMode="adjustPan"
回答by nlmm01
Modify your manifest xml to add windowSoftInputMode in your activity:
修改您的清单 xml 以在您的活动中添加 windowSoftInputMode:
<activity
android:name=".YourActivity"
android:windowSoftInputMode="adjustPan">
</activity>
回答by ShahrozKhan91
I know its a very old thread but this answer might be helpful to someone so here it is:
我知道它是一个非常古老的线程,但这个答案可能对某人有帮助,所以这里是:
Switch to RecyclerViewand you won't have to worry about these annoying issues of ListView. Instead of making new view it recycles and reuses old views.
切换到RecyclerView就不用担心 ListView 的这些烦人的问题了。它不是创建新视图,而是回收和重用旧视图。
回答by Omer
I was having the same problem with recyclerView
and trying all suggested solutions.
我遇到了同样的问题recyclerView
并尝试了所有建议的解决方案。
Finally, the problem on my case was that the recyclerView
had wrap_content
as value for the height on my XML by accident; changed it to match_parent
and started working as expected, no focusable
value set and using android:windowSoftInputMode="adjustResize"
最后,在我的情况下,问题是,recyclerView
有wrap_content
作为对我的XML偶然的高度值; 将其更改为match_parent
并开始按预期工作,未focusable
设置值并使用android:windowSoftInputMode="adjustResize"
回答by user1176776
Use recycler View, this solves several issues from list and gridview. Yo can even work with staggered gridviews. Yo could easily start working with this http://android-er.blogspot.com.co/2015/07/staggeredgridlayoutmanager-google-app.html
使用回收器视图,这解决了列表和网格视图中的几个问题。你甚至可以使用交错的网格视图。你可以很容易地开始使用这个http://android-er.blogspot.com.co/2015/07/staggeredgridlayoutmanager-google-app.html
回答by Kai Wang
When the list is long enough to cover the soft keyboard, the
EditText
in Listview
loses focus when pressed on Android 4.x.
当列表足够长以覆盖软键盘时,
在 Android 4.x 上按下EditText
in 时Listview
会失去焦点。
One solution is to wrap the Listview
in a linear layout with a height of half the screen.
一种解决方案是将其包裹Listview
在高度为屏幕一半的线性布局中。
Whenever the Listview
doesn't cover the softkeyboard, everything is fine.
只要Listview
不覆盖软键盘,一切都很好。
回答by Malder
In my case I've added local value currentlyFocusedRow in my Adapter. In the method getView() I've add these code to each editText:
就我而言,我在适配器中添加了本地值 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;
}
}
});
回答by Aman Agarwal
was having the same issue. Searched for all such solutions with inputMode, focusability et al. Best solution, migrate to recycler view.
有同样的问题。使用 inputMode、focusability 等搜索所有此类解决方案。最佳解决方案,迁移到回收站视图。