InputType 对自定义 EditText 没有影响

InputType has no effect on a custom EditText

我创建了这个扩展自 AppCompatEditText 的自定义视图:

    public class NoteEditText extends AppCompatEditText {

    // INTERFACES ----------------------------------------------------------------------------------------------------
    public interface OnKeyPreImeListener {
        void onKeyPreIme(int keyCode, KeyEvent event);
    }


    // ATTRIBUTES ----------------------------------------------------------------------------------------------------
    private MovementMethod movementMethod;
    private KeyListener keyListener;
    private OnKeyPreImeListener onKeyPreImeListener;


    // CONSTRUCTORS ----------------------------------------------------------------------------------------------------
    public NoteEditText(Context context) {
        super(context);

        this.movementMethod = this.getMovementMethod();
        this.keyListener = this.getKeyListener();
    }

    public NoteEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.movementMethod = this.getMovementMethod();
        this.keyListener = this.getKeyListener();
    }

    public NoteEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.movementMethod = this.getMovementMethod();
        this.keyListener = this.getKeyListener();
    }


    // SETTERS ----------------------------------------------------------------------------------------------------
    public void setOnKeyPreImeListener(OnKeyPreImeListener onKeyPreImeListener) {
        this.onKeyPreImeListener = onKeyPreImeListener;
    }


    // METHODS ----------------------------------------------------------------------------------------------------
    public void enable() {
        this.setMovementMethod(this.movementMethod);
        this.setKeyListener(this.keyListener);
        this.setFocusableInTouchMode(true);
        this.setImeOptions(EditorInfo.IME_ACTION_DONE);
        this.setRawInputType(InputType.TYPE_CLASS_TEXT);
    }

    public void disable() {
        this.setMovementMethod(null);
        this.setKeyListener(null);
    }

    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (this.onKeyPreImeListener != null)
            this.onKeyPreImeListener.onKeyPreIme(keyCode, event);

        return true;
    }

}

我这样称呼它:

    <com.company.adapters.items.NoteEditText
    android:id="@+id/note"
    style="@style/AppTheme.SubItem"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/content_margin_xs"
    android:gravity="center_vertical"
    android:hint=""
    android:imeOptions="actionDone"
    android:inputType="textMultiLine|textCapSentences|textNoSuggestions"
    android:minHeight="@dimen/content_subitem_height"
    android:visibility="gone"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@id/note_icon"
    app:layout_constraintTop_toBottomOf="@+id/name" />

除了 "textMultiLine|textCapSentences|textNoSuggestions" 输入类型外,它运行良好。尽管 "textMultiLine" 有效,但 "textCapSentences" 和 "textNoSuggestions" 均未应用。

如果我使用完全相同的配置但使用原始 EditText 视图,则所有 inputTypes 都有效...非常奇怪。

    this.setRawInputType(InputType.TYPE_CLASS_TEXT);

来自您的启用功能。这是覆盖输入类型。