Android Hide Soft Keyboard from EditText while not losing cursor

前端 未结 14 1941
日久生厌
日久生厌 2020-12-03 04:55

I\'ve come about as far as this which gets me halfway there, but not quite. I have a dialer Fragment that has all the usual Buttons to enter a numb

14条回答
  •  佛祖请我去吃肉
    2020-12-03 05:24

    This is what I did. First, in manifest inside activity

    android:windowSoftInputMode="stateAlwaysHidden|adjustNothing"
    

    Second, in onCreate if inside activity or onActivityCreated if inside fragment

    editText.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                hideSoftKeyboard(v);
            }
        });
    

    Do not forget to request focus to the editText

    editText.requestFocus();
    

    Then add the hideSoftKeyboard(v) method same as the other answer.

    private void hideSoftKeyboard(View v){
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
    

    The key here is to requestFocus before clicking the EditText. If without focus, first click will make the keyboard show up(my experience). However, this is applied if you have a single EditText in an activity. With this, you still can type with custom keyboard(if any), can copy and paste, and cursor is still visible.

提交回复
热议问题