简体   繁体   English

在触摸外部编辑文本区域时隐藏android中的键盘

[英]Hide keypad in android while touching outside Edit Text Area

I know that the code for dismiss tyhe keypad in android is 我知道在android中解雇键盘的代码是

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 to dismiss Softkeyboard is below: 关闭Softkeyboard的代码如下:

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

You can put it in Utility Class or if you are defining it within an activity, avoid the activity parameter, or call hideSoftKeyboard(this). 您可以将它放在Utility Class中,或者如果您在活动中定义它,请避免使用activity参数,或者调用hideSoftKeyboard(this)。

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. 您可以编写一个迭代活动中每个View的方法,并检查它是否是EditText的一个实例,如果它没有将setOnTouchListener注册到该组件,那么一切都将落实到位。 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);
        }
    }
}

Call this method after SetcontentView() with paramet as id of your view like: SetcontentView()使用参数作为视图的id调用此方法,如:

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

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

Best way you can use is DONE button besides EditText make your onClickListener to do like, 你可以使用的最佳方法是除了EditText之外的DONE按钮让你的onClickListener做,

done.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
}
});

This may be old but I got this working by implenting a custom class 这可能是旧的但我通过实现自定义类来实现这一点

public class DismissKeyboardListener implements OnClickListener { 

    Activity mAct;

    public DismissKeyboardListener(Activity act) {
        this.mAct = act;
    } 

    @Override 
    public void onClick(View v) {
        if ( v instanceof ViewGroup ) {
            hideSoftKeyboard( this.mAct );
        } 
    }        
} 

public void hideSoftKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager)
        getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
} 

the best practice here is to create a Helper class and every container Relative / Linear Layouts should implement this. 这里的最佳实践是创建一个Helper类,每个容器相对/线性布局都应该实现这一点。

**** Take note only the main Container should implement this class (For optimization) **** ****请注意只有主要容器应该实现这个类(用于优化)****

and implement it like this : 并像这样实现它:

Parent.setOnClickListener( new DismissKeyboardListener(this) ); 

the keyword this is for Activity. 关键字this是Activity。 so if you are on fragment you use it like getActivity(); 所以,如果你是片段,你就像getActivity();

---thumbs up if it help you... --- cheers Ralph --- ---竖起大拇指,如果它对你有帮助...... ---欢呼拉尔夫---

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在点击发送按钮时防止键盘像whatsapp一样隐藏,并在android中连续关注编辑文本 - How to prevent keypad from hiding like whatsapp while click on send button and make continuous focus on edit text in android 如何在触摸PopupWindow的外部区域时将其关闭? - How to dismiss the PopupWindow on touching outside area of it? 如何在editText外单击时隐藏键盘? - how to hide keypad when click outside editText? 如何通过单击EditText外部隐藏虚拟键盘? - How to hide the virtual keypad by clicking outside of an EditText? 数字键盘后的Phonegap Android文本键盘 - Phonegap Android text keypad after numeric keypad 在活动更改时或未启用编辑文本时如何隐藏键盘? - How do you hide the keypad on activity change or when edit text is not enabled? 如何通过在背景上触摸带有可缩放图像的外部编辑文本来禁用软键盘和光标? - How can I achieve disabling of Soft keyboard and cursor, by touching outside Edit Text with zoomable image on background? 触摸外部时,ShowCaseView不会隐藏 - ShowCaseView does not hide when touching outside 如何禁用外部触摸以隐藏DialogPreference - How to disable outside touching to hide DialogPreference 用于“电话编辑文本”的软键盘动态 - soft keypad for 'phone edit text' dynamically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM