隐藏键盘当用户点击屏幕上的任何其他地方android
当用户点击Edittext以外的任何地方时,我需要隐藏android中的softkeypad。 iphone有很多帮助,但不适用于android。我想这个代码,但它不是事先工作:(隐藏键盘当用户点击屏幕上的任何其他地方android
final RelativeLayout base = (RelativeLayout) findViewById(R.id.RelativeLayout1);
findViewById(R.id.base).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(base.getWindowToken(), 0);
}
});
谢谢!
可以使用onTouchEvent()
隐藏Softkeyboard
。
@Override
public boolean onTouchEvent(MotionEvent event) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.
INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
return true;
}
虽然此解决方案有效,但最好我建议使用低于answer,因为它提供了最好的解决方案,关闭键盘接触其他地方然后EditText。
最好的解决办法,我发现是使用如下,
覆盖dispatchTouchEvent()
并尝试使用该caculates 4角落查看(在这里它的EditText)的
Rect
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
int x = (int) ev.getX();
int y = (int) ev.getY();
if (ev.getAction() == MotionEvent.ACTION_DOWN &&
!getLocationOnScreen(etFeedback).contains(x, y)) {
InputMethodManager input = (InputMethodManager)
activity.getSystemService(Context.INPUT_METHOD_SERVICE);
input.hideSoftInputFromWindow(etFeedback.getWindowToken(), 0);
}
return super.dispatchTouchEvent(ev);
}
方法得到的EditText
面积
protected Rect getLocationOnScreen(EditText mEditText) {
Rect mRect = new Rect();
int[] location = new int[2];
mEditText.getLocationOnScreen(location);
mRect.left = location[0];
mRect.top = location[1];
mRect.right = location[0] + mEditText.getWidth();
mRect.bottom = location[1] + mEditText.getHeight();
return mRect;
}
通过使用上面的代码,我们可以检测到EditText
的区域,我们可以检查屏幕上的触摸是否是EditText
区域的一部分。如果它的一部分EditText
则不做任何事情让触摸做它的工作,如果触摸不包含EditText
区域,那么只需关闭软键盘。
****** EDIT ******
我才发现,如果我们不希望给任何的EditText作为输入,并希望隐藏键盘整个应用程序中,当用户触摸任何地方的另一种方法除EditText以外的其他。然后,你必须创建一个BaseActivity和写入全球规范,下面隐藏键盘,
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
boolean handleReturn = super.dispatchTouchEvent(ev);
View view = getCurrentFocus();
int x = (int) ev.getX();
int y = (int) ev.getY();
if(view instanceof EditText){
View innerView = getCurrentFocus();
if (ev.getAction() == MotionEvent.ACTION_UP &&
!getLocationOnScreen(innerView).contains(x, y)) {
InputMethodManager input = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
input.hideSoftInputFromWindow(getWindow().getCurrentFocus()
.getWindowToken(), 0);
}
}
return handleReturn;
}
嗯,我已经做了这种方式:
将代码添加在你活动。
这将工作片段也无需在片段添加此代码。
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
View view = getCurrentFocus();
if (view != null && (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) && view instanceof EditText && !view.getClass().getName().startsWith("android.webkit.")) {
int scrcoords[] = new int[2];
view.getLocationOnScreen(scrcoords);
float x = ev.getRawX() + view.getLeft() - scrcoords[0];
float y = ev.getRawY() + view.getTop() - scrcoords[1];
if (x < view.getLeft() || x > view.getRight() || y < view.getTop() || y > view.getBottom())
((InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow((this.getWindow().getDecorView().getApplicationWindowToken()), 0);
}
return super.dispatchTouchEvent(ev);
}
希望这会对你有帮助。
方法完美谢谢。 – SalutonMondo 2016-06-22 01:30:41
适合我。谢谢! – 2016-06-28 18:08:24
也适用于自定义对话框。 – 2017-01-27 10:21:34
我试着隐藏键盘。您需要在布局文件中传递该方法。
public void setupUI(View view) {
// Set up touch listener for non-text box views to hide keyboard.
if (!(view instanceof EditText)) {
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard(LOGSignUpActivity.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);
}
}
}
调用此方法在您的MainAcitivity.class
public static void hideKeyboardwithoutPopulate(BaseActivity activity) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
activity.getCurrentFocus().getWindowToken(), 0);
}
对于那些谁正在寻找这个Xamarin的代码,在这里你去:
public override bool DispatchTouchEvent(MotionEvent ev)
{
try
{
View view = CurrentFocus;
if (view != null && (ev.Action == MotionEventActions.Up || ev.Action == MotionEventActions.Move) && view is EditText && !view.Class.Name.StartsWith("android.webkit."))
{
int[] Touch = new int[2];
view.GetLocationOnScreen(Touch);
float x = ev.RawX + view.Left - Touch[0];
float y = ev.RawY + view.Top - Touch[1];
if (x < view.Left || x > view.Right || y < view.Top || y > view.Bottom)
((InputMethodManager)GetSystemService(InputMethodService)).HideSoftInputFromWindow((Window.DecorView.ApplicationWindowToken), 0);
}
}
catch (System.Exception ex)
{
}
return base.DispatchTouchEvent(ev);
}
可以请帮问题清楚了吗? – 2012-01-02 04:59:52
我编辑问题。希望它更容易理解。 – Chrishan 2012-01-02 05:10:22
也许是一个重复的问题[这](http://stackoverflow.com/questions/4005728/hide-default-keyboard-on-click-in-android),检查出来 – manuzhang 2012-01-02 05:10:48