带有 textMultiLine 的 EditText 不起作用

EditText with textMultiLine not working

这是我的 EditText。为什么不允许多行?

<EditText
    android:id="@+id/edtextDesigner"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toggleText"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:alpha="0.5"
    android:background="@drawable/rounded_corner"
    android:ems="10"
    android:lines="2"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:fontFamily="sans-serif-light"
    android:inputType="textMultiLine"
    android:padding="10dp"
    android:scrollHorizontally="false"
    android:textColor="#000000"
    android:textSize="16sp"
    android:typeface="sans"
    android:text=" "
    android:visibility="invisible" />

我也通过编程方式设置了它。

        edit_View.setSingleLine(false);
        edit_View.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
        edit_View.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);

我完全不知道为什么这不起作用。

我最近遇到了这个问题。我发现以编程方式设置 inputType 会重置大多数选项,因此您必须在之后设置它们。我认为将您的代码更改为此可能有效:

edit_View.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
edit_View.setSingleLine(false);
edit_View.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);

这是有效的方法:

<EditText
        android:maxLines="4"
        android:lines="4"
        android:gravity="top"
        android:singleLine="false"
        android:maxLength="255"
        android:id="@+id/etRemarks"
        android:layout_width="match_parent"
        android:inputType="textMultiLine|textCapSentences"
        android:layout_height="wrap_content"
        android:importantForAutofill="no" />

关键属性是:

android:singleLine="false"
android:inputType="textMultiLine|textCapSentences"
android:maxLines="4". // to set the limit
android:lines="4" // to set the 4-liner appearance by default