Is there any way to permanently hide soft keyboard but keep enabling focus for an EditText?

回眸只為那壹抹淺笑 提交于 2019-12-04 11:09:10

Hiding Keyboard Manually Here

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    YourEditText.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub
            mgr.hideSoftInputFromWindow(v.getWindowToken(), 0);

        }
    });
      YourEditText.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mgr.hideSoftInputFromWindow(v.getWindowToken(), 0);

        }
    });

i check my self. its working correct. InputMethodManager imm ;

 edittext1 = (EditText) findViewById(R.id.editText1);

    imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

   edittext1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

     imm.hideSoftInputFromWindow(edittext1.getWindowToken(), 0);
        }
    });

In place of setting hideSoftInputFromWindow() to each EditText, it will be good if you set the parameter for the parent layout of the activity. Suppose the parent layout of the activity is a LinearLayout, Then,

linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
    /* Hide keyboard from this activity permanently */
    InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    manager.hideSoftInputFromWindow(linearLayout.getWindowToken(),0);

Or You can also implement the same thing for xml

<EditText 
android:focusable="false"

.../>

This disables the keyboard permanently for that edittext

Android studio suggested the following command that keeps the keyboard from showing up. I went ahead and threw it into a function as follows and called it for each EditText object.

private void hideKeyboard(EditText editText){
    editText.setShowSoftInputOnFocus(false);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!