You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android偏好设置中useSimpleSummaryProvider使用问题:预览有效编译后无摘要

问题:EditTextPreference的SimpleSummaryProvider编译后不显示摘要

我已在应用中实现偏好设置功能且运行正常,现在希望将当前偏好的value显示为其summary。按照相关建议在preferences.xml中使用EditTextPreference.SimpleSummaryProvider,该方案在预览中正常且无报错,但编译后的应用中完全不显示摘要。

预览正常但实际不显示摘要

复现步骤

  • 打开Android Studio,创建空活动项目
  • 配置如下代码文件:

MainActivity.java

package com.test.simplesummary;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.preference.PreferenceFragment;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getFragmentManager().beginTransaction().replace(R.id.idFrameLayout, new SettingsFragment()).commit();
    }

    public static class SettingsFragment extends PreferenceFragment {

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            addPreferencesFromResource(R.xml.root_preferences);

        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/idFrameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

root_preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory>
        <EditTextPreference
            android:key="test_key"
            android:title="test_title"
            app:useSimpleSummaryProvider="true"
            />
    </PreferenceCategory>
</PreferenceScreen>

build.gradle

plugins {
    id 'com.android.application'
}

android {
    namespace 'com.test.simplesummary'
    compileSdk 32

    defaultConfig {
        applicationId "com.test.simplesummary"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.preference:preference:1.1.+'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
}

解决方案

问题根源

你使用的是Android框架原生的android.preference.PreferenceFragment,而app:useSimpleSummaryProvider="true"是AndroidX偏好库专属的属性,旧版原生Fragment无法识别该属性,因此编译后摘要不显示。

修复步骤

将Fragment替换为AndroidX的PreferenceFragmentCompat,并适配相关API:

修改后的MainActivity.java

package com.test.simplesummary;

import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 使用SupportFragmentManager替代原生FragmentManager
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.idFrameLayout, new SettingsFragment())
                .commit();
    }

    // 继承AndroidX的PreferenceFragmentCompat
    public static class SettingsFragment extends PreferenceFragmentCompat {

        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            // 用AndroidX标准方法加载偏好资源
            setPreferencesFromResource(R.xml.root_preferences, rootKey);
        }
    }
}

额外说明

  • 确保build.gradle中的androidx.preference:preference依赖版本不低于1.1.0(你的当前配置满足要求),建议指定具体版本如1.2.0以避免版本兼容问题。
  • 修改后重新编译运行,EditTextPreference的value会自动显示为summary,修改偏好值后摘要也会同步更新。

内容的提问来源于stack exchange,提问作者Aynos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 17:50:45