Android EditText 限制输入内容

三味码屋 2023年05月22日 554次浏览

通过 InputType 实现

代码中设置

通过 android.widget.TextView#setInputType(int) 设置 InputType。

editText.inputType = ${inputType}

例如:
设置输入内容为密码:

editText.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD

设置输入内容为数字:

editText.inputType = InputType.TYPE_CLASS_NUMBER

设置输入内容为小数:

editText.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL

设置输入内容可以显示负数:

editText.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED

XML中设置

在 XML 布局文件中设置 EditText 的 android:inputType 属性。
例如:
设置输入内容为密码:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPassword" />

设置输入内容为数字:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number" />

设置输入内容为小数:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal" />

设置输入内容可以显示负数:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberSigned" />

通过 keyListener 实现

可以使用 android.text.method.NumberKeyListener 对输入内容进行过滤。
NumberKeyListener:

/**
 * For numeric text entry
 * <p></p>
 * As for all implementations of {@link KeyListener}, this class is only concerned
 * with hardware keyboards.  Software input methods have no obligation to trigger
 * the methods in this class.
 */
public abstract class NumberKeyListener extends BaseKeyListener
    implements InputFilter
{
    /**
     * You can say which characters you can accept.
     */
    @NonNull
    protected abstract char[] getAcceptedChars();

		…………
}

例如:
设置输入内容为数字:

editText.keyListener = object : NumberKeyListener() {
    override fun getInputType(): Int {
        // 返回 InputType
        return InputType.TYPE_CLASS_NUMBER
    }

    override fun getAcceptedChars(): CharArray {
        // 返回允许输入的内容
        return charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
    }
}

设置只允许输入数字和小数点:

editText.keyListener = object : NumberKeyListener() {
    override fun getInputType(): Int {
        // 返回 InputType
        return InputType.TYPE_CLASS_NUMBER
    }

    override fun getAcceptedChars(): CharArray {
        // 返回允许输入的内容
        return charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.')
    }
}

设置只允许输入数字和-:

editText.keyListener = object : NumberKeyListener() {
    override fun getInputType(): Int {
        // 返回 InputType
        return InputType.TYPE_CLASS_NUMBER
    }

    override fun getAcceptedChars(): CharArray {
        // 返回允许输入的内容
        return charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-')
    }
}

设置只允许输入字母:

editText.keyListener = object : NumberKeyListener() {
    override fun getInputType(): Int {
	// 返回 InputType
        return InputType.TYPE_CLASS_TEXT
    }

    override fun getAcceptedChars(): CharArray {
	// 返回允许输入的内容
        return charArrayOf(
            'A', 'B', 'C', 'D', 'E', 'F', 'G',
            'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T', 'U',
            'V', 'W', 'X', 'Y', 'Z',
            'a', 'b', 'c', 'd', 'e', 'f', 'g',
            'h', 'i', 'j', 'k', 'l', 'm', 'n',
            'o', 'p', 'q', 'r', 's', 't', 'u',
            'v', 'w', 'x', 'y', 'z',
        )
    }
}