Android 在子片段中单击非编辑文本视图时如何关闭键盘

Android 在子片段中单击非编辑文本视图时如何关闭键盘,android,android-layout,android-fragments,android-softkeyboard,android-nested-fragment,Android,Android Layout,Android Fragments,Android Softkeyboard,Android Nested Fragment,因此,当我点击非编辑文本时,我发现这段非常酷的代码可以关闭键盘。除了使用getChildFragmentManager()启动的片段和对话框片段之外,它工作得非常好。有人能解释一下为什么会出现异常,以及我如何修复它吗 public static void setupUI(View view, final Activity activity) { // Set up touch listener for non-text box views to hide keyboard. i

因此,当我点击非编辑文本时,我发现这段非常酷的代码可以关闭键盘。除了使用
getChildFragmentManager()
启动的片段和对话框片段之外,它工作得非常好。有人能解释一下为什么会出现异常,以及我如何修复它吗

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

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

        view.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                LayoutUtils.hideSoftKeyboard(activity);
                return false;
            }

        });
    }

    // If a layout container, iterate over children and seed recursion.
    if (view instanceof ViewGroup) {

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

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

            setupUI(innerView, activity);
        }
    }
}

private static void hideSoftKeyboard(Activity activity) {
    InputMethodManager inputMethodManager = (InputMethodManager) activity
        .getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
publicstaticvoidsetupui(视图、最终活动){
//为非文本框视图设置触摸监听器以隐藏键盘。
如果(!(查看EditText的实例)){
view.setOnTouchListener(新的OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
LayoutUtils.hideSoftKeyboard(活动);
返回false;
}
});
}
//如果是布局容器,则在子对象上迭代并为递归设定种子。
if(视图组的视图实例){
对于(int i=0;i<((视图组)视图)。getChildCount();i++){
视图内部视图=((视图组)视图);
setupUI(内部视图、活动);
}
}
}
专用静态无效隐藏键盘(活动){
InputMethodManager InputMethodManager=(InputMethodManager)活动
.getSystemService(活动.输入方法\服务);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),0);
}

我将代码保存在实用程序类中,并在整个应用程序中使用它。基本上,对于问题案例,我在FragmentB上使用它是通过FragmentA使用
getChildFragmentManager()
启动的,那么代码不会影响FragmentB的视图。

请尝试隐藏键盘

public void hideSoftKeyboard() {
        try {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
请尝试以下代码:

@Override
public boolean onTouchEvent(MotionEvent event) {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    return true;
}

如果您使用的是fragment,请使用FragmentActivity而不是Activity。我建议用试抓


我找到了解决办法。也许其他人会发现它很有用

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

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

        view.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                LayoutUtils.hideSoftKeyboard(activity,v);
                return false;
            }

        });
    }

    // If a layout container, iterate over children and seed recursion.
    if (view instanceof ViewGroup) {

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

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

            setupUI(innerView, activity);
        }
    }
}

private static void hideSoftKeyboard(Activity activity, View v) {
    InputMethodManager inputMethodManager = (InputMethodManager) activity
        .getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
publicstaticvoidsetupui(视图、最终活动){
//为非文本框视图设置触摸监听器以隐藏键盘。
如果(!(查看EditText的实例)){
view.setOnTouchListener(新的OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
LayoutUtils.hideSoftKeyboard(活动,v);
返回false;
}
});
}
//如果是布局容器,则在子对象上迭代并为递归设定种子。
if(视图组的视图实例){
对于(int i=0;i<((视图组)视图)。getChildCount();i++){
视图内部视图=((视图组)视图);
setupUI(内部视图、活动);
}
}
}
专用静态无效隐藏键盘(活动,视图v){
InputMethodManager InputMethodManager=(InputMethodManager)活动
.getSystemService(活动.输入方法\服务);
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(),0);
}

请参见如何将
activity.getCurrentFocus()
替换为
v
。就这样。它现在可以在所有情况下工作。

有什么例外?你能分享日志吗?@musharapp by
exception
我是说异常:我不是说Java异常。什么是活动?你在哪里初始化的?
public static void setupUI(View view, final Activity activity) {

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

        view.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                LayoutUtils.hideSoftKeyboard(activity,v);
                return false;
            }

        });
    }

    // If a layout container, iterate over children and seed recursion.
    if (view instanceof ViewGroup) {

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

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

            setupUI(innerView, activity);
        }
    }
}

private static void hideSoftKeyboard(Activity activity, View v) {
    InputMethodManager inputMethodManager = (InputMethodManager) activity
        .getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}