ChatGPT解决这个技术问题 Extra ChatGPT

How do I make an Android EditView 'Done' button and hide the keyboard when clicked?

When the user clicks on the EditView, Android opens the keyboard so that user can write in the EditView.

The problem is, when the user is done writing, there is no way to hide the keyboard. The user has to press the back button to hide the keyboard.

Is there a way to display a Done button on the keyboard that will hide the keyboard?


s
smrf

First you need to set the android:imeOptions attribute equal to actionDone for your target EditText as seen below. That will change your ‘RETURN’ button in your EditText’s soft keyboard to a ‘DONE’ button.

<EditText 
    android:id="@+id/edittext_done"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Enter some text"
    android:imeOptions="actionDone"
    android:singleLine="true"
    />

@Michael removed that link. Thanks
I had to add android:singleLine="true" to get this to work via xml
android:singleLine is deprecated. Use android:maxLines="1".
Yes @FariborZ singleLine is deprecated now.
Important note: singleLine is not the same as maxLines. This misconception causes a lot of issues for everyone, we need to note that everywhere. stackoverflow.com/questions/30879471/…
i
insomniac

Use TextView.setImeOptions and pass it actionDone. like textView.setImeOptions(EditorInfo.IME_ACTION_DONE);


textView.setImeOptions(EditorInfo.IME_ACTION_DONE);
I had to add textView.singleLine(true) to get this to work programmatically.
but this is deprecated since android 2.3
B
Bartek Lipinski

Include both imeOptions and singleLine:

<EditText 
   android:id="@+id/edittext_done"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:imeOptions="actionDone"
   android:singleLine="true"
   />

G
Garg's
android:imeActionLabel="Done" 
android:singleLine="true"

In the XML file works just fine. But this will also cause the editText to keep typing in a single line which you may not want. So adding following to your code will make sure that you won't end up typing everything on a single line.

mainText.setHorizontallyScrolling(false);
mainText.setMaxLines("Maximum integer value that you want to provide");

Thanks, Demo was on 12 , time was 11:58 and you fixed my issue on 11:59 :p
android:singleLine = "true" has been deprecated. So use, android:lines="1"
G
Günter Zöchbauer

For getting the done Button

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

and

android:inputType="text" in the xml

For handling on done clicked from keyboard

    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event){
            if(actionId == EditorInfo.IME_ACTION_DONE){
                // Your action on done
                return true;
            }
            return false;
        }
    });

`


D
Deepzz

Use this:

android:singleLine="true"

Yes...otherwise how is one going to distinguish going to the next line versus closing the keyboard?
This is what ended up working for me. I decided to error with having actionDone in there as well, just in case some devices react differently.
N
NarenderNishad

Use These two lines to your EditText

android:imeActionLabel="Done"
android:singleLine="true"

or you can achieve it Programmatically by this line.

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

A
Andrey Korneyev

Use:

android:imeActionLabel="Done"
android:singleLine="true" 

H
Hardik Parmar

If the property does not change for the widget it may be better to use like android:imeOptions="actionDone" in the layout xml file.


M
Muhammed Refaat

I have to point that out as a lot of people can struggle into that without knowing the problem.

If you want the kb to hide when clicking Done, and you set android:imeOptions="actionDone" & android:maxLines="1" without setting your EditText inputType it will NOT work as the default inputType for the EditText is not "text" as a lot of people think.

so, setting only inputType will give you the results you desire whatever what you are setting it to like "text", "number", ...etc.


C
Community

Kotlin Solution

The direct way to handle the hide keyboard + done action in Kotlin is:

// Set action
edittext.setOnEditorActionListener { _, actionId, _ ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        // Hide Keyboard
        val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
        true
    }
    false
}

Kotlin Extension

Use this to call edittext.onDone {/*action*/} in your main code. Keeps it more readable and maintainable

edittext.onDone { edittext.hideKeyboard() }

fun View.hideKeyboard() {
    val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}

fun EditText.onDone(callback: () -> Unit) {
    // These lines optional if you don't want to set in Xml
    imeOptions = EditorInfo.IME_ACTION_DONE
    maxLines = 1
    setOnEditorActionListener { _, actionId, _ ->
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            callback.invoke()
            true
        }
        false
    }
}

Additional Keyboard Extensions

If you'd like more ways to simplify working with the keyboard (show, close, focus): Read this post

Don't forget to add these options to your edittext Xml, if not in code

<EditText ...
    android:imeOptions="actionDone"
    android:inputType="text"/>

Need inputType="textMultiLine" support? Read this post and don't add imeOptions or inputType in Xml


D
Deepzz

For the code:

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

R
Raj

ActionDone is use when click in next button in the keyboard that time keyboard is hide.Use in Edit Text or AppcompatEdit

XML

1.1 If you use AppCompatEdittext

    <android.support.v7.widget.AppCompatEditText
    android:id="@+id/edittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:imeOptions="actionDone"/>

1.2 If you use Edittext

    <EditText
    android:id="@+id/edittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:imeOptions="actionDone"/>

JAVA

EditText edittext= (EditText) findViewById(R.id.edittext);
edittext.setImeOptions(EditorInfo.IME_ACTION_DONE);

Z
Zar E Ahmer

Actually you can set custom text to that little blue button. In the xml file just use

android:imeActionLabel="whatever"

on your EditText.

Or in the java file use

etEditText.setImeActionLabel("whatever", EditorInfo.IME_ACTION_DONE);

I arbitrarily choose IME_ACTION_DONE as an example of what should go in the second parameter for this function. A full list of these actions can be found here.

It should be noted that this will not cause text to appear on all keyboards on all devices. Some keyboards do not support text on that button (e.g. swiftkey). And some devices don't support it either. A good rule is, if you see text already on the button, this will change it to whatever you'd want.


A
Aditya Patil

If you are using

android:imeOptions="actionDone" 

then you must use

android:inputType="text"

then only you can see Action Done button in Keyboard.


M
Marium Jawed

use this in your view

<EditText 
    ....
    ....
    android:imeOptions="actionDone" 
    android:id="@+id/edtName"
    />

A
Azametzin

If you don't want any button at all (e.g. you are developing a GUI for blind people where tap cannot be positional and you rely on single/double/long taps):

text.setItemOptions(EditorInfo.IME_ACTION_NONE)

Or in Kotlin:

text.imeOptions = EditorInfo.IME_ACTION_NONE