当用户点击android屏幕上的其他任何地方时隐藏键盘 [英] hide keyboard when user taps anywhere else on the screen in android

查看:27
本文介绍了当用户点击android屏幕上的其他任何地方时隐藏键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击 Edittext 以外的任何位置时,我需要在 android 中隐藏软键盘.iphone有很多帮助,但android没有.我试过这段代码,但它不起作用:(

I need to hide the softkeypad in android when user click on anywhere other than a Edittext. There are many help for iphone but not for android. I tried this code but its not working :(

final RelativeLayout base = (RelativeLayout) findViewById(R.id.RelativeLayout1);

    findViewById(R.id.base).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(base.getWindowToken(), 0);

        }
    });

提前致谢!

推荐答案

你可以使用onTouchEvent()来隐藏Softkeyboard.

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

虽然此解决方案有效,但我建议最好在 answer 下方使用,因为它提供了关闭的最佳解决方案键盘触摸其他任何地方,然后是 EditText.

Though this solution works, but the best I would suggest is to use below answer as it gives best solution of closing the keyboard touching anywhere else then EditText.

这篇关于当用户点击android屏幕上的其他任何地方时隐藏键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆