在的EditText列表视图失去焦点时pressed在Android 4.x的 [英] EditText in Listview loses focus when pressed on Android 4.x

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

问题描述

我知道有很多类似的问题在这里 但我无法得到任何的解决方案提供了一个简单的示例应用程序的工作。

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.

softkeyboard 显示首次出现该问题。只要它表明,只有pressing在 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"

这是不解决任何问题。看来,这条线是必须有后softkeyboard是雨后春笋般冒出来调整活动。不幸的是,它也引起任何的 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);

这总是会导致第一个可见的EditText 的ListView 包含获得的重点,这是不可取的。第二行中的第二个EditText上而应该获得焦点时,pressed,这是不会发生。另外,如果我最终设法集中另一个的EditText其他则显示的第一个(例如,通过pressing下一步的 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.

我尝试过其他几个像拦截 onFocusChange()事件为ListView,而知道这的EditText是pressed其 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.

而不是使用的滚动型 A 的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;
}

这工作我的设备上,但要保持这种code生产出来的。我也不会感到惊讶,如果焦点的错误表现在不同的Andr​​oid不同版本或光盘。

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.

也有许多其他问题中嵌入的EditText 的ListView 的有感觉像一个黑客攻击的解决方案。查看所有的其他的挣扎。

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 一行被点击时,更新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?

这篇关于在的EditText列表视图失去焦点时pressed在Android 4.x的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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