可能重复:android- singlline -true-not-working-for edittext

<EditText 
    android:id="@+id/searchbox"  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:lines="1"
    android:scrollHorizontally="true"
    android:ellipsize="end"
    android:layout_weight="1"
    android:layout_marginTop="2dp"
    android:drawablePadding="10dp"
    android:background="@drawable/edittext"
    android:drawableLeft="@drawable/folder_full"
    android:drawableRight="@drawable/search"
    android:paddingLeft="15dp"
    android:hint="search...">
</EditText>

我想使上面的EditText只有单行。即使用户按下“enter”键,光标也不能下到第二行。有人能帮我一下吗?


当前回答

使用android:maxLines="1"和android:inputType="text"

你忘记了android:maxLines属性。在你的例子中,下面会给出这样的结果:

<EditText 
    android:id="@+id/searchbox"  
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:inputType="text"
    android:scrollHorizontally="true"
    android:ellipsize="end"
    android:layout_weight="1"
    android:layout_marginTop="2dp"
    android:drawablePadding="10dp"
    android:background="@drawable/edittext"
    android:drawableLeft="@drawable/folder_full"
    android:drawableRight="@drawable/search"
    android:paddingLeft="15dp"
    android:hint="search...">
</EditText>

其他回答

对以上所有内容再做一个评论。如果你需要数值输入类型,只需使用inputType="number"。但如果你将使用android:digits="0123456789",你无法实现你需要的任何属性的效果,如lines, maxLines, inputType

请试试这段代码

android:inputType="textPersonName"
android:imeOptions="actionDone"

为我工作。

您必须在xml中的EditText中添加这一行:

android:maxLines="1"

编程:

textView.setMaxLines(1);