android: singleline = true not working for edittex

2019-04-18 14:29发布

I want to restrict the edittext entry to a single line but When I add the line android:singleline = "true", the hint is disappears, and after entering 26 characters the cursor is coming down.

Here's my XML code:

   <EditText                      
            android:background="@drawable/edittxtborder"
            android:id="@+id/reglastname"
            android:singleLine="true"
            android:gravity="center"
            android:hint="@string/reglname"
            android:textSize="12dp" 
   />

6条回答
Summer. ? 凉城
2楼-- · 2019-04-18 15:06

You can use setMaxLines(int) to change line numbers programmaticaly.

EditText edit_field = new EditText(this);
edit_field.setMaxLines(1);
查看更多
Summer. ? 凉城
3楼-- · 2019-04-18 15:07

You can edit the button in the code directly by setting its input type to TYPE_CLASS_TEXT. This will allow all kinds of characters, numbers, and space to be entered in, but no enter. So in effect it will restrict it to one line.

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

You should also restrict the maximum amount of characters that can be entered into the EditText. This can also be done through code using an InputFilter and calling setFilters method of the EditText with the InputFilter.

查看更多
Animai°情兽
4楼-- · 2019-04-18 15:08

add this code in your edittext xml code.

android:ellipsize="end"

this will work.

查看更多
Deceive 欺骗
5楼-- · 2019-04-18 15:08

I checked your code and the problem is with android:gravity="center". Change it to android:gravity="left" and you will be able to see the hint.

查看更多
时光不老,我们不散
6楼-- · 2019-04-18 15:14

Use value for the attribute inputType other than textMultiLine. The input will be single line. The attribute singleLine is deprecated.

查看更多
beautiful°
7楼-- · 2019-04-18 15:24

Please use

android:maxLines="1"

instead of

android:singleLine="true" due to deprecate
查看更多
登录 后发表回答