How to hide Soft Keyboard when activity starts

匿名 (未验证) 提交于 2019-12-03 01:08:02

问题:

I have an Edittext with android:windowSoftInputMode="stateVisible" in Manifest. Now the keyboard will be shown when I start the activity. How to hide it? I cannot use android:windowSoftInputMode="stateHidden because when keyboard is visible then minimize the app and resume it the keyboard should be visible. I tried with

InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

but it did not work.

回答1:

Use the following functions to show/hide the keyboard:

/**  * Hides the soft keyboard  */ public void hideSoftKeyboard() {     if(getCurrentFocus()!=null) {         InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);         inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);     } }  /**  * Shows the soft keyboard  */ public void showSoftKeyboard(View view) {     InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);     view.requestFocus();     inputMethodManager.showSoftInput(view, 0); } 


回答2:

In the AndroidManifest.xml:

    

or try

Please check this also



回答3:

Just add two attributes to the parent view of editText.

android:focusable="true" android:focusableInTouchMode="true" 


回答4:

Put this in the manifest inside the Activity tag

  android:windowSoftInputMode="stateHidden"   


回答5:

Try this:

Look at this one for more details.



回答6:

To hide the softkeyboard at the time of New Activity start or onCreate(),onStart() etc. you can use the code below:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 


回答7:

Add the following text to your xml file.

             


回答8:

I hope this will work, I tried a lot of methods but this one worked for me in fragments. just put this line in onCreateview/init.

getActivity().getWindow().setSoftInputMode(     WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 


回答9:

Put this code your java file and pass the argument for object on edittext,

private void setHideSoftKeyboard(EditText editText){     InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);     imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); } 


回答10:

You can set config on AndroidManifest.xml

Example:



回答11:

This is what I did:

yourEditText.setCursorVisible(false); //This code is used when you do not want the cursor to be visible at startup         yourEditText.setOnTouchListener(new View.OnTouchListener() {              @Override             public boolean onTouch(View v, MotionEvent event) {                 v.onTouchEvent(event);   // handle the event first                 InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);                 if (imm != null) {                      imm.hideSoftInputFromWindow(v.getWindowToken(), 0);  // hide the soft keyboard                     yourEditText.setCursorVisible(true); //This is to display cursor when upon onTouch of Edittext                 }                 return true;             }         }); 


回答12:

Try this.

First in your searchable xml the fields (name and hint etc) put @string and not literal strings.

Then method onCreateOptionsMenu, it must have a ComponentName object with your package name and your completed class name (with package name) - In case activity which has the SearchView component is the same as the show search results use getComponentName(), as the google android developer says.

I tried a lot of solutions and after much,much work this solution works for me.



回答13:

Use the following code to Hide the softkeyboard first time when you start the Activity

getActivity().getWindow().setSoftInputMode(WindowManager. LayoutParams.SOFT_INPUT_STATE_HIDDEN); 


回答14:

Try this one also

Ed_Cat_Search = (EditText) findViewById(R.id.editText_Searc_Categories);  Ed_Cat_Search.setInputType(InputType.TYPE_NULL);  Ed_Cat_Search.setOnTouchListener(new View.OnTouchListener() {     public boolean onTouch(View v, MotionEvent event) {         Ed_Cat_Search.setInputType(InputType.TYPE_CLASS_TEXT);         Ed_Cat_Search.onTouchEvent(event); // call native handler         return true; // consume touch even     } }); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!