Android 将编辑文本限制为单行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10978038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:38:18  来源:igfitidea点击:

restrict edittext to single line

androidandroid-edittext

提问by Pankaj Singhal

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 EditTextto have only single line. Even if the user presses "enter" the cursor should not get down to the second line. Can anybody help me doing that?

我想让上面EditText只有一行。即使用户按下“回车”,光标也不应该到第二行。有人可以帮我这样做吗?

回答by Praveenkumar

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

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

You forgot the android:maxLinesattribute. And refer for android:inputTypeWith your example, below will give this result:

你忘记了android:maxLines属性。并参考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>

回答by ralphgabb

Using android:singleLine="true"is deprecated.

使用android:singleLine="true"已被弃用。

Just add your input type and set maxline to 1 and everything will work fine

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

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

回答by Mr. Bungle

android:singleLineis now deprecated. From the documentation:

android:singleLine现在已弃用。从文档

This constant was deprecated in API level 3.

This attribute is deprecated. Use maxLinesinstead to change the layout of a static text, and use the textMultiLineflag in the inputType attribute instead for editable text views (if both singleLine and inputType are supplied, the inputType flags will override the value of singleLine).

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

此属性已弃用。maxLines改为使用来更改静态文本的布局,并使用textMultiLineinputType 属性中的标志代替可编辑文本视图(如果同时提供 singleLine 和 inputType,则 inputType 标志将覆盖 singleLine 的值)。

So you could just set android:inputType="textEmailSubject"or any other value that matches the content of the field.

所以你可以只设置android:inputType="textEmailSubject"或任何其他与字段内容匹配的值。

回答by UGD

You must add this line in your EditText in xml:

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

android:maxLines="1"

回答by ChrisR

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

Add the above code to have a single line in EditText tag in your layout.

添加上面的代码以在您的布局中的 EditText 标记中包含一行。

android:singleLine="true" is deprecated

This constant was deprecatedin API level 3.

This attribute is deprecated. Use maxLines instead to change the layout of a static text, and use the textMultiLine flag in the inputType attribute instead for editable text views (if both singleLine and inputType are supplied, the inputType flags will override the value of singleLine).

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

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

回答by Kaushik

Now android:singleLineattribute is deprecated. Please add these attributes to your EditTextfor an EditTextto be single line.

现在android:singleLine不推荐使用属性。请将这些属性添加到您EditTextEditText单行中。

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

回答by Ray Hunter

This will work for the EditText:

这将适用于 EditText:

android:inputType="text"

Then I would set a max length for the input text:

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

android:maxLines="1"

Those are the 2 that are needed now.

这就是现在需要的2个。

回答by Aleks G

I have done this in the past with android:singleLine="true"and then adding a listener for the "enter" key:

我过去曾这样做过android:singleLine="true",然后为“输入”键添加了一个侦听器:

((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;
    }
});

回答by Vipul Shah

Include android:singleLine="true"

包括 android:singleLine="true"

回答by Peter Griffin

Everyone's showing the XML way, except one person showed calling EditText's setMaxLines method. However, when I did that, it didn't work. One thing that did work for me was setting the input type.

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

EditText editText = new EditText(this);
editText.setInputType(InputType.TYPE_CLASS_TEXT);

This allows A-Z, a-z, 0-9, and special characters, but does not allow enter to be pressed. When you press enter, it'll go to the next GUI component, if applicable in your application.

这允许使用 AZ、az、0-9 和特殊字符,但不允许按 Enter。当您按 Enter 键时,它将转到下一个 GUI 组件(如果在您的应用程序中适用)。

You may also want to set the maximum number of characters that can be put into that EditText, or else it'll push whatever's to the right of it off the screen, or just start trailing off the screen itself. You can do this like this:

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

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

This sets the max characters to 8 in that EditText. Hope all this helps you.

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