在片段

时间:2015-09-24 08:36:41

标签: android listview android-fragments listener

我有一个片段,其中包含用于搜索输入的EditText和一个ListView。我已经让搜索部分正常工作,但现在我想在用户点击EditText外部的屏幕时关闭键盘。我也喜欢使用点击事件(因为它是一个ListView,如果我不使用点击事件,用户可能会意外点击他们不想打开的ListView项目)< / p>

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

到目前为止我已尝试过:

  • 在片段中实现View.OnClickListener并实现onClick:

    @Override
    public void onClick(View v) {
        System.out.println("onClick true");
        if (!(v instanceof EditText)) {
            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.
                    INPUT_METHOD_SERVICE);
            if (getActivity().getCurrentFocus() != null) {
                imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
            }
        }
    }
    

这似乎永远不会发布&#34; onClick true&#34;当我点击时。

  • 覆盖片段活动中的onTouchEvent并实现onTouchEvent:

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

非常感谢任何帮助。提前谢谢。

3 个答案:

答案 0 :(得分:7)

关闭Softkeyboard的代码如下:

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

您可以将它放在Utility Class中,或者如果您在活动中定义它,请避开activity参数,或者调用hideSoftKeyboard(this)。

您可以编写一个迭代活动中每个View的方法,并检查它是否是EditText的一个实例,如果它没有将setOnTouchListener注册到该组件,那么一切都将落实到位。如果你想知道如何做到这一点,事实上它很简单。这是你做的,你写一个递归方法,如下所示。

public void setupUI(View view) {

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

        view.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                hideSoftKeyboard();
                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);
        }
    }
}

在SetcontentView()之后调用此方法,其中参数为视图的ID,如:

RelativeLayoutPanel android:id="@+id/parent"> ... </RelativeLayout>

然后调用`setupUI(findViewById(R.id.parent))

参考:Hide keypad in android while touching outside Edit Text Area

答案 1 :(得分:4)

试试这个,

在xml文件中

android:clickable="true" 
android:focusableInTouchMode="true" 

创建方法。

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

通话方法。

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

答案 2 :(得分:2)

如果我找到了你,你可以尝试使用OnFocusChangeListener:

yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            final InputMethodManager inputManager = (InputMethodManager) getAppContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (inputManager != null && !hasFocus) {
                inputManager.hideSoftInputFromWindow(currentFocusedView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    });

这只是一个样本,但它可能指向正确的方向。