我是靠谱客的博主 瘦瘦飞机,最近开发中收集的这篇文章主要介绍android edittext 字母数字键盘,android EditText默认数字键盘,能输入字母,数字和中文...,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

最近遇到这样的市场需求,EditText默认弹出来是数字键盘,可以相互切换输入字母,数字和中文。还有就是一打开页面就弹出数字软键盘。刚开始对这个还是挺反感,因为这要求太细了。后面还是测试研究一番。

1、最佳方案

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);

}

Java示例:

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.

*

* 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、进入页面就弹出输入软键盘

android:windowSoftInputMode="stateAlwaysVisible|stateUnchanged"/>

在activity注册配置时候加上这个属性控制:android:windowSoftInputMode="stateAlwaysVisible|stateUnchanged"

最后

以上就是瘦瘦飞机为你收集整理的android edittext 字母数字键盘,android EditText默认数字键盘,能输入字母,数字和中文...的全部内容,希望文章能够帮你解决android edittext 字母数字键盘,android EditText默认数字键盘,能输入字母,数字和中文...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部