在 Android 4.x 上按下时,Listview 中的 EditText 失去焦点 [英] EditText in Listview loses focus when pressed on Android 4.x

查看:27
本文介绍了在 Android 4.x 上按下时,Listview 中的 EditText 失去焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里有很多类似的问题但我无法在简单的示例应用中使用提供的任何解决方案.

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 上的Next"),第一个可见的将在键盘关闭和 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() 事件,同时知道其 TouchListener 按下了哪个 EditText.再次请求该特定 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 还存在许多其他问题,这些问题的解决方案感觉像是黑客.查看所有其他人们苦苦挣扎.

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,然后显示一个带有 EditTextDialog> 单击一行时,是否根据需要更新 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 失去焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆