【问题标题】:EditText with textMultiLine not working带有 textMultiLine 的 EditText 不起作用
【发布时间】:2016-03-17 23:00:20
【问题描述】:

这是我的 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);

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

【问题讨论】:

  • 我不明白。默认情况下,所有EditTexts 都是多行
  • 尝试删除 line:2 和背景
  • 我删除了edit_View.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);,它似乎有效。

标签: java android android-edittext textview multiline


【解决方案1】:

我最近遇到了这个问题。我发现以编程方式设置 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);

【讨论】:

    【解决方案2】:

    这是有效的:

    <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
    

    【讨论】:

      猜你喜欢
      • 2012-04-29
      • 1970-01-01
      • 1970-01-01
      • 2012-10-25
      • 1970-01-01
      • 2019-12-29
      • 2015-02-23
      • 2021-06-07
      • 2012-10-11
      相关资源
      最近更新 更多