Chr*_*han 28 android onclick android-softkeyboard
当用户点击Edittext以外的任何地方时,我需要在android中隐藏softkeypad.iphone有很多帮助,但不适用于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);
}
});
Run Code Online (Sandbox Code Playgroud)
提前致谢 !
Lal*_*ani 56
你可以onTouchEvent()用来隐藏Softkeyboard.
@Override
public boolean onTouchEvent(MotionEvent event) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.
INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
return true;
}
Run Code Online (Sandbox Code Playgroud)
虽然这个解决方案有效,但我建议最好使用下面的答案,因为它提供了关闭键盘触摸其他任何地方然后EditText的最佳解决方案.
Hir*_*tel 27
我这样做了:
在您的活动中添加代码.
这也适用于Fragment,无需在Fragment中添加此代码.
@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);
}
Run Code Online (Sandbox Code Playgroud)
希望这会帮助你.
Lal*_*ani 14
我找到的最好的工作是使用如下,
覆盖dispatchTouchEvent()并尝试获取的区域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);
}
Run Code Online (Sandbox Code Playgroud)
计算View的4个角的方法(这里是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;
}
Run Code Online (Sandbox Code Playgroud)
通过使用上面的代码,我们可以检测区域,EditText我们可以检查屏幕上的触摸是否是区域的一部分EditText.如果它的一部分EditText不做任何事情让触摸完成它的工作,如果触摸不包含区域,EditText那么只需关闭软键盘.
******编辑******
我刚刚发现了另一种方法,如果我们不想将任何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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36014 次 |
| 最近记录: |