1、一打开页面,自动弹出软键盘
editText.postDelayed(new Runnable() {
@Override
public void run() {
editText.requestFocus();
InputMethodManager manager = ((InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE));
if (manager != null) manager.showSoftInput(v, 0);
}
}, 1000);
2、触摸软键盘外部收起软键盘
- 根布局布局添加以下属性:抢夺 EditText 的焦点
android:clickable="true"
android:focusableInTouchMode="true"
//点击软键盘外部,收起软键盘
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if(!hasFocus){
InputMethodManager manager = ((InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE));
if (manager != null)
manager.hideSoftInputFromWindow(view.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
}
}
});