Android文本字段:设置焦点+;以编程方式进行软输入

Android文本字段:设置焦点+;以编程方式进行软输入,android,android-edittext,Android,Android Edittext,在我看来,我有一个search EditText,我希望通过编程触发字段上单击事件的行为,即将焦点放在文本字段上,并在必要时显示软键盘(如果没有可用的硬键盘) 我尝试了field.requestFocus()。该字段实际上会获得焦点,但不会显示软键盘 我尝试了field.performClick()。但这只调用字段的onclick侦听器 有什么想法吗?好的先生,试试这个: edittext.setFocusableInTouchMode(true); edittext.requestFocus(

在我看来,我有一个search EditText,我希望通过编程触发字段上单击事件的行为,即将焦点放在文本字段上,并在必要时显示软键盘(如果没有可用的硬键盘)

我尝试了
field.requestFocus()
。该字段实际上会获得焦点,但不会显示软键盘

我尝试了
field.performClick()
。但这只调用字段的onclick侦听器

有什么想法吗?

好的先生,试试这个:

edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
我不确定,但某些手机(某些较旧的设备)可能需要这样做:


这是为我工作的代码

edittext.post(new Runnable() {
    public void run() {
        edittext.requestFocusFromTouch();
        InputMethodManager lManager = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); 
        lManager.showSoftInput(edittext, 0);
    }
});
就这样!享受;)

在其他两个答案对我无效之后,以下代码对我有效:

@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
}
成功输入后,我还要确保隐藏键盘

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()

EDIT2:在对话框(
DialogFragment
)中,使用以下命令

getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
而不是

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

在我的例子中,我想显示虚拟键盘而不引用特定的文本框,因此我使用了公认答案的第一部分来聚焦:

edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
然后我显示虚拟键盘

edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

就我而言,这解决了所有问题:

val inputMethodManager: InputMethodManager = 
   context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT)
我把它放在RecyclerView适配器中,因为我使用数据绑定。 此外,我还没有使用
edittext.setFocusableInTouchMode(true)
,因为在我的布局中,默认情况下它是
true

不要使用这一行:
edittext.requestFocus()
。当我删除它时,它开始工作:)

对于kotlin,使用如下扩展函数实现

fun View.showSoftKeyboard(){
this.requestFocus()
val imm=context.getSystemService(context.INPUT\u METHOD\u SERVICE)作为InputMethodManager
imm.showSoftInput(这是InputMethodManager.SHOW\u隐式)
}

与requestFocus()的结果相同。。。字段获得焦点,但软键盘没有触发。我最终通过调用活动的
onResume()
方法(而不是
onCreate()
)中的
Field.requestFocus()
解决了问题。我不知道它为什么会工作…视图在实际显示在屏幕上之前无法对焦。当onCreate()持有UI线程时,无法执行此操作,因此视图直接在onCreate()之后和onResume()之前布局@fiddler它在
onResume
中工作,因为
onResume
是在
onCreate
之后调用的,所以
字段在运行
onCreate
之前不存在。看这里:很好的建议,老伙计。我声明;这是值得投票表决的!谢谢,这个对我有用。当用户点击按钮时,我以编程方式添加
EditText
。我从对话框模式添加了
EditText
。Android正在设置一些标志,导致该控件无法接收任何触摸。如果在runnable中添加该字段,则不会设置标志。看起来Android正在将
EditText
InputType
设置为null。如果您设置了
InputType
,它应该可以工作。唯一适合我的解决方案(如果我使用InputMethodManager显示键盘强制,那么我无法隐藏它)其他方法都不适合我,但这个方法确实适用。我希望有一个框供用户点击打开编辑对话框视图,而不是有一个编辑行来点击编辑文本视图。在手机上,编辑行失去了大部分功能,因为编辑对话视图覆盖了整个屏幕,因此用户在键入时无法看到编辑行,包括其中的字母。
requestFocusFromTouch()
似乎会触发启动器上的触摸事件。这很奇怪。
val inputMethodManager: InputMethodManager = 
   context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT)