英文:
Add summary to ListPreference List?
问题
我正在尝试为我的列表项之一添加摘要。
(在“Systemstandardeinstellung”下的摘要)
我应该如何做?
我还没有找到任何解决方案,我唯一找到的是如何将当前选定的ListItem用作ListPreference摘要。
以下是我的代码:
preferences.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:key="basic_settings_category"
android:title="Grundeinstellungen"
app:iconSpaceReserved="false">
<ListPreference
android:defaultValue="0"
android:key="@string/theme_preferences_key"
android:title="Designs"
app:entries="@array/themes_entries"
app:entryValues="@array/themes_values"
app:iconSpaceReserved="false" />
<SwitchPreference
android:key="night_mode"
android:title="Nachtmodus"
app:iconSpaceReserved="false" />
</PreferenceCategory>
</PreferenceScreen>
theme_res.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="themes_entries">
<item>Systemstandardeinstellung</item>
<item>Hell</item>
<item>Dunkel</item>
</string-array>
<string-array name="themes_values">
<item>@string/system_theme_preference_value</item>
<item>@string/light_theme_preference_value</item>
<item>@string/dark_theme_preference_value</item>
</string-array>
</resources>
strings.xml
<resources>
<string name="app_name">TestApp</string>
<string name="settings">Settings</string>
<string name="openNavDrawer">Navigation Drawer Open</string>
<string name="closeNavDrawer">Navigation Drawer Close</string>
<string name="theme_preferences_key">theme_preferences_key</string>
<string name="notification_preferences_key"></string>
<string name="system_theme_preference_value">0</string>
<string name="light_theme_preference_value">1</string>
<string name="dark_theme_preference_value">2</string>
<string name="system_theme_description">Systemstandardeinstellung</string>
<string name="light_theme_description">Hell</string>
<string name="dark_theme_description">Dunkel</string>
</resources>
英文:
I am trying to add a summary to one of my list items.
(Summary under 'Systemstandardeinstellung')
How can I do it?
I haven't found any solution yet, the only thing I found was how to use to currently selected ListItem as the ListPreference summary.
This is what I have.:
preferences.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:key="basic_settings_category"
android:title="Grundeinstellungen"
app:iconSpaceReserved="false">
<ListPreference
android:defaultValue="0"
android:key="@string/theme_preferences_key"
android:title="Designs"
app:entries="@array/themes_entries"
app:entryValues="@array/themes_values"
app:iconSpaceReserved="false" />
<SwitchPreference
android:key="night_mode"
android:title="Nachtmodus"
app:iconSpaceReserved="false" />
</PreferenceCategory>
</PreferenceScreen>
theme_res.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="themes_entries">
<item>Systemstandardeinstellung</item>
<item>Hell</item>
<item>Dunkel</item>
</string-array>
<string-array name="themes_values">
<item>@string/system_theme_preference_value</item>
<item>@string/light_theme_preference_value</item>
<item>@string/dark_theme_preference_value</item>
</string-array>
</resources>
strings.xml
<resources>
<string name="app_name">TestApp</string>
<string name="settings">Settings</string>
<string name="openNavDrawer">Navigation Drawer Open</string>
<string name="closeNavDrawer">Navigation Drawer Close</string>
<string name="theme_preferences_key">theme_preferences_key</string>
<string name="notification_preferences_key"></string>
<string name="system_theme_preference_value">0</string>
<string name="light_theme_preference_value">1</string>
<string name="dark_theme_preference_value">2</string>
<string name="system_theme_description">Systemstandardeinstellung</string>
<string name="light_theme_description">Hell</string>
<string name="dark_theme_description">Dunkel</string>
</resources>
答案1
得分: 0
最好的方法是使用带有自定义项和自定义适配器的对话框。实现这个的简单方法是使用一个 Spinner,并将 spinnermode 设置为 dialog,然后用自定义项填充它。
在你的活动布局中添加这个 Spinner:
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
android:id="@+id/test_spn"/>
创建你的自定义项类:
public class custom_item {
public String name, description;
public int id;
public custom_item(String name, String description) {
this.name = name;
this.description = description;
}
public custom_item(int id, String name, String description) {
this.name = name;
this.description = description;
this.id = id;
}
}
在你的布局文件夹中,创建自定义项布局文件(custom_item_layout):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="5dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:textStyle="bold"
android:id="@+id/title"
android:layout_height="wrap_content"
android:text="Title"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/description"
android:textSize="10dp"
android:text="description"
android:layout_marginLeft="10dp"/>
</LinearLayout>
接下来,创建适配器类以进行适配(custom_adapter):
public class custom_adapter extends BaseAdapter {
ArrayList<custom_item> pref_items;
Context contxt;
public custom_adapter(Context contxt, ArrayList<custom_item> pref_items) {
this.contxt = contxt;
this.pref_items = pref_items;
}
// ...(省略 getCount、getItem、getItemId 方法)
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = null;
custom_item item = pref_items.get(position);
convertView = LayoutInflater.from(contxt).inflate(R.layout.custom_item_layout, null, false);
TextView title_ = (TextView) convertView.findViewById(R.id.title);
TextView description = (TextView) convertView.findViewById(R.id.description);
title_.setText(item.name);
description.setText(item.description);
return convertView;
}
}
最后,在你的活动的 onCreate 方法中完成设置:
Spinner my_spn = ((Spinner) findViewById(R.id.test_spn));
final String sp_name = "SharedPrefS_name";
SharedPreferences prefs = getSharedPreferences(sp_name, MODE_PRIVATE);
ArrayList<custom_item> items = new ArrayList<custom_item>();
items.add(new custom_item("Systemstandardeinstellung", "第一个标题的描述 (Systemstandardeinstellung)"));
items.add(new custom_item("Hell", "第二个标题的描述 (Hell)"));
items.add(new custom_item("Dunkell", "第三个标题的描述 (Dunkell)"));
my_spn.setAdapter(new custom_adapter(this, items));
my_spn.setSelection(prefs.getInt("my_item_identifier", 0));
my_spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
SharedPreferences.Editor saver = act.getSharedPreferences(sp_name, MODE_PRIVATE).edit();
saver.putInt("my_item_identifier", position);
saver.commit();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
英文:
Best chance is to use a dialog with custom items with a custom adapter.An easy way to do this is by using a spinner with spinnermode set to dalog and populate it with custom items.
in your activity layout add this spinner
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
android:id="@+id/test_spn"/>
Have your custom items class
public class custom_item {
public String name,description;
public int id;
public custom_item(String name, String description)
{
this.name=name;
this.description=description;
}
public custom_item(int id, String name, String description)
{
this.name=name;
this.description=description;
this.id=id;
}
}
In Your layout folder, have your custom item layout file (custom_item_layout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:orientation="vertical"
android:padding="5dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:textStyle="bold"
android:id="@+id/title"
android:layout_height="wrap_content"
android:text="Title"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/description"
android:textSize="10dp"
android:text="description"
android:layout_marginLeft="10dp"/>
</LinearLayout>
Then your adapter class to do the adaptation (custom_adapter)
public class custom_adapter extends BaseAdapter {
ArrayList<custom_item> pref_items;
Context contxt;
public custom_adapter(Context contxt, ArrayList<custom_item> pref_items)
{
this.contxt=contxt;
this.pref_items=pref_items;
}
@Override
public int getCount() {
return pref_items.size();
}
@Override
public Object getItem(int position) {
return pref_items.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView=null;
custom_item item= pref_items.get(position);
convertView= LayoutInflater.from(contxt).inflate(R.layout.custom_item_layout,null,false);
TextView title_=(TextView)convertView.findViewById(R.id.title);
TextView description=(TextView)convertView.findViewById(R.id.description);
title_.setText(item.name);
description.setText(item.description);
return convertView;
}
}
so with all things set ,just save either the id or the index of the item into a shared preference when selected.
Put this in your activity's oncreate
Spinner my_spn=((Spinner)findViewById(R.id.test_spn));
final String sp_name="SharedPrefS_name";
SharedPreferences prefs = getSharedPreferences(sp_name, MODE_PRIVATE);
ArrayList<custom_item> items=new ArrayList<custom_item>();
items.add(new custom_item("Systemstandardeinstellung","Description for the first title (Systemstandardeinstellung)" ));
items.add(new custom_item("Hell","Description for the second title (Hell)" ));
items.add(new custom_item("Dunkell","Description for the third title (Dunkell)" ));
my_spn.setAdapter(new custom_adapter(this,items));
my_spn.setSelection(prefs.getInt("my_item_identifier", 0));
my_spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
SharedPreferences.Editor saver = act.getSharedPreferences(sp_name, MODE_PRIVATE).edit();
saver.putInt("my_item_identifier", position);
saver.commit();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
答案2
得分: 0
您可以直接在XML中使用 androidx.preference.ListPreference
和 app:useSimpleSummaryProvider
:
<ListPreference
android:key="@string/setting_example_key"
android:title="@string/setting_example_title"
android:entries="@array/setting_example_labels"
android:entryValues="@array/setting_example_values"
app:useSimpleSummaryProvider="true"
android:defaultValue="@string/setting_example_default" />
英文:
You can use androidx.preference.ListPreference
and app:useSimpleSummaryProvider
directly in XML:
<ListPreference
android:key="@string/setting_example_key"
android:title="@string/setting_example_title"
android:entries="@array/setting_example_labels"
android:entryValues="@array/setting_example_values"
app:useSimpleSummaryProvider="true"
android:defaultValue="@string/setting_example_default" />
专注分享java语言的经验与见解,让所有开发者获益!
评论