Android - How to set numeric EditText to a specified number of digits

时间:2015-06-15 14:48:53

标签: android android-edittext

I want an EditText to contain exactly 8 digits (like bank accounts). Of course in addition to :

android:inputType="number"

Is there an option in XML or do I have to manage this programmatically ?

5 个答案:

答案 0 :(得分:1)

I think there is no way just in xml. You have to validate it programmatically.

@Override
public void afterTextChanged(Editable s) {
    // validation code goes here
}

答案 1 :(得分:1)

Programmatically.

Also, you can save time by using existing library EditText with validation. You can use a regex type with following pattern [0-9]{8}.

答案 2 :(得分:0)

Try the following line in your EditText:

android:maxLength="8"

答案 3 :(得分:0)

This is my phone edit text:

<EditText
    style="@style/ObligatoryEditTextStyle"
    android:id="@+id/etxtClientPhone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentLeft="true"
    android:gravity="left"
    android:textColor="@color/hub_blue_normal"
    android:visibility="visible"
    android:selectAllOnFocus="true"
    android:digits="0123456789"
    android:maxLength="8"/>

Tell me if I helped you and good programming!!

答案 4 :(得分:-1)

Change input type in decimal, then set maxLength="8".

android:inputType="decimal"
android:maxLength="8"