java - 首选项 XML 中不允许使用该元素?

标签 java android android-preferences

我正在 android 项目中研究偏好。我想在 EditTextPreference 中获取整数值。我搜索了这个主题,并且可以使用这个 java 类。

import androidx.preference.EditTextPreference;

public class IntegerEditTextPreference extends EditTextPreference {

    public IntegerEditTextPreference(Context context) {
        super(context);
    }

    public IntegerEditTextPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public IntegerEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected String getPersistedString(String defaultReturnValue) {
        int defaultValue = -1;
        try {
            defaultValue = Integer.valueOf(defaultReturnValue);
        } catch(NumberFormatException e) {

        }

        return String.valueOf(getPersistedInt(defaultValue));
    }

    @Override
    protected boolean persistString(String value) {
        return persistInt(Integer.valueOf(value));
    }
}

但是,我不能在 XML 中使用。当我使用时;

   <com.example.A.IntegerEditTextPreference
            android:defaultValue="80"
            android:inputType="numberPassword"
            android:key="Speed"
            android:persistent="false"
            android:numeric="integer"
            android:maxLength="3"
            android:title="Choose Speed" />

我收到此错误:“此处不允许使用元素”。现在,我正在搜索,他们说我必须在可绘制对象中使用 xml 文件夹,但它是首选项 XML,所以我不能这样做。我能做什么?

编辑:我添加我的 xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <PreferenceCategory android:title="Notification">
        <SwitchPreference
            android:defaultValue="false"
            android:icon="@drawable/B"
            android:key="B"
            android:title="B" />
        <SwitchPreference
            android:defaultValue="false"
            android:icon="@drawable/A"
            android:key="A"
            android:title="A" />

       <com.example.A.IntegerEditTextPreference
            android:defaultValue="80"
            android:inputType="numberPassword"
            android:key="Speed"
            android:persistent="false"
            android:numeric="integer"
            android:maxLength="3"
            android:title="Choose Speed" />
    </PreferenceCategory>
</PreferenceScreen>

最佳答案

您可以限制 EditTextPreference。但是,当你得到这个值时,转换Integer或其他值。

<EditTextPreference
    android:key="Speed"
    android:selectAllOnFocus="true"
    android:singleLine="true"
    android:summary="A"
    android:title="A" />

关于java - 首选项 XML 中不允许使用该元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56935317/

相关文章:

java - Spring Data Jpa 不支持 Groupby

android - 如何改变音调?

android - 偏好类别应该有键吗?

Android:如何从首选项标题启动 Activity ?

java - 在运行时设置类路径

java - SLF4J MDC 的替代方案

java - 我怎样才能使我的开发工作流程更 "enterprisey"?

Android,将 BitmapPool 与 Glide 库一起使用

android - MD在移动开发中意味着什么?

SwitchPreferenceCompat 自定义布局的 Android id