可能重复: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”键,光标也不能下到第二行。有人能帮我一下吗?


当前回答

编程:

textView.setMaxLines(1);

其他回答

包括android:单行模式= " true "

使用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>

将这一行添加到编辑文本中

android: inputType =“文本”

这将适用于EditText:

android:inputType="text"

然后我将为输入文本设置一个最大长度:

android:maxLines="1"

这是现在需要的两个。

不建议使用android:singleLine="true"。

只需添加您的输入类型并将maxline设置为1,一切都会正常工作

android:inputType="text"
android:maxLines="1"