Thursday, September 29, 2016

android - EditText filter characters

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ef39a4"
    >
    <!--
    android:digits
        If set, specifies that this TextView has a numeric input method and that these specific
        characters are the ones that it will accept. If this is set, numeric is implied to
        be true. The default is false.

        Must be a string value, using '\\;' to escape characters such
        as '\\n' or '\\uxxxx' for a unicode character.

        This may also be a reference to a resource (in the form "@[package:]type:name") or
        theme attribute (in the form "?[package:][type:]name") containing a value of this type.

        This corresponds to the global attribute resource symbol digits.
    -->
    <!--
        android:inputType
            The type of data being placed in a text field, used to help an input method
            decide how to let the user enter text. The constants here correspond to those defined
            by InputType. Generally you can select a single value, though some can be combined
            together as indicated. Setting this attribute to anything besides none also
            implies that the text is editable.

            Must be one or more (separated by '|') of the following constant values.

            textFilter
                Text that is filtering some other data. Corresponds to
                TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_FILTER.
    -->
    <!--
        This EditText will only accept input to 012345ABCDEF characters.
    -->
    <EditText
        android:id="@+id/et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center_horizontal"
        android:textSize="25sp"
        android:padding="5dp"
        android:layout_marginTop="50dp"
        android:hint="Write some text here."
        android:inputType="textFilter"
        android:digits="012345ABCDEF"
        />
</android.support.design.widget.CoordinatorLayout>