android - 以编程方式设置 EditText 数字

我实际上是在尝试以编程方式设置 EditText 的数字值。到目前为止,我有:

weightInput.setInputType(InputType.TYPE_CLASS_PHONE);
weightInput.setKeyListener(DigitsKeyListener.getInstance());

这很好,但我也希望能够包含小数位 (.)。有什么想法吗?

最佳答案

试试这个:

<EditText
    android:inputType="number"
    android:digits="0123456789."
/>

来自代码:

weightInput.setKeyListener(DigitsKeyListener.getInstance("0123456789."));

但是,它允许用户包含多个“.”。 请参阅 JoeyRA 对实数的回答。

https://stackoverflow.com/questions/7300490/

相关文章:

android - 如何使用 Android Studio 查看共享首选项文件?

android - 通过代码将 TextView 的宽度设置为 wrap_content

java - Android:如何以编程方式启用/禁用 Wifi 或 Internet 连接

android - 如何使用它自己的操作按钮关闭 Snackbar?

android - NoSuchFieldError : No static field listV

android - Android PopupWindow Activity 时背景模糊或变暗

android - 什么是 CSS 的 dp(密度独立像素)单位?

android - 在 AsyncTask 中获取上下文

android - Kotlin 错误 : Could not find org. jetbrain

android - 何时清除 Android 中的缓存目录?