简体   繁体   English

在软键盘上按下Next IME按钮时,跳过禁用EditText

[英]Skipping disabled EditText's when pressing Next IME button on soft keyboard

I have a LinearLayout with several EditText 's, all of them created programmatically (not with an XML layout), and in particular without IDs. 我有一个带有几个EditTextLinearLayout ,它们都是以编程方式创建的(不是使用XML布局),特别是没有ID。

When I'm typing in one of the EditText 's, and the next one (respective to focus) is disabled, and I press the Next IME button on the keyboard, the focus advances to the disabled EditText , but I can't type anything in it. 当我输入其中一个EditText ,下一个(相应于焦点)被禁用,然后按下键盘上的Next IME按钮,焦点前进到禁用的EditText ,但我无法输入其中的任何东西。

What I was expecting was focus to advance to the next enabled EditText . 我期待的是专注于推进到下一个启用的 EditText I also tried, in addition to making the EditText disabled via edittext.setEnabled(false) , to disable its focusability via edittext.setFocusable(false) and edittext.setFocusableInTouchMode(false) , and to set a TYPE_NULL input type, but to no avail. 除了通过edittext.setEnabled(false)禁用EditText之外,我还尝试通过edittext.setFocusable(false)edittext.setFocusableInTouchMode(false)禁用其可聚焦性,并设置TYPE_NULL输入类型,但无济于事。

Any hints? 任何提示?

Thanks ;) 谢谢 ;)

Solved by examining how the next focusable is found by the keyboard from this blog post and by subclassing EditText : 通过检查键盘从这篇博客文章和子类化EditText找到下一个可聚焦的方法来解决:

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;

public class MyEditText extends EditText {

    public MyEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyEditText(Context context) {
        super(context);
    }

    @Override
    public View focusSearch(int direction) {
        View v = super.focusSearch(direction);
        if (v != null) {
            if (v.isEnabled()) {
                return v;
            } else {
                // keep searching
                return v.focusSearch(direction);
            }
        }
        return v;
    }

}

More details: 更多细节:

ViewGroup implementation of focusSearch() uses a FocusFinder , which invokes addFocusables() . focusSearch() ViewGroup实现使用FocusFinder ,它调用addFocusables() The ViewGroup 's implementation tests for visibility, while the View implementation tests for focusability. ViewGroup的实现测试可见性,而View实现测试可聚焦性。 Neither test for the enabled state, which is why I added this test to MyEditText above. 既没有测试启用状态,这就是我将此测试添加到上面的MyEditText

I solved it setting the focusable property to false, not only the enabled property: 我解决了它将focusable属性设置为false,而不仅仅是enabled属性:

editText.setEnabled(false);
editText.setFocusable(false);

See 看到

EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            sendMessage();
            handled = true;
        }
        return handled;
    }
});

This was grabbed from http://developer.android.com/training/keyboard-input/style.html#Action . 这是从http://developer.android.com/training/keyboard-input/style.html#Action获取的

If you can figure out how to focus on the next TextView , you can add an OnEditorActionListener to every TextView and have it pass the focus to the next one if it is disabled. 如果你可以弄清楚如何专注于下一个TextView ,你可以为每个TextView添加一个OnEditorActionListener ,如果它被禁用,它会将焦点传递给下一个TextView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM