怎么一下外面的EditText后隐藏软键盘上的Andr​​oid? [英] how to hide soft keyboard on android after clicking outside EditText?

查看:221
本文介绍了怎么一下外面的EditText后隐藏软键盘上的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定每个人都知道你需要实现隐藏键盘:

Ok everyone knows that to hide a keyboard you need to implement:

InputMethodManager imm = (InputMethodManager) getSystemService(
    INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

但这里的大问题是如何当用户触摸或选择不是一个编辑框或softKeyboard?

But the big deal here is how to hide the keyboard when the user touches or selects any other place that is not an EditBox or the softKeyboard?

我试图用的onTouchEvent我父活动,但是,只有当用户触摸的任何其它视图之外,没有滚动视图工作。

I tried to use the onTouchEvent on my parent Activity but that only works if user touches outside any other view and there is no scrollview.

我试图实现触摸,点击,焦点侦听器没有成功。

I tried to implement a touch, click, focus listener without any success.

我甚至想实现我自己的滚动型拦截触摸事件,但我只能得到事件的坐标,而不是视图点击。

I even tried to implement my own scrollview to intercept touch events but I can only get the coordinates of the event and not the view clicked.

有没有一种标准的方式来做到这一点?在iPhone这是很容易。

Is there a standard way to do this?? in iPhone it was really easy.

推荐答案

下面是我做我的应用程序,它的工作原理是,等待它,完美!

Here is what I did for my application and it works like, wait for it, perfect!

首先这里是code,它只是隐藏键盘:

First here is the code that simply hides the keyboard:

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

您可以把这个在一个工具类,或者如果你是一个活动中定义它,避免活动参数,或致电 hideSoftKeyboard(本)

You can put this up in a utility class, or if you are defining it within an activity, avoid the activity parameter, or call hideSoftKeyboard(this).

最棘手的部分是,当调用它。你可以写,通过每一个查看在您的活动迭代,并检查它是否是一个的instanceof的EditText ,如果它的方法未注册 setOnTouchListener 到该组件,一切将下降到位。如果你想知道如何做到这一点,其实很简单。这里是你做什么,你写类似下面的递归方法,其实你可以用这个做任何事情,就像设置自定义字体等等......这里是法

The trickiest part is when to call it. You can write a method that iterates through every View in your activity, and check if it is an instanceof EditText if it is not register a setOnTouchListener to that component and everything will fall in place. In case you are wondering how to do that, it is in fact quite simple. Here is what you do, you write a recursive method like the following, in fact you can use this to do anything, like setup custom typefaces etc... Here is the method

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(MyActivity.this);
                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 。指定一个 ID 你父容器像

That is all, just call this method after you setContentView in your activity. In case you are wondering what parameter you would pass, it is the id of the parent container. Assign an id to your parent container like

&LT; RelativeLayoutPanel机器人:ID =@ + ID /父&GT; ...&LT; / RelativeLayout的&GT;

和呼叫 setupUI(findViewById(R.id.parent)),仅此而已。

如果你想有效地利用这一点,您可以创建扩展活动并把此方法,并在应用程序中的所有其他活动扩大本次活动,并调用它 setupUI()的onCreate()方法。

If you want to use this effectively, you may create an extended Activity and put this method in, and make all other activities in your application extend this activity and call its setupUI() in the onCreate() method.

希望它帮助。

如果您使用超过1活动定义通用ID像父布局 &LT; RelativeLayout的机器人:ID =@ + ID / main_parent&GT; ...&LT; / RelativeLayout的&GT;

If you use more than 1 activity define common id to parent layout like <RelativeLayout android:id="@+id/main_parent"> ... </RelativeLayout>

再从活动扩展一个类,并定义 setupUI(findViewById(R.id.main_parent))在其 OnResume()和扩展,而不是这个类``活动在程序中

Then extend a class from Activity and define setupUI(findViewById(R.id.main_parent)) Within its OnResume() and extend this class instead of ``Activityin your program

这篇关于怎么一下外面的EditText后隐藏软键盘上的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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