android - 在android中单击时隐藏默认键盘

标签 android

当我在屏幕中单击编辑框的外侧时,我想隐藏软键盘。我该怎么做?

最佳答案

必须对其进行编辑才能使其正常工作。添加了一个检查以查看焦点 View 是否为 EditText。

@Override
public boolean dispatchTouchEvent(MotionEvent event) {

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

    if (v instanceof EditText) {
        View w = getCurrentFocus();
        int scrcoords[] = new int[2];
        w.getLocationOnScreen(scrcoords);
        float x = event.getRawX() + w.getLeft() - scrcoords[0];
        float y = event.getRawY() + w.getTop() - scrcoords[1];

        Log.d("Activity", "Touch event "+event.getRawX()+","+event.getRawY()+" "+x+","+y+" rect "+w.getLeft()+","+w.getTop()+","+w.getRight()+","+w.getBottom()+" coords "+scrcoords[0]+","+scrcoords[1]);
        if (event.getAction() == MotionEvent.ACTION_UP && (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;
}

可能会以更流畅的方式完成,但效果非常好。

关于android - 在android中单击时隐藏默认键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4005728/

相关文章:

android - Maps v3.0.0 Beta 的 Proguard 规则

java - 如何使用 CursorAdapter 从 ListView 中删除所选项目

android - 使用 NFC 启动特定的 Android 应用程序

android - Android TextView 到图像文件?

java - 在 java 类和 android Activity 之间流式传输时音频不清晰

Android星级评分如何适应宽度

android - 为什么我的 Flutter 应用程序无法运行?漏洞!源单元中的阶段 'semantic analysis' 中的异常 '_BuildScript_' 不支持的类文件主要版本 60

android - 在开发模式下,如何自动将Android的版本字符串更改为-dev?

android - 如何避免在 Android Facebook SDK 中显示已授权应用对话框

android - 可扩展的 ListView 组 View 指示器显示在错误的组中