Automatic popping up keyboard on start Activity

后端 未结 19 1056
醉梦人生
醉梦人生 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: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.

提交回复
热议问题