android EditText默认数字键盘,能输入字母,数字和中文
最近遇到这样的市场需求,EditText默认弹出来是数字键盘,可以相互切换输入字母,数字和中文。还有就是一打开页面就弹出数字软键盘。刚开始对这个还是挺反感,因为这要求太细了。后面还是测试研究一番。
1、最佳方案
<EditText android:id="@+id/edtProjectName" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null" android:gravity="right|center" android:paddingRight="@dimen/signal_perference_pad_arrow_text_interval" android:textSize="@dimen/signal_perference_right_select_item_text_size" />xml 就是简单点,不要做多余的限制。
Java 实现
edtProjectName = (EditText) findViewById(R.id.edtProjectName); edtProjectName.setRawInputType(Configuration.KEYBOARD_QWERTY);
这个测试,可以实现弹出来时候默认是数字键盘,可以相互切换输入字母,中文等。就是输入还是没有限制的。
2、setRawInputType源码
/** * Directly change the content type integer of the text view, without * modifying any other state. * @see #setInputType(int) * @see android.text.InputType * @attr ref android.R.styleable#TextView_inputType */ public void setRawInputType(int type) { if (type == InputType.TYPE_NULL && mEditor == null) return; //TYPE_NULL is the default value createEditorIfNeeded(); mEditor.mInputType = type; }
源码上面英文意思就是:只是改变输入键盘的显示(数字,文本等等),不改变输入类型的限制。
3、setInputType源码
/** * Set the type of the content with a constant as defined for {@link EditorInfo#inputType}. This * will take care of changing the key listener, by calling {@link #setKeyListener(KeyListener)}, * to match the given content type. If the given content type is {@link EditorInfo#TYPE_NULL} * then a soft keyboard will not be displayed for this text view. * * Note that the maximum number of displayed lines (see {@link #setMaxLines(int)}) will be * modified if you change the {@link EditorInfo#TYPE_TEXT_FLAG_MULTI_LINE} flag of the input * type. * * @see #getInputType() * @see #setRawInputType(int) * @see android.text.InputType * @attr ref android.R.styleable#TextView_inputType */ public void setInputType(int type) { final boolean wasPassword = isPasswordInputType(getInputType()); final boolean wasVisiblePassword = isVisiblePasswordInputType(getInputType()); setInputType(type, false); final boolean isPassword = isPasswordInputType(type); final boolean isVisiblePassword = isVisiblePasswordInputType(type); boolean forceUpdate = false; if (isPassword) { setTransformationMethod(PasswordTransformationMethod.getInstance()); setTypefaceFromAttrs(null /* fontFamily */, MONOSPACE, 0); } else if (isVisiblePassword) { if (mTransformation == PasswordTransformationMethod.getInstance()) { forceUpdate = true; } setTypefaceFromAttrs(null /* fontFamily */, MONOSPACE, 0); } else if (wasPassword || wasVisiblePassword) { // not in password mode, clean up typeface and transformation setTypefaceFromAttrs(null /* fontFamily */, -1, -1); if (mTransformation == PasswordTransformationMethod.getInstance()) { forceUpdate = true; } } boolean singleLine = !isMultilineInputType(type); // We need to update the single line mode if it has changed or we // were previously in password mode. if (mSingleLine != singleLine || forceUpdate) { // Change single line mode, but only change the transformation if // we are not in password mode. applySingleLine(singleLine, !isPassword, true); } if (!isSuggestionsEnabled()) { mText = removeSuggestionSpans(mText); } InputMethodManager imm = InputMethodManager.peekInstance(); if (imm != null) imm.restartInput(this); }
edtProjectName.setInputType(InputType.TYPE_CLASS_NUMBER);
当然,在xml里面也可以设置这个属性。
这个方法就是设置输入类型的限制了。
通过源码就可以知道setRawInputType和setInputType区别。
4、digits 限制
edittext 的xml属性里面可以设置android:digits属性,就是限制输入的字符。
例如只输入数字可以android:digits="1234567890" 或者带有字母android:digits="1234567890abcdef"
那么在Java中实现,可以这种形式
edtProjectName = (EditText) findViewById(R.id.edtProjectName); String dig = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"; edtProjectName.setKeyListener(DigitsKeyListener.getInstance(dig));
这种设置:弹出软键盘也是数字键盘,可以相互切换输入字母,数字。但不能输入中文。
源码:
/** * Sets the key listener to be used with this TextView. This can be null * to disallow user input. Note that this method has significant and * subtle interactions with soft keyboards and other input method: * see {@link KeyListener#getInputType() KeyListener.getContentType()} * for important details. Calling this method will replace the current * content type of the text view with the content type returned by the * key listener. * <p> * Be warned that if you want a TextView with a key listener or movement * method not to be focusable, or if you want a TextView without a * key listener or movement method to be focusable, you must call * {@link #setFocusable} again after calling this to get the focusability * back the way you want it. * * @attr ref android.R.styleable#TextView_numeric * @attr ref android.R.styleable#TextView_digits * @attr ref android.R.styleable#TextView_phoneNumber * @attr ref android.R.styleable#TextView_inputMethod * @attr ref android.R.styleable#TextView_capitalize * @attr ref android.R.styleable#TextView_autoText */ public void setKeyListener(KeyListener input) { setKeyListenerOnly(input); fixFocusableAndClickableSettings(); if (input != null) { createEditorIfNeeded(); try { mEditor.mInputType = mEditor.mKeyListener.getInputType(); } catch (IncompatibleClassChangeError e) { mEditor.mInputType = EditorInfo.TYPE_CLASS_TEXT; } // Change inputType, without affecting transformation. // No need to applySingleLine since mSingleLine is unchanged. setInputTypeSingleLine(mSingleLine); } else { if (mEditor != null) mEditor.mInputType = EditorInfo.TYPE_NULL; } InputMethodManager imm = InputMethodManager.peekInstance(); if (imm != null) imm.restartInput(this); }
5、进入页面就弹出输入软键盘
<activity android:name=".project.ProjectActivity" android:windowSoftInputMode="stateAlwaysVisible|stateUnchanged"/>
在activity注册配置时候加上这个属性控制:android:windowSoftInputMode="stateAlwaysVisible|stateUnchanged"
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇: vs2008 创建对话框MFC应用程序调试c++代码