EditText请求焦点不起作用

Rah*_*hul 25 android android-edittext

我有一个EditText和一个Button.点击按钮我想打开EditText键盘并同时请求重点EditText,以便用户直接开始键入键盘和文本出现在EditText.但在我的情况下,当我单击按钮时,它会打开键盘,但它不会将焦点设置在EditText,因为用户必须EditText再次单击它才能在其上书写.问题是什么 任何帮助或建议.

代码单击按钮

m_SearchEditText.requestFocus();
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(m_SearchEditText.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
Run Code Online (Sandbox Code Playgroud)

Jac*_*ski 36

确保edittext在触摸模式下可聚焦.你可以两种方式做到这一点.

在xml中:

android:focusableInTouchMode="true"
Run Code Online (Sandbox Code Playgroud)

在Java中:

view.setFocusableInTouchMode(true);
Run Code Online (Sandbox Code Playgroud)

就个人而言,我不相信这个参数的XML定义.我总是通过以下两行代码请求关注:

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

键盘应该出现在自身而不需要调用InputMethodManager.

它适用于大多数情况.一旦它对我不起作用,因为由于处理相当繁重而丢失了对象的引用 - 确保这不是你的问题.

  • 我有同样的问题,这对我不起作用。这个问题似乎是一个普遍的问题,似乎没有共同的解决方案。我尝试了其他人建议的一些技巧,但似乎无济于事。 (2认同)
  • 这么低的答案,但创业板!像魅力一样工作! (2认同)

sal*_*hki 14

在我的情况下,它通过在您单击按钮并在另一个视图中聚焦设置后添加处理程序来工作,焦点可以返回到您需要的视图.

把它放在你的代码中:

final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        lastview.getEditText().clearFocus();
                        m_SearchEditText.requestFocus();
                        InputMethodManager mgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);


                        mgr.showSoftInput(mLastNameET, InputMethodManager.SHOW_IMPLICIT);

                    }
                }, 100);
Run Code Online (Sandbox Code Playgroud)

我希望它有所帮助


rid*_*doy 8

manifest.xml中写道:

<activity android:name=".MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateAlwaysVisible" />
Run Code Online (Sandbox Code Playgroud)

并呼吁 m_SearchEditText.requestfocus()oncreate().
或者,
尝试:

if(m_SearchEditText.requestFocus()) {
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
Run Code Online (Sandbox Code Playgroud)


S.A*_*ley 7

以下对我有用,应该有所帮助:

EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
Run Code Online (Sandbox Code Playgroud)


Mat*_*cco 5

作为此答案的扩展(由于声誉,我没有将其添加为评论......)。

如果您想将延迟时间减少到零,请改用 handler.post()。完整代码:

final Handler handler = new Handler();
handler.post(new Runnable() {
    @Override
    public void run() {
        lastview.getEditText().clearFocus();
        m_SearchEditText.requestFocus();
        InputMethodManager mgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);


        mgr.showSoftInput(mLastNameET, InputMethodManager.SHOW_IMPLICIT);
    }
});
Run Code Online (Sandbox Code Playgroud)


met*_*ure 5

极简的 Kotlin 扩展版本,因为我们应该假装这些对系统服务的钝调用是不必要的:

fun EditText.requestKeyboardFocus() {
    val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
Run Code Online (Sandbox Code Playgroud)


Rob*_*nde 5

根据saleh sereshki的回答和pauminku的评论,我正在使用:

m_SearchEditText.post(new Runnable() {
            @Override
            public void run() {
                m_SearchEditText.requestFocus();
            }
        });
Run Code Online (Sandbox Code Playgroud)

在 Kotlin 中相当于:

m_SearchEditText.post { m_SearchEditText.requestFocus() }
Run Code Online (Sandbox Code Playgroud)