Automatic popping up keyboard on start Activity

后端 未结 19 987
醉梦人生
醉梦人生 2020-11-28 23:05

I got a relative simple question. I have an activity with a lot of EditText\'s in them. When I open the activity it automatically focusses to the first EditText and displays

相关标签:
19条回答
  • 2020-11-28 23:24

    https://stackoverflow.com/a/11627976/5217837 This is almost correct:

    @Override
    public void onCreate(Bundle savedInstanceState) {
    
    getWindow().setSoftInputMode(
       WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    
    }
    

    But it should be SOFT_INPUT_STATE_HIDDEN rather than SOFT_INPUT_STATE_ALWAYS_VISIBLE

    0 讨论(0)
  • 2020-11-28 23:29

    Use this in your Activity's code:

    @Override
    public void onCreate(Bundle savedInstanceState) {
    
    getWindow().setSoftInputMode(
       WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    
    }
    
    0 讨论(0)
  • 2020-11-28 23:29

    I had a simular problem, even when switching tabs the keyboard popped up automatically and stayed up, with Android 3.2.1 on a Tablet. Use the following method:

    public void setEditTextFocus(EditText searchEditText, boolean isFocused)
    {
        searchEditText.setCursorVisible(isFocused);
        searchEditText.setFocusable(isFocused);
        searchEditText.setFocusableInTouchMode(isFocused);
        if (isFocused) {
            searchEditText.requestFocus();
        } else {
            InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
            inputManager.hideSoftInputFromWindow(searchEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS );
        }
    }   
    

    In the onCreate() and in the onPause() of the activity for each EditText:

    setEditTextFocus(myEditText, false);

    For each EditText an OnTouchListener:

        myEditText.setOnTouchListener(new EditText.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                setEditTextFocus(myEditText, true); 
                return false;
            }
        });
    

    For each EditText in the OnEditorActionListener:

        myEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
                                .......
                setEditTextFocus(myEditText, false); 
                return false;
            }
        });
    

    And for each EditText in the layout xml:

                android:imeOptions="actionDone"
                android:inputType="numberDecimal|numberSigned" // Or something else
    

    There is probably more code optimizing possible.

    0 讨论(0)
  • 2020-11-28 23:31

    I have found this simple solution that worked for me.Set these attributes in your parent layout:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true" >
    

    And now, when the activity starts this main layout will get focus by default.

    Also, we can remove focus from child views at runtime by giving the focus to the main layout again, like this:

    findViewById(R.id.mainLayout).requestFocus();
    

    Hope it will work for you .

    0 讨论(0)
  • 2020-11-28 23:33

    Accepted answer is not working for me, that's why give answer working solution, may be it is helpful !

    EditText edt = (EditText) findViewById(R.id.edt);
    edt.requestFocus();    
    edt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
    edt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
    

    Now keyboard is open enjoy :)

    0 讨论(0)
  • 2020-11-28 23:35

    You can add this to your Android Manifest activity:

    android:windowSoftInputMode="stateHidden|adjustResize"
    
    0 讨论(0)
提交回复
热议问题