开发者

how to hide keypad when click outside editText?

开发者 https://www.devze.com 2023-04-12 04:43 出处:网络
i have custom listview with editText and edit the edittext data on tapping edi开发者_开发知识库ttext with help of showing keypad it is working fine .

i have custom listview with editText and edit the edittext data on tapping edi开发者_开发知识库ttext with help of showing keypad it is working fine .

my problem is when i click outside of edittext the keypad must hide.

thanks...


For this you have to take the onTouchListener on the parent layout of the Layout File. on the TouchListener you have to code to hide the Keyboard when click outside the EditText. Please follow XML Layout and Java class to resolve this issue please follow following url.

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


One way is that you can set a focus change listener to the EditText. When the widget loses focus you can hide the Keyborad by:-

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


You can achieve this by doing the following steps:

  1. Make the parent view(content view of your activity) clickable and focusable by adding the following attributes

        android:clickable="true" 
        android:focusableInTouchMode="true" 
    
  2. Implement a hideKeyboard() method

        public void hideKeyboard(View view) {
            InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    
  3. Lastly, set the onFocusChangeListener of your edittext.

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

From This source


Another way similar to the accepted answer is to subclass the root view of the layout and override OnInterceptTouchEvent, hide the keyboard and return false to allow the touch to propagate as usual.

The example below is for Xamarin but it is easy to port 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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号