在外部点击时隐藏片段中的键盘

时间:2016-04-27 11:58:55

标签: android android-fragments keyboard fragment

我有一个包含EditText的片段用于输入,但现在我想在用户点击EditText之外的屏幕时关闭键盘。

我知道如何在一项活动中做到这一点,但对于片段似乎有所不同。

我在view.onTouchListener

上调用此方法
public static void hideSoftKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);

inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}

任何人都有解决方案,谢谢

7 个答案:

答案 0 :(得分:16)

在片段的父Activity中覆盖以下方法:

 @Override
public boolean dispatchTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        View v = getCurrentFocus();
        if ( v instanceof 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);
}

在片段的布局中使用此属性:

android:focusableInTouchMode="true"

希望这会对你有所帮助。

答案 1 :(得分:1)

使用此方法可以正常使用

public static void hideKeyBoardMethod(final Context con, final View view) {
        try {
            view.post(new Runnable() {
                @Override
                public void run() {
                    InputMethodManager imm = (InputMethodManager) con.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

答案 2 :(得分:0)

您也可以这样做

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

希望有所帮助!

答案 3 :(得分:0)

如果你想在editText之外触摸,那么键盘隐藏自动,所以使用此代码

public boolean dispatchTouchEvent(MotionEvent event) {

        View view = getCurrentFocus();
        boolean ret = super.dispatchTouchEvent(event);

        if (view instanceof EditText) {
            View w = getCurrentFocus();
            int location[] = new int[2];
            w.getLocationOnScreen(location);
            float x = event.getRawX() + w.getLeft() - location[0];
            float y = event.getRawY() + w.getTop() - location[1];
            if (event.getAction() == MotionEvent.ACTION_DOWN
                    && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom())) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
            }
        }
        return ret;
    }

答案 4 :(得分:0)

你可以使用这种方法对我来说很好。 只需传递布局的根元素的引用,就像这样

setupUI(rootView.findViewById(R.id.rootParent))

setupUI的代码在下面..

public void setupUI(View parentView) {

    //Set up touch listener for non-text box views to hide keyboard.
    if(!(view instanceof EditText)) {

        view.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                hideSoftKeyboard();
                handleCallBack();
                return false;
            }
        });
    }

答案 5 :(得分:0)

如果Fragment使用下面的代码

View view = inflater.inflate(R.layout.fragment_test, container, false);

    view.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {

                if(event.getAction() == MotionEvent.ACTION_MOVE){
                    dispatchTouchEvent(event);
                }
                return true;
            }
    });

//here the rest of your code

return view;

应用此代码并调用dispatchTouchEvent();方法

答案 6 :(得分:0)

我刚刚在活动的所有片段中覆盖了 dispatchTouchEvent()方法。

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    // TODO Auto-generated method stub
     if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if ( v instanceof EditText) {
                Rect outRect = new Rect();
                v.getGlobalVisibleRect(outRect);
                if (!outRect.contains((int)ev.getRawX(), (int)ev.getRawY())) {
                    v.clearFocus();
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
        } 

并在主要布局中的每个片段XML中设置属性

android:focusableInTouchMode="true"
相关问题