我有一个EditText,它的ImeOptions设置为EditorInfo.IME_ACTION_NEXT。因此,当字段被聚焦时,键盘上会显示“下一步”按钮。我希望在用户键入时,按钮变为“完成”(出于某些原因)。所以我有一个TextWatcher,我试着在"afterTextChanged“上将ImeOptions改为EditorInfo.IME_ACTION_DONE,但是键盘上的键没有改变。我试图隐藏键盘,更改ImeOptions,然后再次显示键盘,但不起作用(此解决方案适用于iOS)。
有人知道怎么做吗?
发布于 2016-05-23 20:07:00
我试过了,它起作用了,问题是你有时可以看到键盘的出现和消失。
@Override
public void afterTextChanged(Editable s) {
if (/*condition*/) {
// Change the IME of the current focused EditText
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
// Hide the keyboard
hideKeyboard(activity);
// Restart the input on the current focused EditText
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.restartInput(editText);
// Show the keyboard
showKeyboard(activity);
}
}
发布于 2016-05-23 20:10:08
您可以尝试这样的操作:
if (your condition) {
youreditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
hideKeyboard(activity);
InputMethodManager input= (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
input.restartInput(youreditText);
showKeyboard(youractivity);
}
发布于 2020-07-07 21:11:03
根据上面的答案:
// Restart the input on the current focused EditText
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.restartInput(editText);
只有重新启动输入才能起作用。否则,可能会出现键盘闪烁。输入重新启动也不会清除字段的内容,即不会导致数据丢失,这是所需的行为。
https://stackoverflow.com/questions/37351052
复制相似问题