Android TextField:以编程方式设置焦点+软输入

sda*_*bet 75 android android-edittext

在我看来,我有一个搜索EditText,我想以编程方式触发字段上的单击事件的行为,即将焦点放在文本字段上并在必要时显示软键盘(如果没有可用的硬键盘).

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

我试过了field.performClick().但那只调用该字段的OnClickListener.

任何的想法 ?

pgs*_*rom 143

好先生,试试这个:

edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
Run Code Online (Sandbox Code Playgroud)

我不确定,但某些手机(某些旧设备)可能需要这样做:

final InputMethodManager inputMethodManager = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT);
Run Code Online (Sandbox Code Playgroud)

  • 视图在实际布局在屏幕上之前无法获得焦点.当onCreate()保存UI线程时,无法完成此操作,因此视图在onCreate()之后和onResume()之前直接布局.:) (29认同)
  • 我终于通过在活动的`onResume()`方法中调用`field.requestFocus()`来解决了问题(而不是`onCreate()`).不知道它为什么有效...... (10认同)
  • 优秀的建议,小伙子.我确实声明了; 这是值得的一个upvote! (3认同)
  • 与requestFocus()相同的结果......字段获得焦点但不触发软键盘. (2认同)

小智 55

这是适合我的代码.

edittext.post(new Runnable() {
    public void run() {
        edittext.requestFocusFromTouch();
        InputMethodManager lManager = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); 
        lManager.showSoftInput(edittext, 0);
    }
});
Run Code Online (Sandbox Code Playgroud)

而已!请享用 ;)

  • 谢谢,这个对我有用。当用户点击按钮时,我以编程方式添加“EditText”。 (2认同)

Epi*_*rce 6

下面的代码 为我工作,后其他两个答案 ,我没有工作

@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
}
Run Code Online (Sandbox Code Playgroud)

哪里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);
    }
}
Run Code Online (Sandbox Code Playgroud)

输入成功后,我还要确保隐藏键盘

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(getView().getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)

从技术上讲,我只是在运行软键盘显示请求之前增加了300 ms的延迟。奇怪吧?也更改requestFocus()requestFocusFromTouch()

编辑:不要使用requestFocusFromTouch()它给启动器提供触摸事件。坚持下去requestFocus()

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

getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Run Code Online (Sandbox Code Playgroud)

代替

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Run Code Online (Sandbox Code Playgroud)