Android 默认情况下带有数字键盘的 EditText,但允许使用字母字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3544214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EditText with number keypad by default, but allowing alphabetic characters
提问by Frank Bozzo
I have an EditText box and I want the default keyboard that comes up when it is selected to be the numeric keypad, since most of the timeusers will be entering numbers. However, I do want to allow users to enter alphabetic characters too, if they need to. Using android:inputType="number"
restricts input to numeric digits only. What options do I have?
我有一个 EditText 框,我希望在选择它作为数字小键盘时出现默认键盘,因为大多数时候用户将输入数字。但是,如果需要,我也希望允许用户输入字母字符。使用android:inputType="number"
将输入限制为仅数字。我有哪些选择?
回答by Diego Santamaria
Define your EditText in your xml this way:
以这种方式在您的 xml 中定义您的 EditText:
<EditText
android:id="@+id/txtOppCodigoCliente"
android:inputType="textVisiblePassword"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLength="11"
android:hint="@string/strCodigoCliente"/>
output: android:inputType="textVisiblePassword"
输出: android:inputType="textVisiblePassword"
A row of numbers and the rest of letters.
一行数字和其余字母。
回答by Sherif elKhatib
I would suggest a more Robust solution:
我会建议一个更健壮的解决方案:
Present the user with the ability to choose the input type:
为用户提供选择输入类型的能力:
NEEDED FIXES
需要修复
-better handling of showing/hiding the input selection buttons
-更好地处理显示/隐藏输入选择按钮
-probably remove the suggestions when the input type is letter
-当输入类型为字母时,可能会删除建议
Here is the activity:
这是活动:
package mobi.sherif.numberstexttextview;
import android.os.Bundle;
import android.app.Activity;
import android.text.InputType;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText mEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEdit = (EditText) findViewById(R.id.input);
mEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean focused) {
findViewById(R.id.llmenu).setVisibility(focused?View.VISIBLE:View.GONE);
}
});
}
public void onNumbers(View v) {
mEdit.setInputType(InputType.TYPE_CLASS_NUMBER);
}
public void onLetters(View v) {
mEdit.setInputType(InputType.TYPE_CLASS_TEXT);
}
}
Here is the activity's xml : activity_main.xml
这是活动的 xml : activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignWithParentIfMissing="true"
android:layout_above="@+id/llmenu" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" >
<requestFocus />
</EditText>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="@+id/llmenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#000000"
android:padding="0dp" >
<Button
android:id="@+id/buttonnumbers"
android:layout_width="0dp"
android:onClick="onNumbers"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Numbers" />
<Button
android:id="@+id/buttonletters"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onLetters"
android:text="Letters" />
</LinearLayout>
</RelativeLayout>
回答by Terel
Here is what you are looking for:
这是您要寻找的内容:
Define your EditText
in your xml this way:
EditText
以这种方式在您的 xml 中定义您的:
<EditText
android:id="@+id/etTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/hint">
</EditText>
And call following in your onCreate()
:
并在您的以下调用onCreate()
:
etTest = (EditText) findViewById(R.id.etTest);
etTest.setRawInputType(InputType.TYPE_CLASS_NUMBER);
// Note the "Raw"
Whenever you click into your EditText
now, you can enter digits AND letters! (Digit keyboard is shown first)
每当您点击EditText
现在,您就可以输入数字和字母!(首先显示数字键盘)
回答by Alpay
Unfortunately this is not possible, because only one inputType can be set for an EditText. The following will just show alphabatic characters on buttons.
不幸的是,这是不可能的,因为只能为 EditText 设置一个 inputType。以下将仅显示按钮上的字母字符。
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text|number" />
However, what I can suggest you is, set your input type to numeric, and just add a small switchKeyboard button on your form close to the numpad. And set its onClick
但是,我可以建议您将输入类型设置为数字,然后在表单上靠近小键盘的位置添加一个小的 switchKeyboard 按钮。并设置其 onClick
editText1.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
so that users can switch to alphabetic characters, too.
这样用户也可以切换到字母字符。
回答by Paresh Mayani
Basically, EditText inputtype is used to set the input typein your EditText.
基本上,EditText inputtype 用于在 EditText 中设置输入类型。
The possible values for the android:inputtype
are:
的可能值为android:inputtype
:
- text
- textCapCharacters
- textCapWords
- textCapSentences
- textAutoCorrect
- textAutoComplete
- textMultiLine
- textImeMultiLine
- textNoSuggestions
- textUri
- textEmailAddress
- textEmailSubject
- textShortMessage
- textLongMessage
- textPersonName
- textPostalAddress
- textPassword
- textVisiblePassword
- textWebEditText
- textFilter
- textPhonetic
- number
- numberSigned
- numberDecimal
- phone
- datetime
- date
- time
- 文本
- textCapCharacters
- 文字大写字
- textCapSentences
- 文本自动更正
- 文本自动完成
- 文本多行
- 文本输入多行
- 文本无建议
- 文本Uri
- 文本电子邮件地址
- 文本电子邮件主题
- 文本短消息
- 文本长消息
- 文字人名
- 文本邮政地址
- 文本密码
- 文本可见密码
- 文字网页编辑文字
- 文本过滤器
- 文字拼音
- 数字
- 数字签名
- 数字十进制
- 电话
- 约会时间
- 日期
- 时间
then you can use any value as showing in example:
然后您可以使用任何值,如示例所示:
<EditText android:id="@+id/EditText01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:inputType="number"
android:text="1212223"/>
回答by Shaista Naaz
Asus tablet has that feature, they have customized the default keyboard. And the keyboard contains the numbers and alphabets only. I think your requirement solution is too to customize the default keyboard
华硕平板有这个功能,他们自定义了默认键盘。键盘只包含数字和字母。我认为您的要求解决方案太要自定义默认键盘
回答by dilix
In OnCreate in Activity do this:
在 Activity 中的 OnCreate 中执行以下操作:
t = (EditText) findViewById(R.id.test);
t.setKeyListener(new KeyListener() {
@Override
public boolean onKeyUp(View view, Editable text, int keyCode, KeyEvent event) {
return false;
}
@Override
public boolean onKeyOther(View view, Editable text, KeyEvent event) {
return false;
}
@Override
public boolean onKeyDown(View view, Editable text, int keyCode, KeyEvent event) {
if (keyCode == 67) { // Handle backspace button =)
if (text.length() > 0) {
t.setText(text.subSequence(0, text.length() - 1));
t.setSelection(text.length()-1);
}
}
return false;
}
@Override
public int getInputType() {
return InputType.TYPE_CLASS_NUMBER;
}
@Override
public void clearMetaKeyState(View view, Editable content, int states) {
}
});
In this case you type is number
and android will show numeric keyboard, but you can input whatever you want.
Also you can override other methods as you wish.
在这种情况下,您输入 isnumber
并且 android 将显示数字键盘,但您可以输入任何您想要的内容。您也可以根据需要覆盖其他方法。
回答by Shreyash Mahajan
You can follow Paresh Mayani answer.
您可以按照 Paresh Mayani 的回答进行操作。
And in advice you can use android:inputType="textPhonetic"
for the inputType Text and Number based on user selection.
在建议中,您可以android:inputType="textPhonetic"
根据用户选择使用inputType 文本和数字。
Hope you will got my point.
希望你能明白我的意思。
回答by slaman
Here is how I achieved it...
这是我如何实现它...
android:inputType="textPhonetic|numberDecimal" android:digits="/0123456789.abcdefghijklmnopqrstuvwxyz"
android:inputType="textPhonetic|numberDecimal" android:digits="/0123456789.abcdefghijklmnopqrstuvwxyz"
Only characters specified by digits will be allowed.
仅允许由数字指定的字符。
Please note that the keyboard shown is that of the dialer, not the typical keyboard. For me, it achieved what I needed to entirely within the XML.
请注意,显示的键盘是拨号器的键盘,而不是典型的键盘。对我来说,它完全在 XML 中实现了我所需要的。
回答by Naveed Ahmad
You should not use any inputType filter in this case, just add the below line in your EditText Property
在这种情况下,您不应使用任何 inputType 过滤器,只需在 EditText 属性中添加以下行
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "
Now only these characters will be allowed to enter in the EditText. The complete code of your edittext will be like this:
现在只允许这些字符进入 EditText。您的编辑文本的完整代码将如下所示:
<EditText
android:id="@+id/etTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "
android:hint="@string/hint">