【问题标题】:InputType has no effect on a custom EditTextInputType 对自定义 EditText 没有影响
【发布时间】:2018-03-13 19:27:40
【问题描述】:

我创建了这个从 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”输入类型。 “textCapSentences”和“textNoSuggestions”都没有应用,虽然“textMultiLine”正在工作。

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

【问题讨论】:

  • 你可以尝试用代码添加这些属性,看看它是否可以这样工作?
  • @DroiDev 我直接在“super(context, attrs);”之后使用“setInputType”和“setRawInputType”尝试了这些属性(InputType.TYPE_TEXT_FLAG_MULTI_LINE|InputType.TYPE_TEXT_FLAG_CAP_SENTENCES|InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS)。没有效果,连“textMultiLine”属性都不起作用。
  • @DroiDev 添加“InputType.TYPE_CLASS_TEXT”让“textMultiLine”属性再次起作用。但是“textCapSentences”和“textNoSuggestions”都不起作用。

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


【解决方案1】:
    this.setRawInputType(InputType.TYPE_CLASS_TEXT);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多