在我的看法中,我有一个搜索EditText,并且我想通过编程方式触发字段上的单击事件的行为,即聚焦于该文本字段并在必要时(如果没有物理键盘)显示软键盘。
我尝试使用field.requestFocus()
。该字段实际上获得了焦点,但软键盘未显示。
我尝试使用field.performClick()
。但这只调用了该字段的OnClickListener。
有什么建议吗?
在我的看法中,我有一个搜索EditText,并且我想通过编程方式触发字段上的单击事件的行为,即聚焦于该文本字段并在必要时(如果没有物理键盘)显示软键盘。
我尝试使用field.requestFocus()
。该字段实际上获得了焦点,但软键盘未显示。
我尝试使用field.performClick()
。但这只调用了该字段的OnClickListener。
有什么建议吗?
尊敬的先生,请试试这个:
edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
我不确定,但是这可能在一些手机上是必需的(一些旧设备):
final InputMethodManager inputMethodManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT);
以下是对我有用的代码。
edittext.post(new Runnable() {
public void run() {
edittext.requestFocusFromTouch();
InputMethodManager lManager = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
lManager.showSoftInput(edittext, 0);
}
});
就这样了!享受吧 ;)
EditText
。 - dumbfingersEditText
。由于 Android 设置了一些标志,导致该控件无法接收任何触摸事件。如果您在可运行的代码块中添加该字段,则不会设置这些标志。 - JeffReganEditText
зЪД InputType
襀 Android иЃЊзљЃдЄЇдЇЖ nullгАВе¶ВжЮЬдљ†иЃЊзљЃ InputType
пЉМеЃГеЇФиѓ•еПѓдї•еЈ•дљЬгАВ - JeffRegan其他两个答案 对我没用,但是以下代码对我有用:
@Override
public void onResume() {
super.onResume();
SingletonBus.INSTANCE.getBus().register(this);
//passwordInput.requestFocus(); <-- that doesn't work
passwordInput.postDelayed(new ShowKeyboard(), 300); //250 sometimes doesn't run if returning from LockScreen
}
ShowKeyboard
是什么
private class ShowKeyboard implements Runnable {
@Override
public void run() {
passwordInput.setFocusableInTouchMode(true);
// passwordInput.requestFocusFromTouch();
passwordInput.requestFocus();
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(passwordInput, 0);
}
}
在成功输入后,我还会确保隐藏键盘。
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(getView().getWindowToken(), 0);
从技术上讲,我只是在运行软键盘显示请求之前增加了300毫秒的延迟。很奇怪,对吧?还将requestFocus()
更改为requestFocusFromTouch()
。
编辑:不要使用requestFocusFromTouch()
,它会给启动器发送一个触摸事件。坚持使用requestFocus()
。
编辑2:在对话框(DialogFragment
)中,请使用以下内容
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
替代
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
requestFocusFromTouch()
似乎会在启动器上触发一个触摸事件。这很奇怪。 - EpicPandaForceedittext.setFocusableInTouchMode(true);
edittext.requestFocus();
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
对于 Kotlin,可以使用扩展函数实现如下:
fun View.showSoftKeyboard() {
this.requestFocus()
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
val mAlertDialog = mBuilder.show()
变量 mAlertDialog = mBuilder.show()
//Coloca o foo no txtDlgQuantidade e então chama o teclado
mDialogView.txtDlgQuantidade.isFocusable = true
mDialogView.txtDlgQuantidade.isFocusableInTouchMode = true
mDialogView.txtDlgQuantidade.requestFocus()
mAlertDialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
mAlertDialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
val inputMethodManager: InputMethodManager =
context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT)
我把它放在RecyclerView Adapter中,因为我使用数据绑定。
另外,我没有使用edittext.setFocusableInTouchMode(true)
,因为在我的布局中,默认情况下是true
。
而且不要使用这一行:edittext.requestFocus()
。当我将其删除时,它开始工作了 :)
field.post(new Runnable() {
@Override
public void run() {
field.requestFocus();
field.onKeyUp(KeyEvent.KEYCODE_DPAD_CENTER, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER));
}
});
onResume()
方法中调用field.requestFocus()
来解决问题,而不是在onCreate()
中。我不太清楚为什么这样做有效... - sdabetonResume
中工作是因为onResume
在onCreate
之后调用,所以field
直到运行onCreate
后才存在。请参见此处:http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle - Randy