一:在开发中有时候需要需要自动弹出键盘和隐藏键盘。代码如下。
/** * 弹出键盘 * @param et */ protected void showKeyboard(EditText et) { ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)) .showSoftInputFromInputMethod(et.getWindowToken(), 0); }
/** * 隐藏键盘 */ protected void hideKeyboard(View v) { ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)) .hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); }
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0);//强制隐藏键盘,view为键盘视图,比如edittext
但在一些机型上或者popupwindow需要弹出键盘时会失效。这个时候用切换键盘完美解决问题:
首先要对弹出的EditText设置允许焦点
etSerch = (EditText) findViewById(R.id.act_nearby_et_keyword);
etSerch.setFocusable(true); etSerch.setFocusableInTouchMode(true); etSerch.requestFocus();//重新获取焦点 etSerch.clearFocus();//失去焦点
然后用下面的方法切换显示或者隐藏的时候用下面的方法即可
/** * 切换键盘 */ protected void switchKeyBoard(){ ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)). toggleSoftInput(0,InputMethodManager.HIDE_IMPLICIT_ONLY); }
二:数字键盘
<EditText android:id="@+id/act_regster_et_account_et01" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00000000" android:hint="输入手机号,仅限中国大陆地区" android:layout_toRightOf="@+id/act_regster_et_account_iv" android:layout_marginLeft="@dimen/company_5dp" android:layout_marginRight="@dimen/company_27dp" android:digits="1234567890." />
在java中
etWord = (EditText) findViewById(R.id.act_regster_et_account_et01); etWord.setInputType(EditorInfo.TYPE_CLASS_PHONE);
三:只能输入英文和数字的键盘
1.
private String dataID = "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM1234567890";2.
/** * 限制只能输入字母和数字,默认弹出英文输入法 */ edIdCard.setKeyListener(new DigitsKeyListener() { @Override public int getInputType() { return InputType.TYPE_TEXT_VARIATION_PASSWORD; } @Override protected char[] getAcceptedChars() { //char[] data = getStringData(R.string.login_only_can_input).toCharArray(); char[] data =dataID.toCharArray(); return data; } });