Java 在首选项XML中不允许使用元素?
我在安卓项目中工作,关于偏好。我想在EditTextPreference中取整数值。我搜索这个主题,我可以使用这个java类Java 在首选项XML中不允许使用元素?,java,android,android-preferences,Java,Android,Android Preferences,我在安卓项目中工作,关于偏好。我想在EditTextPreference中取整数值。我搜索这个主题,我可以使用这个java类 import androidx.preference.EditTextPreference; public class IntegerEditTextPreference extends EditTextPreference { public IntegerEditTextPreference(Context context) { super(
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" />
我接受这个错误:“这里不允许元素”。现在,我正在搜索,他们说我必须在drawable中使用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。但是,当您得到这个值时,需要转换整数或其他值
<EditTextPreference
android:key="Speed"
android:selectAllOnFocus="true"
android:singleLine="true"
android:summary="A"
android:title="A" />
您可以约束EditTextPreference。但是,当您得到这个值时,需要转换整数或其他值
<EditTextPreference
android:key="Speed"
android:selectAllOnFocus="true"
android:singleLine="true"
android:summary="A"
android:title="A" />
@whiterabbitj这是我的包名。@whiterabbitj是的,只是EdittexPrefences?你能发布整个XML吗?@whiterabbitj发布了。你检查了IntegerEditTextPreference的java包了吗?在电视上播放screenshot@whiterabbitj这是我的包名。@whiterabbitj是的,只是EdittexPrefences?你能发布整个XML吗?@whiterabbitj发布了。你检查了IntegerEditTextPreference
的java包了吗?在屏幕截图上显示它