Nachding
Nachding

Reputation: 465

EditText does not accept numbers Android

I am developing an app for Android and I have a problem with my EditText : I can't write numbers on it. I created a function in order to allow my users to valid an answer in an EditText when they click on "OK" but, with this function, they can't write numbers on my EditTexts.

Here is my function's code :

ed.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                ed.setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {
                            if (keyCode == 66){
                                if (ed.getText().toString().equals(rep)) {
                                    k++;
                                    if (k == 1) {
                                                Intent ActivityTransition = new Intent(Level6f.this, Transition6.class);
                                                startActivityForResult(ActivityTransition, KEY1);
                                    }
                                }
                                else {
                                    ed.getText().clear();
                                    Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                                    vib.vibrate(50);
                                }
                            }
                        if (keyCode == 67) {
                            ed.getText().clear();
                        }
                        return true;
                    }
                });

            }
        });

Here my XML EditText's code :

 <EditText
        android:layout_width="110dp"
        android:layout_height="45dp"
        android:inputType="textVisiblePassword"
        android:id="@+id/editText"

        android:textSize="22sp"
        android:layout_above="@id/curseur"
        android:layout_centerHorizontal="true"/>

Upvotes: 0

Views: 404

Answers (3)

Tushar Patil
Tushar Patil

Reputation: 326

Use this line in your xml (EditText)   

android:inputType="number|numberDecimal"

this line allows number and number decimal.

Example -

<EditText
   android:layout_width="110dp"
   android:layout_height="45dp"
   android:inputType="number|numberDecimal|textVisiblePassword"    
   android:id="@+id/editText"
   android:textSize="22sp"
   android:layout_above="@id/curseur"
   android:layout_centerHorizontal="true"/>

Upvotes: 2

SmulianJulian
SmulianJulian

Reputation: 802

Create your own ok button. Surround editText with a Tablerow and put the ok button in there.

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="110dp"
    android:layout_height="45dp"
    android:layout_above="@id/curseur" >
<EditText
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:id="@+id/editText"
    android:textSize="22sp"
    android:layout_centerHorizontal="true"/>

 <Button
        android:id="@+id/okbutton"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="Ok"
        android:textColor="#000000"
        android:textSize="22sp"
        android:textStyle="bold" />

</TableRow>

Upvotes: 1

Remees M Syde
Remees M Syde

Reputation: 2609

I think you can use

android:imeOptions="actionDone" 

in your xml instead of android:inputType="textVisiblePassword"

Upvotes: 2

Related Questions