给ListPreference列表添加摘要?

huangapple 未分类评论65阅读模式
英文:

Add summary to ListPreference List?

问题

我正在尝试为我的列表项之一添加摘要。
(在“Systemstandardeinstellung”下的摘要)

给ListPreference列表添加摘要?

我应该如何做?
我还没有找到任何解决方案,我唯一找到的是如何将当前选定的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')

给ListPreference列表添加摘要?

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;PreferenceScreen xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;

    &lt;PreferenceCategory
        android:key=&quot;basic_settings_category&quot;
        android:title=&quot;Grundeinstellungen&quot;
        app:iconSpaceReserved=&quot;false&quot;&gt;

        &lt;ListPreference
            android:defaultValue=&quot;0&quot;
            android:key=&quot;@string/theme_preferences_key&quot;
            android:title=&quot;Designs&quot;
            app:entries=&quot;@array/themes_entries&quot;
            app:entryValues=&quot;@array/themes_values&quot;
            app:iconSpaceReserved=&quot;false&quot; /&gt;

        &lt;SwitchPreference
            android:key=&quot;night_mode&quot;
            android:title=&quot;Nachtmodus&quot;
            app:iconSpaceReserved=&quot;false&quot; /&gt;

    &lt;/PreferenceCategory&gt;


&lt;/PreferenceScreen&gt;

theme_res.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;resources&gt;
    &lt;string-array name=&quot;themes_entries&quot;&gt;
        &lt;item&gt;Systemstandardeinstellung&lt;/item&gt;
        &lt;item&gt;Hell&lt;/item&gt;
        &lt;item&gt;Dunkel&lt;/item&gt;
    &lt;/string-array&gt;
    &lt;string-array name=&quot;themes_values&quot;&gt;
        &lt;item&gt;@string/system_theme_preference_value&lt;/item&gt;
        &lt;item&gt;@string/light_theme_preference_value&lt;/item&gt;
        &lt;item&gt;@string/dark_theme_preference_value&lt;/item&gt;
    &lt;/string-array&gt;
&lt;/resources&gt;

strings.xml

&lt;resources&gt;
    &lt;string name=&quot;app_name&quot;&gt;TestApp&lt;/string&gt;
    &lt;string name=&quot;settings&quot;&gt;Settings&lt;/string&gt;

    &lt;string name=&quot;openNavDrawer&quot;&gt;Navigation Drawer Open&lt;/string&gt;
    &lt;string name=&quot;closeNavDrawer&quot;&gt;Navigation Drawer Close&lt;/string&gt;

    &lt;string name=&quot;theme_preferences_key&quot;&gt;theme_preferences_key&lt;/string&gt;
    &lt;string name=&quot;notification_preferences_key&quot;&gt;&lt;/string&gt;

    &lt;string name=&quot;system_theme_preference_value&quot;&gt;0&lt;/string&gt;
    &lt;string name=&quot;light_theme_preference_value&quot;&gt;1&lt;/string&gt;
    &lt;string name=&quot;dark_theme_preference_value&quot;&gt;2&lt;/string&gt;

    &lt;string name=&quot;system_theme_description&quot;&gt;Systemstandardeinstellung&lt;/string&gt;
    &lt;string name=&quot;light_theme_description&quot;&gt;Hell&lt;/string&gt;
    &lt;string name=&quot;dark_theme_description&quot;&gt;Dunkel&lt;/string&gt;

&lt;/resources&gt;

答案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

 &lt;Spinner
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:spinnerMode=&quot;dialog&quot;
                    android:id=&quot;@+id/test_spn&quot;/&gt;

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)

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
 &lt;LinearLayout
   xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;   android:layout_width=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;
android:padding=&quot;5dp&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
&lt;TextView
android:layout_width=&quot;wrap_content&quot;
android:textStyle=&quot;bold&quot;
android:id=&quot;@+id/title&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;Title&quot;/&gt;
&lt;TextView
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
    android:id=&quot;@+id/description&quot;
    android:textSize=&quot;10dp&quot;
android:text=&quot;description&quot;
    android:layout_marginLeft=&quot;10dp&quot;/&gt;
&lt;/LinearLayout&gt;

Then your adapter class to do the adaptation (custom_adapter)

public class custom_adapter extends BaseAdapter {
ArrayList&lt;custom_item&gt; pref_items;
Context contxt;
public custom_adapter(Context contxt, ArrayList&lt;custom_item&gt; 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=&quot;SharedPrefS_name&quot;;
    SharedPreferences prefs = getSharedPreferences(sp_name, MODE_PRIVATE);
    ArrayList&lt;custom_item&gt; items=new ArrayList&lt;custom_item&gt;();
    items.add(new custom_item(&quot;Systemstandardeinstellung&quot;,&quot;Description for the first title (Systemstandardeinstellung)&quot; ));
    items.add(new custom_item(&quot;Hell&quot;,&quot;Description for the second  title (Hell)&quot; ));
    items.add(new custom_item(&quot;Dunkell&quot;,&quot;Description for the third  title (Dunkell)&quot; ));
    my_spn.setAdapter(new custom_adapter(this,items));
    my_spn.setSelection(prefs.getInt(&quot;my_item_identifier&quot;, 0));
    my_spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
            SharedPreferences.Editor saver = act.getSharedPreferences(sp_name, MODE_PRIVATE).edit();

            saver.putInt(&quot;my_item_identifier&quot;, position);
            saver.commit();
        }

        @Override
        public void onNothingSelected(AdapterView&lt;?&gt; parent) {

        }
    });

答案2

得分: 0

您可以直接在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" />
英文:

You can use androidx.preference.ListPreference and app:useSimpleSummaryProvider directly in XML:

&lt;ListPreference
    android:key=&quot;@string/setting_example_key&quot;
    android:title=&quot;@string/setting_example_title&quot;
    android:entries=&quot;@array/setting_example_labels&quot;
    android:entryValues=&quot;@array/setting_example_values&quot;
    app:useSimpleSummaryProvider=&quot;true&quot;
    android:defaultValue=&quot;@string/setting_example_default&quot; /&gt;

huangapple
  • 本文由 发表于 2020年7月28日 20:50:42
  • 转载请务必保留本文链接:https://java.coder-hub.com/63134550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定