EditText request focus not working

后端 未结 11 601
自闭症患者
自闭症患者 2020-12-15 17:03

I have an EditText and a Button. On click of the button i want to open the EditText keyboard and at the same time request focus on the

相关标签:
11条回答
  • 2020-12-15 17:11

    In my case none of the answers above worked (they don't work in Nox, for example). To set focus to EditText, you could trick it into thinking that the user actually clicked it.

    So I use MotionEvent, like this:

    // simulate a click, which consists of ACTION_DOWN and ACTION_UP
    MotionEvent eventDown = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0);
    editText.dispatchTouchEvent(eventDown);
    eventDown.recycle();
    
    MotionEvent eventUp = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_UP, 0, 0, 0);
    editText.dispatchTouchEvent(eventUp);
    eventUp.recycle();
    
    // To be on the safe side, also use another method
    editText.setFocusableInTouchMode(true);
    editText.requestFocus();
    editText.requestFocusFromTouch();
    
    0 讨论(0)
  • 2020-12-15 17:12

    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);
        }
    });
    
    0 讨论(0)
  • 2020-12-15 17:13

    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);
    
    0 讨论(0)
  • 2020-12-15 17:18

    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.

    0 讨论(0)
  • 2020-12-15 17:19

    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

    0 讨论(0)
  • 2020-12-15 17:19

    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);
    }
    
    0 讨论(0)
提交回复
热议问题