我有一个包含用于输入的EditText的片段,但是现在我想在用户单击EditText之外的屏幕时关闭键盘。
我知道如何在活动中执行此操作,但是对于片段来说似乎有所不同。
我正在view.onTouchListener上调用此方法
public static void hideSoftKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}
任何人都有解决方案,谢谢
在片段的父活动中,重写以下方法:
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if ( v instanceof EditText) {
Rect outRect = new Rect();
v.getGlobalVisibleRect(outRect);
if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
v.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
return super.dispatchTouchEvent(event);
}
并且在片段的布局中使用此属性:
android:focusableInTouchMode="true"
希望这会对您有所帮助。
使用此方法效果很好
public static void hideKeyBoardMethod(final Context con, final View view) {
try {
view.post(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) con.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
您可以使用此方法对我来说效果很好。只需像这样传递布局的根元素的引用
setupUI(rootView.findViewById(R.id.rootParent))
setupUI的代码在下面。
public void setupUI(View parentView) {
//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();
handleCallBack();
return false;
}
});
}
您也可以这样做
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
);
希望有帮助!
如果要在editText之外触摸,则键盘会自动隐藏,因此请使用此代码
public boolean dispatchTouchEvent(MotionEvent event) {
View view = getCurrentFocus();
boolean ret = super.dispatchTouchEvent(event);
if (view instanceof EditText) {
View w = getCurrentFocus();
int location[] = new int[2];
w.getLocationOnScreen(location);
float x = event.getRawX() + w.getLeft() - location[0];
float y = event.getRawY() + w.getTop() - location[1];
if (event.getAction() == MotionEvent.ACTION_DOWN
&& (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom())) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
}
}
return ret;
}
如果使用片段,请使用下面的代码
View view = inflater.inflate(R.layout.fragment_test, container, false);
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_MOVE){
dispatchTouchEvent(event);
}
return true;
}
});
//here the rest of your code
return view;
应用此代码并调用dispatchTouchEvent();方法
i刚刚覆盖了活动的所有片段中的dispatchTouchEvent()方法。
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if ( v instanceof EditText) {
Rect outRect = new Rect();
v.getGlobalVisibleRect(outRect);
if (!outRect.contains((int)ev.getRawX(), (int)ev.getRawY())) {
v.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
并在主布局中的每个片段XML中设置属性
android:focusableInTouchMode="true"