【发布时间】:2010-12-27 13:04:52
【问题描述】:
当用户点击EditView时,Android会打开键盘以便用户在EditView中书写。
问题是,当用户写完后,没有办法隐藏键盘。用户必须按后退按钮才能隐藏键盘。
有没有办法在键盘上显示Done 按钮来隐藏键盘?
【问题讨论】:
当用户点击EditView时,Android会打开键盘以便用户在EditView中书写。
问题是,当用户写完后,没有办法隐藏键盘。用户必须按后退按钮才能隐藏键盘。
有没有办法在键盘上显示Done 按钮来隐藏键盘?
【问题讨论】:
首先,您需要为您的目标 EditText 设置 android:imeOptions 属性等于 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"
android:singleLine="true"
/>
【讨论】:
android:singleLine="true" 才能通过 xml 工作
使用TextView.setImeOptions 并将其传递给actionDone。
喜欢textView.setImeOptions(EditorInfo.IME_ACTION_DONE);
【讨论】:
textView.setImeOptions(EditorInfo.IME_ACTION_DONE);
textView.singleLine(true) 才能让它以编程方式工作。
包括both imeOptions 和 singleLine:
<EditText
android:id="@+id/edittext_done"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
/>
【讨论】:
android:imeActionLabel="Done"
android:singleLine="true"
在 XML 文件中工作得很好。但这也会导致editText 继续输入您可能不想要的单行。因此,在您的代码中添加以下内容将确保您最终不会在一行中输入所有内容。
mainText.setHorizontallyScrolling(false);
mainText.setMaxLines("Maximum integer value that you want to provide");
【讨论】:
用于完成按钮
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
和
android:inputType="text" 在 xml 中
用于处理从键盘点击完成
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;
}
});
`
【讨论】:
使用这个:
android:singleLine="true"
【讨论】:
actionDone 也加入其中,以防某些设备反应不同。
使用这两行到你的EditText
android:imeActionLabel="Done"
android:singleLine="true"
或者您可以通过这一行以编程方式实现它。
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
【讨论】:
用途:
android:imeActionLabel="Done"
android:singleLine="true"
【讨论】:
如果小部件的属性没有改变,最好使用 like
android:imeOptions="actionDone" 在布局 xml 文件中。
【讨论】:
我必须指出这一点,因为很多人可能会在不知道问题的情况下陷入困境。
如果您希望在单击 Done 时隐藏 kb,并且设置 android:imeOptions="actionDone" 和 android:maxLines="1" 而不设置 EditText inputType,它将不 EditText 的默认 inputType 不是很多人认为的 "text"。
所以,只设置inputType 会给你想要的结果whatever你设置它喜欢"text","number",...等等。
【讨论】:
在 Kotlin 中处理隐藏键盘 + 完成操作的直接方法是:
// 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
}
使用它在您的主代码中调用edittext.onDone {/*action*/}。使其更具可读性和可维护性
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
}
}
如果您想要更多简化键盘操作的方法(显示、关闭、聚焦):Read this post
不要忘记将这些选项添加到您的 edittext Xml(如果不在代码中)
<EditText ...
android:imeOptions="actionDone"
android:inputType="text"/>
需要
inputType="textMultiLine"支持吗? Read this post 并且不要在 Xml 中添加imeOptions或inputType
【讨论】:
代码:
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
【讨论】:
ActionDone 是在点击键盘上的下一个按钮时使用的 键盘是隐藏的。在编辑文本或 AppcompatEdit 中使用
XML
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 如果你使用 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);
【讨论】:
实际上,您可以为那个蓝色小按钮设置自定义文本。在 xml 文件中只需使用
android:imeActionLabel="whatever"
在您的 EditText 上。
或者在java文件中使用
etEditText.setImeActionLabel("whatever", EditorInfo.IME_ACTION_DONE);
我随意选择IME_ACTION_DONE作为这个函数的第二个参数应该去的例子。可以在here 找到这些操作的完整列表。
需要注意的是,这不会导致文本出现在所有设备的所有键盘上。某些键盘不支持该按钮上的文本(例如 swiftkey)。有些设备也不支持它。一个好的规则是,如果您看到按钮上已有文本,这会将其更改为您想要的任何内容。
【讨论】:
如果你正在使用
android:imeOptions="actionDone"
那么你必须使用
android:inputType="text"
那么只有您可以在键盘中看到“操作完成”按钮。
【讨论】:
在你的视图中使用它
<EditText
....
....
android:imeOptions="actionDone"
android:id="@+id/edtName"
/>
【讨论】:
如果您根本不想要任何按钮(例如,您正在为盲人开发一个无法定位且您依赖单/双/长点击的 GUI):
text.setItemOptions(EditorInfo.IME_ACTION_NONE)
或者在 Kotlin 中:
text.imeOptions = EditorInfo.IME_ACTION_NONE
【讨论】: