[英]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.