【问题标题】:Add summary to ListPreference List?将摘要添加到 ListPreference 列表?
【发布时间】:2020-11-17 22:03:33
【问题描述】:

我正在尝试将摘要添加到我的列表项之一。 (“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>

【问题讨论】:

  • 我认为没有内置设置或首选项。您需要创建自己的 ListPreference 或自定义 PreferenceDialog

标签: java android list androidx listpreference


【解决方案1】:

最好的机会是使用带有自定义适配器的自定义项的对话框。一个简单的方法是使用微调器,将 spinnermode 设置为 dalog 并使用自定义项填充它。

在您的活动布局中添加此微调器

 <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;

}
@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;
}
}

所以所有的东西都设置好了,选择时只需将项目的 id 或索引保存到共享首选项中。 把它放在你的活动的 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】:

    您可以直接在 XML 中使用 androidx.preference.ListPreferenceapp: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" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多