Android Edittext question, replace keypad enter wi

2020-02-06 02:47发布

I'm defining an edit text like so...

<EditText
android:id="@+id/user"
android:layout_width="200px"
android:layout_height="wrap_content"
android:text="Username"
android:textSize="18sp"
android:gravity="center"
android:layout_x="64px"
android:layout_y="140px"
>

I'd like to replace the "enter" button that appears on the keypad automatically, with a done button that would just close the keypad when the user hits it. I've seen this done on a couple of apps (e.g the Nexus One twitter app) and would like to implement something similar. does anyone know how?

Also is it possible to get the text that appears by default to auto erase when the user click to edit the edittext box? Thanks,

标签: xml android
5条回答
成全新的幸福
2楼-- · 2020-02-06 03:32

Here is an updated answer:

android:maxLines="1"

The accepted answer of android:singleLine="true" has been deprecated.

查看更多
Ridiculous、
3楼-- · 2020-02-06 03:36

Simple solution: add android:singleLine="true" for the EditText. This will replace the enter with Next (for all edittexts except the last one) and Done (for the last one).

查看更多
神经病院院长
4楼-- · 2020-02-06 03:44

About your other question, getting the text to auto-erase, you should include it in android:hint instead of android:text.

查看更多
够拽才男人
5楼-- · 2020-02-06 03:45
android:imeOptions="actionNext"

It replaces the name of the enter key with "Next" in the virtual keyboard and by clicking on it - focus jumps to the next field

In your layout, you would do things like this

<EditText
    ...
    android:text="Field 1"
    android:imeOptions="actionNext" />
<EditText
    ...
    android:text="Field 2"
    android:imeOptions="actionNext" />

And in Java

TextView field2 = (TextView) field1.focusSearch(View.FOCUS_RIGHT);
field2.requestFocus();

It's up to you to decide which field will request focus next.

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2020-02-06 03:50

Just add in the EditText XML : android:imeOptions="actionDone"

查看更多
登录 后发表回答