android edittext 只显示数字

需求

想要键盘只能显示数字
-w400

实现

概括

主要使用 inputTypenumberPassword 实现

代码

1. 声明类型

<EditText
    android:id="@+id/input"
    android:textColor="#18181A"
    android:maxLines="1"
    android:textSize="14sp"
    android:focusable="true"
    android:layout_marginStart="12dp"
    android:layout_marginEnd="12dp"
    android:layout_marginBottom="14sp"
    android:layout_marginTop="14sp"
    android:inputType="numberPassword"
    android:hint="1-20,000"
    android:layout_marginRight="16dp"
    android:background="@color/transparent"
    android:layout_toStartOf="@+id/unit"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:importantForAutofill="no" />

2. 让密码显示出来

binding.input.transformationMethod = object : TransformationMethod {
            override fun getTransformation(source: CharSequence?, view: View?): CharSequence? {
            // 直接显示密码
                return source!!
            }

            override fun onFocusChanged(view: View?, sourceText: CharSequence?, focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {

            }

        }