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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20559948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 21:23:24  来源:igfitidea点击:

Hide keypad in android while touching outside Edit Text Area

androideclipseandroid-edittextontouchlistenerandroid-keypad

提问by Nithin Michael

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

我知道在 android 中关闭 tyhe 键盘的代码是

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.

如果我们触摸屏幕中键盘以外的文本区域之外的区域,任何人都可以向我建议一种隐藏键盘的方法。

回答by Sharmilee

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);
}

You can put it in Utility Class or if you are defining it within an activity, avoid the activity parameter, or call hideSoftKeyboard(this).

您可以将它放在实用程序类中,或者如果您在活动中定义它,请避免使用活动参数,或调用 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. 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.

您可以编写一个方法来遍历活动中的每个视图,并检查它是否是 EditText 的实例,如果它没有向该组件注册 setOnTouchListener 并且一切都会就位。如果您想知道如何做到这一点,实际上很简单。这就是你要做的,你写一个递归方法,如下所示。

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 idof your view like:

在您的视图中SetcontentView()使用参数调用此方法,id例如:

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

Then call setupUI(findViewById(R.id.parent))

然后打电话 setupUI(findViewById(R.id.parent))

回答by Jaydipsinh Zala

Best way you can useis DONE button besides EditTextmake your onClickListenerto 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);
}
});

回答by ralphgabb

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. so if you are on fragment you use it like getActivity();

关键字 this 是针对 Activity 的。所以如果你在片段上,你可以像 getActivity() 一样使用它;

---thumbs up if it help you... --- cheers Ralph ---

--- 如果它对你有帮助,请竖起大拇指... --- 拉尔夫欢呼 ---