700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > android编辑框单行 android - 将edittext限制为单行

android编辑框单行 android - 将edittext限制为单行

时间:2019-01-19 15:21:56

相关推荐

android编辑框单行 android  - 将edittext限制为单行

android - 将edittext限制为单行

可能重复:android-singleline-true-not-working-for-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只有单行。 即使用户按下“输入”,光标也不应该下到第二行。 任何人都可以帮我这样做吗?

21个解决方案

457 votes

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

你忘了android:maxLines属性。 并参考android:inputType在您的示例中,下面将给出以下结果:

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...">

Praveenkumar answered -02-17T22:53:10Z

167 votes

不推荐使用android:singleLine="true"。

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

android:inputType="text"

android:maxLines="1"

ralphgabb answered -02-17T22:53:40Z

51 votes

android:inputType="textEmailSubject"现已弃用。 从文档:

此常量在API级别3中已弃用。

不推荐使用此属性。 而是使用android:inputType="textEmailSubject"来更改静态文本的布局,并使用inputType属性中的textMultiLine标志来代替可编辑的文本视图(如果提供了singleLine和inputType,则inputType标志将覆盖singleLine的值)。

因此,您可以设置android:inputType="textEmailSubject"或与该字段内容匹配的任何其他值。

Mr. Bungle answered -02-17T22:54:25Z

17 votes

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

android:maxLines="1"

UGD answered -02-17T22:54:49Z

14 votes

android:maxLines="1"

android:inputType="text"

添加上面的代码,在您的布局中的EditText标记中有一行。

android:singleLine="true" is deprecated

此常量在API级别3中已弃用。

不推荐使用此属性。 请改用maxLines来改变 布局静态文本,并使用textMultiLine标志 inputType属性代替可编辑的文本视图(如果两者都有 提供singleLine和inputType,inputType标志将 覆盖singleLine的值)。

ChrisR answered -02-17T22:55:22Z

12 votes

现在不推荐使用android:singleLine属性。 请将这些属性添加到EditText以获取EditText作为单行。

android:inputType="text"

android:imeOptions="actionNext"

android:maxLines="1"

Kaushik answered -02-17T22:55:48Z

11 votes

我在过去使用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;

}

});

Aleks G answered -02-17T22:56:14Z

11 votes

这适用于EditText:

android:inputType="text"

然后易为输入Fixed设置最大长度:

android:maxLines="1"

这些是现在需要的2。

Ray Hunter answered -02-17T22:56:52Z

6 votes

包括android:singleLine="true"

Vipul Shah answered -02-17T22:57:18Z

5 votes

每个人都显示XML方式,除了一个人显示调用EditText的setMaxLines方法。 但是,当我这样做时,它没有用。 对我有用的一件事是设置输入类型。

EditText editText = new EditText(this);

editText.setInputType(InputType.TYPE_CLASS_TEXT);

这允许A-Z,a-z,0-9和特殊字符,但不允许输入被按下。 当您按Enter键时,它将转到下一个GUI组件(如果适用于您的应用程序)。

您可能还想设置可以放入EditText的最大字符数,否则它会将其右侧的任何字符推出屏幕,或者只是开始拖尾屏幕本身。 你可以这样做:

InputFilter[] filters = new InputFilter[1];

filters[0] = new InputFilter.LengthFilter(8);

editText.setFilters(filters);

这会将EditText中的最大字符数设置为8。 希望这一切对你有所帮助。

Peter Griffin answered -02-17T22:58:04Z

2 votes

为了限制你只需要在“true”上设置单行选项。

android:singleLine="true"

user3469914 answered -02-17T22:58:30Z

2 votes

编程方式:

textView.setMaxLines(1);

Anton Duzenko answered -02-17T22:58:56Z

2 votes

使用

android:ellipsize="end"

android:maxLines="1"

Raghu Nagaraju answered -02-17T22:59:17Z

1 votes

使用下面的代码代替您的代码

android:id="@+id/searchbox"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:lines="1"

android:singleLine="true"

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..."/>

Dipak Keshariya answered -02-17T22:59:43Z

1 votes

@Aleks G OnKeyListener()工作得很好,但是我从MainActivity运行它,所以不得不稍微修改它:

EditText searchBox = (EditText) findViewById(R.id.searchbox);

searchBox.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) MainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(searchBox.getApplicationWindowToken(), 0);

//do what you need on your enter key press here

return true;

}

return false;

}

});

我希望这有助于任何人尝试这样做。

Mark Cramer answered -02-17T23:00:15Z

1 votes

如果属性android:inputType是textMultiLine,那么注意也很重要。 如果是这样,属性android:singleLine,android:imeOptions,android:ellipsize和android maxLines将被忽略,你的EditText仍会接受MultiLines。

Thales Valias answered -02-17T23:00:41Z

1 votes

将此行添加到您的edittext中

安卓的inputType=“文本”

Sagar Devanga answered -02-17T23:01:14Z

1 votes

由于android:singleLine="true"现已弃用,所以使用

机器人:MAXLINES= “1”

使用maxLines来改变静态文本的布局,并使用inputType属性中的textMultiLine标志代替可编辑的文本视图(如果提供了singleLine和inputType,则inputType标志将覆盖singleLine的值)。

Maveňツ answered -02-17T23:01:49Z

1 votes

请试试这段代码

android:inputType="textPersonName"

TheLastSummer answered -02-17T23:02:15Z

0 votes

在XML文件中,在“编辑文本”中添加此属性

android:maxLines="1"

yousef answered -02-17T23:02:41Z

0 votes

android:imeOptions="actionDone"

为我工作。

Bikram Pandit answered -02-17T23:03:02Z

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。