I have an EditText view in my android application. My EditText is not taking integer values as an input however it is taking all the other ones. the following is my EditText View.
<EditText
android:id="@+id/txtSearchItems"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:visibility="visible"
android:hint="Search"
android:textSize="14dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true">
</EditText>
On the possible issue that i faced once and suspects is: The EditText might be contained in an XML layout file and the layout file will be used for a Dialog instance. The problem occurs when the onKeyListener of the Dialog instance is returning some invalid default value i.e. 'return true' for irrelevant keys. In such case your EditText will be behaving strangely. The sample code for the scenario is
Dialog d=new Dialog(context);
d.setContentView(R.layout.mylayout);
d.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
Log.i("onkey", keyCode + " == Onkey");
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return true; // !!! this is wrong it should be return false;
}
});
}
Try this?
android:inputType="number"
android:digits="0123456789"
If it works, you can add letters in with the numbers in digits that you want to allow. Here's a handy list of all the inputTypes you could try. They mostly change what appears on the soft keyboard.
You need to add android:inputType="number" in your xml just like follows,
<EditText
android:id="@+id/txtSearchItems"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:visibility="visible"
android:hint="Search"
android:textSize="14dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true"
android:inputType="number"> <------------- this line added
</EditText>
来源:https://stackoverflow.com/questions/12489447/edittext-is-not-taking-numeric-input