失去焦点时隐藏软键盘

时间:2013-03-14 15:08:10

标签: android

如果我们有一个EditText并且它失去焦点(对于一个不需要键盘的元素),软键盘应该自动隐藏还是我们应该自己隐藏它?

我正在将焦点从AutoCompleteSearchView(我的行为应该像EditText一样)移动到ButtonrequestFocus()返回true,但是键盘没有别隐藏。

9 个答案:

答案 0 :(得分:61)

最好的方法是为EditText设置OnFocusChangeListener,然后将键盘中的代码添加到侦听器的OnFocusChange方法中。当EditText失去焦点时,Android将自动关闭键盘。

OnCreate方法中的类似内容:

EditText editText = (EditText) findViewById(R.id.textbox);
OnFocusChangeListener ofcListener = new MyFocusChangeListener();
editText.setOnFocusChangeListener(ofcListener);

然后添加类:

private class MyFocusChangeListener implements OnFocusChangeListener {

    public void onFocusChange(View v, boolean hasFocus){

        if(v.getId() == R.id.textbox && !hasFocus) {

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

        }
    }
}

答案 1 :(得分:6)

Android不会为您隐藏键盘。如果您希望在EditText失去焦点时隐藏键盘,请尝试在该事件上使用这样的方法:

private void hideKeypad() {
    EditText edtView = (EditText) findViewById(R.id.e_id);

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

答案 2 :(得分:6)

试试这个

 /**
 * Hide keyboard on touch of UI
 */
public void hideKeyboard(View view) {

    if (view instanceof ViewGroup) {

        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {

            View innerView = ((ViewGroup) view).getChildAt(i);

            hideKeyboard(innerView);
        }
    }
    if (!(view instanceof EditText)) {

        view.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                hideSoftKeyboard(v);
                return false;
            }

        });
    }

}

/**
 * Hide keyboard while focus is moved
 */
public void hideSoftKeyboard(View view) {
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) contentsContext_
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputManager != null) {
            if (android.os.Build.VERSION.SDK_INT < 11) {
                inputManager.hideSoftInputFromWindow(view.getWindowToken(),
                        0);
            } else {
                if (this.getCurrentFocus() != null) {
                    inputManager.hideSoftInputFromWindow(this
                            .getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                }
                view.clearFocus();
            }
            view.clearFocus();
        }
    }
}

答案 3 :(得分:2)

试试这个,可能会解决你的问题。

private void hideKeyboard() {
    InputMethodManager mImMan = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    mImMan.hideSoftInputFromWindow(mYourEdttxtName.getWindowToken(), 0);
}

您可以从here.

中找到更多信息

答案 4 :(得分:1)

已找到此问题的解决方案here 它在活动上使用DispatchTouchEvent,并且不会将每个EditText挂钩到FocusChange或Touch事件。
这是更好的解决方案。

我的Xamarin实现如下:

public override bool DispatchTouchEvent(MotionEvent ev)
    {
        if (ev.Action == MotionEventActions.Down)
        {
            var text = CurrentFocus as EditText;
            if (text != null)
            {
                var outRect = new Rect();
                text.GetGlobalVisibleRect(outRect);
                if (outRect.Contains((int) ev.RawX, (int) ev.RawY)) return base.DispatchTouchEvent(ev);
                text.ClearFocus();
                HideSoftKeyboard();
            }
        }
        return base.DispatchTouchEvent(ev);
    }

protected void HideSoftKeyboard()
    {
        var inputMethodManager = (InputMethodManager) GetSystemService(InputMethodService);
        inputMethodManager.HideSoftInputFromWindow(CurrentFocus.WindowToken, 0);
    }

答案 5 :(得分:1)

只需创建一个静态方法

public static void touchScreenAndHideKeyboardOnFocus(View view, final Activity activity) {

    if (view instanceof EditText) {
        view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    if(activity != null) {
                       InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
                       if (activity.getCurrentFocus() != null) {
                          inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                       }
                    }
                }
            }
        });
    }

    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            View innerView = ((ViewGroup) view).getChildAt(i);
            touchScreenAndHideKeyboardOnFocus(innerView, activity);
        }
    }
}

view是布局的根视图..但是如果你的edittext中有另一个焦点监听器,请小心..

答案 6 :(得分:0)

您可以覆盖dispatchTouchEvent方法来实现它:

@Override
public boolean dispatchTouchEvent(MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) {

        /**
         * It gets into the above IF-BLOCK if anywhere the screen is touched.
         */

        View v = getCurrentFocus();
        if ( v instanceof EditText) {


            /**
             * Now, it gets into the above IF-BLOCK if an EditText is already in focus, and you tap somewhere else
             * to take the focus away from that particular EditText. It could have 2 cases after tapping:
             * 1. No EditText has focus
             * 2. Focus is just shifted to the other EditText
             */

            Rect outRect = new Rect();
            v.getGlobalVisibleRect(outRect);
            if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
                v.clearFocus();
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
        }
    }
    return super.dispatchTouchEvent( event );
}

奖励:如果EditText获得焦点,则触发事件的顺序为:

    调用另一个EditText的
  1. onFocusChange()(如果其他edittext失去焦点)
  2. ACTION_DOWN被称为
  3. 最后,将调用该EditText的onFocusChange()方法。

答案 7 :(得分:0)

我的问题解决了这个代码(在片段中)

LinearLayout linearLayoutApply=(LinearLayout)rootView.findViewById(id.LinearLayoutApply);

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

hideKeyBoard

 public void hideKeyBoard(View v)
{
    InputMethodManager imm=(InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}

答案 8 :(得分:0)

这对我有用:

InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
相关问题