簡體   English   中英

在軟鍵盤上按下Next IME按鈕時,跳過禁用EditText

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

我有一個帶有幾個EditTextLinearLayout ,它們都是以編程方式創建的(不是使用XML布局),特別是沒有ID。

當我輸入其中一個EditText ,下一個(相應於焦點)被禁用,然后按下鍵盤上的Next IME按鈕,焦點前進到禁用的EditText ,但我無法輸入其中的任何東西。

我期待的是專注於推進到下一個啟用的 EditText 除了通過edittext.setEnabled(false)禁用EditText之外,我還嘗試通過edittext.setFocusable(false)edittext.setFocusableInTouchMode(false)禁用其可聚焦性,並設置TYPE_NULL輸入類型,但無濟於事。

任何提示?

謝謝 ;)

通過檢查鍵盤從這篇博客文章和子類化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;
    }

}

更多細節:

focusSearch() ViewGroup實現使用FocusFinder ,它調用addFocusables() ViewGroup的實現測試可見性,而View實現測試可聚焦性。 既沒有測試啟用狀態,這就是我將此測試添加到上面的MyEditText

我解決了它將focusable屬性設置為false,而不僅僅是enabled屬性:

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

看到

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;
    }
});

這是從http://developer.android.com/training/keyboard-input/style.html#Action獲取的

如果你可以弄清楚如何專注於下一個TextView ,你可以為每個TextView添加一個OnEditorActionListener ,如果它被禁用,它會將焦點傳遞給下一個TextView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM