/** * Created by jiangchen on 2017/5/10- 8:59. * 说明:隐藏小键盘 */ public class HideKeyboard { /** * 2017/5/10 11:14 * Created by jiangchen * 作用:隐藏小键盘 */ public static void hideBoard(Activity activity) { final View v = activity.getWindow().peekDecorView(); if (v != null && v.getWindowToken() != null) { InputMethodManager imm = (InputMethodManager) activity.getSystemService(activity.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0); } } /** * 2017/5/10 13:35 * Created by jiangchen * 作用:小键盘里面的回车键隐藏小键盘 */ public static void enterHide(EditText editText, final Activity activity) { editText.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { //回车键隐藏小键盘 if (keyCode == KeyEvent.KEYCODE_ENTER) { //如果action分为up,move,down如果不做一个判断,下面的方法就会执行两次甚至多次 if (event.getAction() == KeyEvent.ACTION_UP) { final View vv = activity.getWindow().peekDecorView(); if (vv != null && vv.getWindowToken() != null) { InputMethodManager imm = (InputMethodManager) activity.getSystemService(activity.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(vv.getWindowToken(), 0); } } //return true就是拦截输入信息 return true; } else { //return false就是不拦截输入信息,即输入的字符会显示到edittext上 return false; } } }); } }
android 隐藏小键盘工具类,可用
最新推荐文章于 2021-11-04 20:44:15 发布