本文翻译自:restrict edittext to single line
possible duplicate : android-singleline-true-not-working-for-edittext 可能重复: android-singleline-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>
I want to make the above EditText
to have only single line. 我想使上面的EditText
只包含一行。 Even if the user presses "enter" the cursor should not get down to the second line. 即使用户按下“输入”,光标也不应下降到第二行。 Can anybody help me doing that? 有人可以帮我吗?
#1楼
参考:https://stackoom.com/question/K3T8/将edittext限制为单行
#2楼
包括android:singleLine="true"
#3楼
Use android:maxLines="1"
and android:inputType="text"
使用android:maxLines="1"
和android:inputType="text"
You forgot the android:maxLines attribute. 您忘记了android:maxLines属性。 And refer for android:inputType With your example, below will give this result: 并参考android:inputType与您的示例,下面将给出此结果:
<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>
#4楼
I have done this in the past with android:singleLine="true"
and then adding a listener for the "enter" key: 我过去使用android:singleLine="true"
完成此操作,然后为“ enter”键添加了一个侦听器:
((EditText)this.findViewById(R.id.mytext)).setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
//if the enter key was pressed, then hide the keyboard and do whatever needs doing.
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0);
//do what you need on your enter key press here
return true;
}
return false;
}
});
#5楼
use 采用
android:ellipsize="end"
android:maxLines="1"
#6楼
Use Below Code instead of your code 使用以下代码代替您的代码
<EditText
android:id="@+id/searchbox"
android:layout_width="fill_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..."/>