xml根布局中添加:
android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true"
Activity/Fragment中添加:
//点击软键盘外部,收起软键盘
binding.passwordEt.setOnFocusChangeListener((view, hasFocus) -> {
if(!hasFocus){
KeyboardUtil.closeSoftKeyboard(view);
}
});
KeyboardUtil中:
/**
* 关闭软键盘
*
* @param view 当前页面上任意一个可用的view
*/
public static void closeSoftKeyboard(View view) {
if (view == null || view.getWindowToken() == null) {
return;
}
InputMethodManager imm = (InputMethodManager)
view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}