android - 使用支持首选项 fragment 时 inputType 在 EditTextPreference 中不起作用

标签 android android-preferences

当我使用支持库作为首选项时,EditTextPreference 中的 inputType 属性不起作用。

我正在使用的支持库的版本

com.android.support:preference-v7:27.1.1

但是当我使用首选项 fragment 时 inputType 似乎可以工作 有人可以解释为什么吗?

这里是EditTextPreference的代码

  <EditTextPreference
            android:id="@+id/computer_ip"
            android:defaultValue="192.168.0.103"
            android:hint="Computer Ip"
            android:inputType="numberDecimal"
            android:key="@string/computerIp"
            android:padding="8dp"
            android:textSize="16sp"
            android:title="Computer Ip" />

在这段代码中,当使用支持库时,inputType 是 numberDecimal,numberDecimal 对键盘没有任何影响,而是显示带有所有字母的键盘。

最佳答案

有一个简单的解决方案,只需使用

EditTextPreference preference = (EditTextPreference) findPreference("key");
EditText et = preference.getEditText();
if (et != null) {
    et.setInputType(InputType.TYPE_CLASS_NUMBER);
    //in fact, you can do whatever you can normally with an EditText here
}

关于android - 使用支持首选项 fragment 时 inputType 在 EditTextPreference 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50889283/

相关文章:

android - startActivityForResult 和 FLAG_ACTIVITY_CLEAR_TOP

android - 非 Activity 类中的 SharedPreference

android - 尝试从首选项调用 'explicit' Intent 以打开默认包中的 Activity 时出现 ActivityNotFoundException?

安卓 : Where should a OnSharedPreferenceChangeListener be defined/registered

android - 如何仅在我的数字时钟中显示下午

java - 如何通过文件上传将数据发送到服务器

android - 如何在 RxJava 中链接任务

android - 自定义标题栏-Android崩溃

android - 如何以编程方式设置 SharedPreferences 的默认值?

android - 为什么使用AndroidX时嵌套的PreferenceScreen打不开?