How to hide the keyboard when pressed towards editText?

I have custom listviews editTextand edit the data editTextwhen clicked editTextby showing the keyboard. It is working fine.

my problem is that when I press editText, the keyboard should be hiding.

thank...

+5
source share
4 answers

To do this, you need to take onTouchListener in the parent layout of the layout file. on TouchListener you need to encode to hide the keyboard by clicking outside of the EditText. Please follow the XML layout and Java class to solve this problem, follow the following URL.

http://amitthaperandroidquery.blogspot.com/2011/10/remove-keyboard-after-click-outside.html

+5

, EditText. , Keyborad: -

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
+2

, :

  • ( ) ,

        android:clickable="true" 
        android:focusableInTouchMode="true" 
    
  • hideKeyboard()

        public void hideKeyboard(View view) {
            InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    
  • , onFocusChangeListener edittext.

        edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    hideKeyboard(v);
                }
            }
        });
    

0

, , OnInterceptTouchEvent, false, , .

The example below is for Xamarin, but it is easy to port it to Java:

public class KeyboardHidingScrollView : ScrollView
{
    public KeyboardHidingScrollView (Context context) : base (context)
    {
    }

    public KeyboardHidingScrollView (Context context, IAttributeSet attrs) : base (context, attrs)
    {
    }

    public override bool OnInterceptTouchEvent (Android.Views.MotionEvent ev)
    {
        var methodManager = (InputMethodManager)Context.GetSystemService (Context.InputMethodService);
        methodManager.HideSoftInputFromWindow (WindowToken, HideSoftInputFlags.None);
        return false;
    } 
}
0
source

All Articles