user2179825
user2179825

Reputation: 59

Editext Not taking Numerical input due to setOnKeyListener

I have an EditText, its taking inputs of letters and different symbols however not numbers. Additionally, I have set the EditText on setOnKeyListener to close the virtual keyboard by pressing Enter. Observed and found out that it failing to take numerical inputs due to the below code

Code for setting virtual keyboard to hide after pressing enter

durOnTreadmill.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) 
    {
        if (keyCode == KeyEvent.KEYCODE_ENTER) 
        { 
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(durOnTreadmill.getWindowToken(), 0);
        }
        return true;
    }

});

my EditText in XML

 <EditText
        android:id="@+id/durOnTreadmill"
        android:layout_width="129dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="32dp"
        android:inputType="text"
        android:hint="hh:mm:ss" />

Where am I going wrong?

Upvotes: 4

Views: 934

Answers (2)

Tushar
Tushar

Reputation: 8049

Copying my comment here:

You should only return true from onKey if you're handling the input. I.e., move the return true inside your if block and return false otherwise.

Upvotes: 2

JRowan
JRowan

Reputation: 7114

android:imeOptions="actionDone"

in you EditText xml will dissmiss the keyboard when the user hits enter and you say that it wont except numbers you have the input set to "text"

take this line out:

android:inputType="text"

Upvotes: 1

Related Questions