【问题标题】:EditTextPreference not showing numeric keyboard after migrating project to androidx将项目迁移到 androidx 后 EditTextPreference 不显示数字键盘
【发布时间】:2018-11-23 12:35:17
【问题描述】:

使用 Android Studio 提供的 Migrate to AndroidX... 功能将我的项目迁移到 AndroidX 并相应地更改我的依赖项以使所有内容都按预期运行后,我遇到了一个我无法解决的小问题解决。

要在我的应用程序中设置设备编号,我使用了如下在我的pref_screen.xml 中定义的 EditTextPreference,它在 PreferenceFragmentCompat 类中设置为 setPreferencesFromResource(R.xml.pref_screen, string)

<EditTextPreference
            android:icon="@drawable/ic_perm_device_information_black_24dp"
            android:inputType="number"
            android:key="change_device_id"
            android:maxLines="1"
            android:selectAllOnFocus="true"
            android:singleLine="true"
            android:summary="@string/settings_device_id"
            android:title="@string/pref_title_change_device_id" />

它曾经显示一个数字键盘来更改值,但在迁移到 AndroidX 后,它一直显示一个普通键盘,如下图所示。我尝试更改 inputType 并在 xml 中定义小数,但无济于事。迁移到 AndroidX 后是否更改了设置键盘的 inputType 或我遗漏了一些明显的东西?

android:inputType="numberDecimal"
android:digits="0123456789"

【问题讨论】:

标签: android android-preferences androidx edittextpreference


【解决方案1】:

从这里的答案: https://stackoverflow.com/a/55461028/7059947

将您的偏好设置为 EditTextPreference 并在绑定时使用 setInputType。 这拯救了我的一天:)

            EditTextPreference edpPrefernce = (EditTextPreference) pPreference;

            edpPrefernce.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
                @Override
                public void onBindEditText(@NonNull EditText editText) {

                    editText.setInputType(InputType.TYPE_CLASS_NUMBER);
                }
            });

【讨论】:

  • 如果有人需要小数,那么就使用这样的editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
【解决方案2】:

Google 尚未解决此问题。您可以使用 takisoft 修复此问题:

https://bintray.com/takisoft/android/com.takisoft.preferencex%3Apreferencex/1.0.0

添加到build.gradle(项目):

buildscript {
    ...
    repositories {
        maven {
            url  "https://dl.bintray.com/takisoft/android"
        }
    }
    ....
}

添加到build.gradle(应用模块):

implementation 'com.takisoft.preferencex:preferencex:1.0.0'

更改类中的导入和 XML 资源中的组件:

androidx.preference.EditTextPreference -> com.takisoft.preferencex.EditTextPreference
androidx.preference.PreferenceCategory -> com.takisoft.preferencex.PreferenceCategory
androidx.preference.PreferenceFragmentCompat -> com.takisoft.preferencex.PreferenceFragmentCompat

在您的 PreferenceFragmentCompat 子类中,将 onCreatePreferences(...) 声明更改为 onCreatePreferencesFix(...)

...瞧!旧参数,如 numericsingleLine 将恢复正常工作!

【讨论】:

    【解决方案3】:

    [已解决]

    添加到build.gradle(应用模块):

    implementation 'androidx.preference:preference:1.1.0-rc01'
    

    【讨论】:

    • 如何从 XML 中用 androidx 指定 inputType?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 2019-12-25
    • 1970-01-01
    • 2020-07-12
    相关资源
    最近更新 更多