有时ListPreference的条目是emptyArray,我希望系统显示提示信息,而不是弹出一个空列表对话框,当我点击区域 android:summary =“选择要恢复的备份项目”。
我该怎么办?谢谢!
<ListPreference
android:key="RestoreItem"
android:title="Restore Item"
android:summary="Choose backup item to restore"
/>
fun setPreference(){
var aListPreference=preferenceManager.findPreference("RestoreItem") as ListPreference
val entries = arrayOf<CharSequence>("English", "French")
val entryValues = arrayOf<CharSequence>("1", "2")
val aa = emptyArray<CharSequence>()
var bb= emptyArray<CharSequence>()
aListPreference.entries=cc
aListPreference.entryValues=dd
}
BTW,以下代码不起作用,系统框仍然显示空列表框
aListPreference.onPreferenceClickListener = OnPreferenceClickListener {
var result=aListPreference.entries.size>=1
if (result==false){
toast("There is no backup item")
}
result
}
答案 0 :(得分:1)
您需要使用不同的内容逻辑自定义您自己的DialogPreference
(我用Java写这个)
班级YourCustomDialogPreference
:
public AboutDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
// Define buttons
builder.setPositiveButton(null, null);
builder.setNegativeButton(null, null);
super.onPrepareDialogBuilder(builder);
}
@Override
protected void onBindDialogView(View view) {
// Your view binding and content logic goes here
mAboutList = view.findViewById(R.id.privacy_list);
mAboutList.setAdapter(new ArrayAdapter<>(getContext(), R.layout.layout_privacy_item, mPrivacyItems));
mAboutList.setOnItemClickListener(this);
// bla bla ...
super.onBindDialogView(view);
}
在preference.xml
中定义:
<your.package.name.YourCustomDialogPreference
android:key="pref_about"
android:dialogLayout="@layout/dialog_about"
android:title="@string/pref_about" />
自定义布局dialog_about.xml
:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/privacy_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@drawable/divider"
android:dividerHeight="1px">
</ListView>
</FrameLayout>