Hide Soft keyboard on return key press

时光怂恿深爱的人放手 提交于 2019-12-03 01:05:53

If you dont want multiple line,for you edittext you can just specify single line for the edittext and also you can put imeOptions as Done like this

<EditText 
   android:id="@+id/edittext_done"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:imeOptions="actionDone"
   android:singleLine="true"
   />

I clearly dunno whether you are trying to achieve this or not.Any way take a look.

EDIT: android:singleLine is deprecated since API 3 due to bad performance, you have to use android:maxLines instead. singleLine will be available because even now some effects are not supported by android:maxLines attribute.

I solved the issue by changing the following in the XML file from:

 android:inputType="textMultiLine"

to:

 android:inputType="textImeMultiLine"
Ketan Patel

Below code works for me.

IN XML:

    android:imeOptions="actionDone"
    android:inputType="textCapWords"

IN JAVA CLASS:

    edit_text.setOnEditorActionListener(new OnEditorActionListener() {

                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    // TODO Auto-generated method stub

                    if ((actionId==EditorInfo.IME_ACTION_DONE )   )
                     {   
                        //Toast.makeText(getActivity(), "call",45).show();
                       // hide virtual keyboard
                       InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);

                       //or try following:
                       //InputMethodManager imm = (InputMethodManager)getBaseContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                       imm.hideSoftInputFromWindow(auto_data.getWindowToken(), 0);
                       return true;
                    }
                    return false;

                }
            });

Based on my tests, what is really required to dismiss the soft keyboard, when a user clicks the enter button, can be achieved simply by adding the code below to your EditText in xml.

android:imeOptions="actionDone"
android:inputType="textImeMultiLine"

No need for OnEditorActionListener or any other Java code.

Accepted answer is deprecated:

<EditText 
   android:id="@+id/edittext_done"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:imeOptions="actionDone"
   android:singleLine="true"
/>

Try with this:

<EditText 
   android:id="@+id/edittext_done"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:imeOptions="actionDone"
   android:maxLines="1"
/>

EditText.xml

<EditText
        android:id="@+id/edit_text_searh_home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="4dp"
        android:singleLine="true"
        android:lines="1"
        android:hint="Ingresa palabras claves"
        android:imeActionLabel="Search"
        android:background="@drawable/rounded_edit_text_search"
        android:drawableRight="@android:drawable/ic_menu_search"/>

Fragment.java

final EditText edit_text_searh = (EditText) v.findViewById(R.id.edit_text_searh_home);

    edit_text_searh.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(edit_text_searh.getWindowToken(), 0);

            Toast.makeText(getContext(),edit_text_searh.getText(),Toast.LENGTH_SHORT).show();
            return true;

        }
    });

This may be you could add a attribute to your EditText like this:

android:imeOptions="actionSearch"

Try this method, It may be solve your problem.

protected void showKeyboard() {

            InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (activity.getCurrentFocus() == null) {
                inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
            } else {
                View view = activity.getCurrentFocus();
                inputMethodManager.showSoftInput(view,InputMethodManager.SHOW_FORCED);
            }
        }

        /**
         * Hide keyboard.
         */
        protected void hideKeyboard() {

            InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            View view = activity.getCurrentFocus();
            if (view == null) {
                if (inputMethodManager.isAcceptingText())
                    inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
            } else {
                if (view instanceof EditText)
                    ((EditText) view).setText(((EditText) view).getText().toString()); // reset edit text bug on some keyboards bug
                inputMethodManager.hideSoftInputFromInputMethod(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }          
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!