問題描述
在我看來,我有一個搜索 EditText,我想以編程方式觸發(fā)該字段上單擊事件的行為,即,將焦點放在文本字段上,并在必要時顯示軟鍵盤(如果沒有可用的硬鍵盤).
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()
.但這只會調(diào)用該字段的 OnClickListener.
I tried field.performClick()
. But that only calls the OnClickListener of the field.
有什么想法嗎?
推薦答案
好先生,試試這個:
edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
我不確定,但某些手機(某些舊設(shè)備)可能需要這樣做:
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);
這篇關(guān)于Android TextField:以編程方式設(shè)置焦點+軟輸入的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!