如何制作一个Android EditView 'Done'按钮,并在点击时隐藏键盘?

时间:2022-11-21 20:43:58

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

当用户单击EditView时,Android将打开键盘,以便用户可以在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?

有没有办法在键盘上显示一个“完成”按钮来隐藏键盘?

13 个解决方案

#1


107  

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

使用TextView。setImeOptions并将其通过actionDone。像textView.setImeOptions(EditorInfo.IME_ACTION_DONE);

#2


125  

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.

首先,需要将android:imeOptions属性设置为目标EditText的actionDone,如下所示。这将把EditText的软键盘上的“返回”按钮改为“完成”按钮。

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

#3


74  

Include both imeOptions and singleLine:

包括imeOptions和singleLine:

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

#4


26  

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.

在XML文件中工作正常。但是这也会导致editText继续输入一个您可能不想要的行。因此,在代码中添加follow将确保不会在一行中输入所有内容。

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

#5


13  

Use this:

用这个:

android:singleLine="true"

#6


8  

Use These two lines to your EditText

将这两行代码用于EditText

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

or you can achieve it Programmatically by this line.

或者您可以通过这行代码来实现它。

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

#7


6  

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

如果小部件的属性没有改变,那么最好使用android:imeOptions="actionDone"在布局xml文件中。

#8


5  

Use:

使用:

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

#9


5  

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.

如果您想要在单击Done时隐藏kb,并且您设置了android:imeOptions=“actionDone”& android:maxLines=“1”,而不设置EditText inputType,那么它将不起作用,因为EditText的默认inputType不是许多人认为的“文本”。

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

所以,只设置inputType会得到你想要的结果,不管你把它设置成“文本”、“数字”等等。

#10


4  

For the code:

的代码:

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

#11


4  

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

ActionDone是当你在键盘上点击next按钮时,键盘被隐藏。用于编辑文本或AppcompatEdit

XML

XML

1.1 If you use AppCompatEdittext

1.1如果您使用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

1.2如果使用Edittext

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

JAVA

JAVA

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

#12


2  

For getting the done Button

完成按钮

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

and

android:inputType="text" in the xml

android:inputType = 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;
        }
    });

`

#13


1  

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

实际上你可以设置自定义文本到那个小蓝色按钮。在xml文件中使用

android:imeActionLabel="whatever"

on your EditText.

EditText。

Or in the java file use

或者在java文件中使用

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.

我任意选择IME_ACTION_DONE作为这个函数的第二个参数的一个例子。这些行为的完整列表可以在这里找到。

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.

需要注意的是,这不会导致文本出现在所有设备的所有键盘上。有些键盘不支持该按钮上的文本(例如swiftkey)。有些设备也不支持它。一个很好的规则是,如果你看到按钮上已经有文字了,这将把它改成你想要的。

#1


107  

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

使用TextView。setImeOptions并将其通过actionDone。像textView.setImeOptions(EditorInfo.IME_ACTION_DONE);

#2


125  

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.

首先,需要将android:imeOptions属性设置为目标EditText的actionDone,如下所示。这将把EditText的软键盘上的“返回”按钮改为“完成”按钮。

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

#3


74  

Include both imeOptions and singleLine:

包括imeOptions和singleLine:

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

#4


26  

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.

在XML文件中工作正常。但是这也会导致editText继续输入一个您可能不想要的行。因此,在代码中添加follow将确保不会在一行中输入所有内容。

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

#5


13  

Use this:

用这个:

android:singleLine="true"

#6


8  

Use These two lines to your EditText

将这两行代码用于EditText

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

or you can achieve it Programmatically by this line.

或者您可以通过这行代码来实现它。

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

#7


6  

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

如果小部件的属性没有改变,那么最好使用android:imeOptions="actionDone"在布局xml文件中。

#8


5  

Use:

使用:

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

#9


5  

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.

如果您想要在单击Done时隐藏kb,并且您设置了android:imeOptions=“actionDone”& android:maxLines=“1”,而不设置EditText inputType,那么它将不起作用,因为EditText的默认inputType不是许多人认为的“文本”。

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

所以,只设置inputType会得到你想要的结果,不管你把它设置成“文本”、“数字”等等。

#10


4  

For the code:

的代码:

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

#11


4  

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

ActionDone是当你在键盘上点击next按钮时,键盘被隐藏。用于编辑文本或AppcompatEdit

XML

XML

1.1 If you use AppCompatEdittext

1.1如果您使用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

1.2如果使用Edittext

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

JAVA

JAVA

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

#12


2  

For getting the done Button

完成按钮

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

and

android:inputType="text" in the xml

android:inputType = 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;
        }
    });

`

#13


1  

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

实际上你可以设置自定义文本到那个小蓝色按钮。在xml文件中使用

android:imeActionLabel="whatever"

on your EditText.

EditText。

Or in the java file use

或者在java文件中使用

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.

我任意选择IME_ACTION_DONE作为这个函数的第二个参数的一个例子。这些行为的完整列表可以在这里找到。

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.

需要注意的是,这不会导致文本出现在所有设备的所有键盘上。有些键盘不支持该按钮上的文本(例如swiftkey)。有些设备也不支持它。一个很好的规则是,如果你看到按钮上已经有文字了,这将把它改成你想要的。