我知道这里有很多类似的问题但我无法在一个简单的示例应用程序中获得任何提供的解决方案.
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模板网!