EditText is not taking numeric input

扶醉桌前 提交于 2019-12-06 07:17:15

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!