Android中Fragmen首选项使用自定义的ListPreference的方法

ListPreference简介

ListPreference是Android中的一种对话框样式,它允许用户从一个预定义的列表中选择一个选项,ListPreference通常用于需要用户从有限的选项中进行选择的情况,例如设置语言、屏幕方向等,与Spinner相比,ListPreference提供了更加简洁的界面,同时支持搜索功能,方便用户快速找到所需的选项。

自定义ListPreference的方法

要使用自定义的ListPreference,我们需要继承ListPreference类,并重写其onBindView方法,在onBindView方法中,我们可以自定义ListPreference的布局和显示内容,以下是一个简单的示例:

Android中Fragmen首选项使用自定义的ListPreference的方法

1、创建一个自定义的ListPreference类,继承自AppCompatActivity中的ListPreference:

import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.ListPreference;
public class CustomListPreference extends ListPreference {
    private Context context;
    private String[] entries;
    private String entryValue;
    private OnEntrySetListener onEntrySetListener;
    public CustomListPreference(Context context, String title, String[] entries) {
        super(context);
        this.context = context;
        this.entries = entries;
    }
    public void setEntryValue(String entryValue) {
        this.entryValue = entryValue;
    }
    public void setOnEntrySetListener(OnEntrySetListener onEntrySetListener) {
        this.onEntrySetListener = onEntrySetListener;
    }
    @Override
    protected void onBindView(View view, final CharSequence defaultValue) {
        // 自定义布局文件,这里假设为custom_list_preference_layout.xml
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View customView = inflater.inflate(R.layout.custom_list_preference_layout, null);
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item, entries);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        TextView textView = customView.findViewById(R.id.textView);
        textView.setText(entryValue);
        ListPreference listPreference = (ListPreference) view;
        listPreference.setAdapter(adapter);
        listPreference.setEntries(entries);
        listPreference.setEntryValues(entries);
        listPreference.setSummary(defaultValue);
        listPreference.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (onEntrySetListener != null) {
                    onEntrySetListener.onEntrySelected(position);
                }
            }
        });
        super.setOnBindViewListener((parent, viewHolder) -> viewHolder.itemView == customView);
    }
}

2、在res/layout目录下创建一个名为custom_list_preference_layout.xml的布局文件,用于自定义ListPreference的布局:

Android中Fragmen首选项使用自定义的ListPreference的方法

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

3、在Activity或Fragment中使用自定义的ListPreference:

CustomListPreference customListPreference = findViewById(R.id.custom_list_preference);
String[] entries = {"Option1", "Option2", "Option3"};
customListPreference.setEntries(entries);
customListPreference.setEntryValues(entries);
customListPreference.setSummary("Default"); // 设置默认选项的描述文本
customListPreference.setOnItemSelectedListener((parent, view, position, id) -> Toast.makeText(this, "选中了:" + entries[position], Toast.LENGTH_SHORT).show()); // 当选项改变时,显示提示信息
customListPreference = findViewById(R.id.custom_list_preference); // 确保在设置监听器后获取到正确的实例对象,避免空指针异常
customListPreference.setOnEntrySetListener((position) -> Toast.makeText(this, "选中了:" + entries[position], Toast

原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/189687.html

Like (0)
Donate 微信扫一扫 微信扫一扫
K-seoK-seo
Previous 2024-01-01 23:13
Next 2024-01-01 23:21

相关推荐

  • vue视频如何添加滚动文字

    Vue视频添加滚动文字,可以使用vue-scroll动画库。

    2024-01-22
    0187
  • html5怎么做时钟

    HTML5是一种用于构建网页的标准标记语言,它提供了丰富的功能和特性,使得开发者可以轻松地创建动态和交互式的网页,在本文中,我们将介绍如何使用HTML5制作一个简单的时钟。1、准备工作我们需要创建一个HTML文件,并在其中添加一个&lt;canvas&gt;元素,用于绘制时钟。&lt;canvas&gt……

    2024-03-22
    0179
  • JMS和企业集成

    JMS(Java Message Service)是一种用于在分布式系统中发送和接收消息的API,它提供了一种简单的方法来实现异步通信,JMS最初是由Sun Microsystems(现已被Oracle收购)开发的,现在是Java EE规范的一部分,本文将介绍JMS的基本概念、使用方法以及与企业集成的相关技术。JMS基本概念1、1 J……

    2023-12-16
    0127
  • html5 怎么在图形中加汉字

    HTML5 怎么在图形中加汉字HTML5 是一种用于创建和呈现网页的标准标记语言,它提供了许多新的元素和属性,使得开发者能够创建更加丰富和动态的网页,如何在图形中添加汉字是一个常见的需求,本文将详细介绍如何使用 HTML5 在图形中添加汉字。使用 HTML5 的 &lt;canvas&gt; 标签HTML5 提供了一个……

    2023-12-20
    0136
  • tomcat启动报错lifecycle如何解决

    Tomcat启动报错lifecycle的原因Tomcat是一个用于部署Java Web应用程序的Web服务器,它提供了一个完整的Servlet容器,包括管理servlet、JSP和其他Java技术,在Tomcat中,一个web应用被抽象为一个叫做“Context”的对象,而每个Context对象都包含一个或多个“Host”对象,在To……

    2024-01-30
    0218
  • java中contextconfiglocation的作用是什么

    在Java中,ContextConfigLocation是一个用于配置Spring应用程序上下文的参数,它的主要作用是指定Spring配置文件的位置,以便Spring框架能够正确地加载和管理应用程序的配置信息,本文将详细介绍ContextConfigLocation的作用、使用方法以及与其他相关概念的关系。1、ContextConfi……

    2024-02-26
    0107

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

免备案 高防CDN 无视CC/DDOS攻击 限时秒杀,10元即可体验  (专业解决各类攻击)>>点击进入