相关疑难解决方法(0)

如何在EditText外单击后在android上隐藏软键盘?

好的,大家都知道要隐藏你需要实现的键盘:

InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)

但这里最重要的是当用户触摸或选择任何其他不是EditText软键盘或软键盘的地方时如何隐藏键盘?

我试图onTouchEvent()在我的父母Activity身上使用,但这只有在用户触摸任何其他视图以外且没有滚动视图时才有效.

我尝试实现触摸,单击,集中监听器而没有任何成功.

我甚至尝试实现自己的scrollview来拦截触摸事件,但我只能获取事件的坐标而不是点击的视图.

有没有一种标准的方法来做到这一点?在iPhone中它真的很容易.

android android-softkeyboard

340
推荐指数
17
解决办法
33万
查看次数

在片段中单击EditText外部关闭键盘

我有一个片段,包含用于搜索输入的EditText和一个ListView.我有搜索部分工作,但现在我想在用户点击EditText之外的屏幕时关闭键盘.我也想使用click事件(因为它是一个ListView,如果我不使用click事件,用户可能会意外地点击他们不想打开的ListView项目)

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

到目前为止我尝试过的:

  • 实现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);
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

当我点击时,这似乎永远不会发布"onClick true".

  • 覆盖片段活动中的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;
    }
    
    Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.提前致谢.

android listview listener android-fragments

0
推荐指数
2
解决办法
5253
查看次数