问题描述
在我看来,我有一个搜索 EditText,我想以编程方式触发该字段上单击事件的行为,即,将焦点放在文本字段上,并在必要时显示软键盘(如果没有可用的硬键盘).
In my view, I have a search EditText and I would like to trigger programmatically the behaviour of a click event on the field, i.e give focus to the text field AND display soft keyboard if necessary (if no hard keyboard available).
我试过 field.requestFocus()
.该字段实际上获得了焦点,但未显示软键盘.
I tried field.requestFocus()
. The field actually gets focus but soft keyboard is not displayed.
我试过 field.performClick()
.但这只会调用该字段的 OnClickListener.
I tried field.performClick()
. But that only calls the OnClickListener of the field.
有什么想法吗?
推荐答案
好先生,试试这个:
edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
我不确定,但某些手机(某些旧设备)可能需要这样做:
I'm not sure, but this might be required on some phones (some of the older devices):
final InputMethodManager inputMethodManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT);
这篇关于Android TextField:以编程方式设置焦点+软输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!