将edittext限制为单行

本文探讨了如何在Android中将EditText控件限制为仅接受单行输入,即使用户按下'enter'键,光标也不会换行。提到了可能的解决方案,如设置android:singleLine属性、使用android:maxLines属性,以及监听'enter'键来防止换行。
摘要由CSDN通过智能技术生成

本文翻译自: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..."/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值