EditText request focus not working

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 01:10:56

ensure that the edittext is focusable in touch mode. You can do it two way.

In xml:

android:focusableInTouchMode="true"

in Java:

view.setFocusableInTouchMode(true);

Personally I don't trust the XML definition of this param. I always request focus by these two lines of code:

view.setFocusableInTouchMode(true);
view.requestFocus();

The keyboard shoul appear on itself without the need to call InputMethodManager.

It works in most of the cases. Once it did not work for me because I have lost the reference to the object due to quite heavy processing - ensure that this is not your issue.

In my case it worked by adding a handler after you clicked to button and focus set in another view the focus can get back to your needed view.

just put this in your code:

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

I hope it was helpful

In your manifest.xml write:

<activity android:name=".MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateAlwaysVisible" />

And call m_SearchEditText.requestfocus() in oncreate().
OR,
Try:

if(m_SearchEditText.requestFocus()) {
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}

The following works for me and should help:

EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
Matias Olocco

As an extension to this answer (I didn't add it as a comment because of reputation...).

If you want to reduce the delayed time to zero, use handler.post() instead. Full code:

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);
    }
});

U need two xml attributes also to achieve this:

android:focusable="true"
android:focusableInTouchMode="true"

Add them to the EditText as well as the parent layouts(Any layout inside which these views are). By default these are false, so the focus is not given to the requested view.

Source: https://developer.android.com/reference/android/view/View.html#attr_android:focusable

After u show the EditText based on the checkbox selection, add the next and previous focus points dynamically in code.

Hope this helps.

Minimalist Kotlin extension version because we should pretend these sorts of obtuse calls into system services are not necessary:

fun EditText.requestKeyboardFocus() {
    val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!