我有一个EditText框,希望在选中它时默认弹出数字键盘,因为大多数情况下用户会输入数字。但是,如果需要,我也想允许用户输入字母字符。使用android:inputType="number"
限制了输入仅限于数字。我有哪些选项?
我有一个EditText框,希望在选中它时默认弹出数字键盘,因为大多数情况下用户会输入数字。但是,如果需要,我也想允许用户输入字母字符。使用android:inputType="number"
限制了输入仅限于数字。我有哪些选项?
在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"/>
输出: android:inputType="textVisiblePassword"
一行数字和其他字母。
我建议采用更为严谨的解决方案:
让用户能够选择输入类型:
需要修复的问题
- 更好地处理显示/隐藏输入选择按钮
- 当输入类型为字母时,可能要删除建议
以下是活动:
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);
}
}
这里是活动的 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>
以下是您要查找的内容:
按照以下方式在xml中定义您的EditText
:
<EditText
android:id="@+id/etTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/hint">
</EditText>
在您的onCreate()
中调用以下内容:
etTest = (EditText) findViewById(R.id.etTest);
etTest.setRawInputType(InputType.TYPE_CLASS_NUMBER);
// Note the "Raw"
现在,每当您点击 EditText
时,您可以输入数字和字母!(数字键盘首先显示)
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text|number" />
editText1.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
这样用户也可以切换到字母字符。
TYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_SIGNED
。但是您永远不能混合使用number
和text
这样的类。 - OneWorldnumber|text
时,还有一个特定的键盘,这意味着您可以通过长按输入字母,但实际上并不起作用。看起来对我来说相当有缺陷,或者至少是不完整的。 - masc3dTYPE_MASK_CLASS
的描述中:
决定给出的文本的总体类别的位掩码。支持的类别有:TYPE_CLASS_TEXT、TYPE_CLASS_NUMBER、TYPE_CLASS_PHONE、TYPE_CLASS_DATETIME。
IME作者:如果这个类别不是你理解的其中之一,请假设TYPE_CLASS_TEXT,并且没有变化或标志。 - ToolmakerSteve<EditText android:id="@+id/EditText01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:inputType="number"
android:text="1212223"/>
Asus平板电脑具有该功能,他们已经定制了默认键盘。键盘只包含数字和字母。我认为您的要求解决方案也应该定制默认键盘。
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "
现在只允许输入这些特定字符到EditText中。
您的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">
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) {
}
});
number
,Android将显示数字键盘,但您可以输入任何内容。
此外,您可以根据需要覆盖其他方法。android:inputType="textPhonetic"
作为输入类型文本和数字。以下是我实现它的方法...
android:inputType="textPhonetic|numberDecimal" android:digits="/0123456789.abcdefghijklmnopqrstuvwxyz"
只有digits指定的字符将被允许输入。
请注意,显示的键盘是拨号键盘,而不是普通键盘。对我来说,在XML中完全实现了我需要的功能。