Alternative of singleLine attribute (Deprecated) TextInputEditText

六月ゝ 毕业季﹏ 提交于 2019-11-28 05:41:29
fractalwrench

The android:singleLine attribute has been deprecated since API Level 3. You can achieve the same behaviour by using android:maxLines, which allows you to specify an arbitrary number of lines. This is superior to android:singleLine, which restricts you to only allowing one line.

<TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:minLines="2"
     android:maxLines="2" /> <!-- can specify arbitrary number of max lines -->

android:singleLine is deprecated since API 3, you have to use android:maxLines instead (in your case android:maxLines="1").

The reason of the deprecation is for its bad performance. Anyway the singleLine attribute will not be removed because it's still the only way to make some effects that android:maxLines can't make:

e.g.

This will produce a scrolling horizontal text on one line if the text is selected.

<TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:singleLine="true"
     android:ellipsize="end"
     android:scrollHorizontally="true" />

Instead, this won't work:

<TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:maxLines="1"
     android:ellipsize="end"
     android:scrollHorizontally="true" />

Always define input type for single line

ex : inputType="text"

You don't need to do anything else.

Following a commment of Juan José Melero Gómez, I would like to add more information:

If you are attempting to set an imeAction to your EditText such as actionSearch, setting android:maxLines="1" won't be enough. Adding android:inputType="text" is mandatory if you want to see the search icon.

<EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:inputType="text"
      android:maxLines="1"
      />

use android:inputType="text" and android:maxLines="1" togather

Needed replace android:singleLine="true" to android:maxLines="1" if you want indicate imeOptions needed add this line android:inputType="number|text ..."

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!