InputType对自定义EditText没有影响

时间:2018-03-13 19:27:41

标签: android android-edittext android-custom-view android-inputtype

我创建了从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”inputTypes之外,它运行良好。虽然“textMultiLine”正在运行,但“textCapSentences”和“textNoSuggestions”都没有应用。

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

1 个答案:

答案 0 :(得分:1)

    this.setRawInputType(InputType.TYPE_CLASS_TEXT);

从启用功能。这会覆盖输入类型。