Disable Software Keyboard in Android until EditText is chosen

前端 未结 3 941
悲&欢浪女
悲&欢浪女 2021-02-06 19:28

how can I prevent the software keyboard to disappear until a specific EditText is choosen? I have several EditTexts in my Layout when it is opened the first is automatically sel

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-06 20:07

    final InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    
        txt1.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(!(hasFocus || txt2.hasFocus()))
                {   
                mgr.hideSoftInputFromWindow(txt1.getWindowToken(), 0);
                }
    
            }
        });
    

    this code has good effect for focus handling and keyboard display...

提交回复
热议问题