问题
Good afternoon.
I have an EditText that may contain numbers and letters. By deploying the keyboard, is there any way to deploy it in numerical form?.
If I put the EditText as numeric I do, but that's not my case.
Thank you very much.
回答1:
Try this:
EditText myEditText = (EditText)findViewById(R.id.myEditText);
myEditText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
Then set it back to alphanumeric when you are done with numeric..
回答2:
You can:
- Hide/Show different EditTexts that have different input types
- Dynamically set the input type of your EditText as needed (editText.setInputType(InputType.TYPE_WHATEVER)
You cannot:
- Default an IME on an EditText with inputType text to the "Numeric" layout.
回答3:
Using XML:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edittext"
android:inputType="number"/>
or
Using code:
EditText editText=new EditText(this);
editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
回答4:
In your oncreate method you can put this bloc of code :
final EditText myEditJava = (EditText) findViewById(R.id.myEdit);
myEditJava.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (s.toString().matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+"))
myEditJava.setInputType(InputType.TYPE_CLASS_PHONE);
else
myEditJava.setInputType(InputType.TYPE_CLASS_TEXT);
}
});
So when you put the first char if It will be a Numeric Value the keyborad will be displayed on the Numeric input mode , else on text input mode.
来源:https://stackoverflow.com/questions/9912548/android-numeric-keyboard