隐藏键盘的Andr​​oid的同时,编辑文本区域外的触摸 [英] Hide keypad in android while touching outside Edit Text Area

查看:118
本文介绍了隐藏键盘的Andr​​oid的同时,编辑文本区域外的触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,$ C $下驳回Android的tyhe小键盘

I know that the code for dismiss tyhe keypad in android is

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

任何人都可以给我建议的方法来隐藏键盘,如果我们接触文本区域比在屏幕键盘之外的其他地区。

Can anyone suggest me a method to hide the keypad if we touch the area outside the text area other than the keypad in the screen.

推荐答案

code解雇Softkeyboard是如下:

Code to dismiss Softkeyboard is below:

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 it in Utility Class or if you are defining it within an activity, avoid the activity parameter, or call hideSoftKeyboard(this).

您可以写一个方法,通过在活动的每个视图迭代,并检查它是否是一个instanceof的EditText如果不注册一个setOnTouchListener该组件,一切将下降到位。如果你想知道如何做到这一点,其实很简单。这里是你做什么,你写类似下面的递归方法。

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.

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()此方法PARAMET为 ID 你的看法一样的:

Call this method after SetcontentView() with paramet as id of your view like:

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

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

这篇关于隐藏键盘的Andr​​oid的同时,编辑文本区域外的触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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