簡體   English   中英

PreferenceScreen在更新應用程序后不顯示摘要

[英]PreferenceScreen not displaying summary after updating app

所以,我有這個xml用於PreferenceScreen

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <EditTextPreference
        android:dialogMessage="@string/w_common_display_name_desc"
        android:key="display_name"
        android:summary="@string/w_common_display_name_desc"
        android:title="@string/w_common_display_name" />
    <EditTextPreference
        android:dialogMessage="@string/w_basic_username_desc"
        android:inputType="textVisiblePassword"
        android:key="username"
        android:summary="@string/w_basic_username_desc"
        android:title="@string/w_basic_username" />
    <EditTextPreference
        android:dialogMessage="@string/w_common_server_desc"
        android:inputType="textVisiblePassword"
        android:key="server"
        android:summary="@string/w_common_server_desc"
        android:title="@string/w_common_server" />

    <com.csipsimple.widgets.PasswordPreference
        android:dialogMessage="@string/w_basic_password_desc"
        android:key="password"
        android:password="true"
        android:summary="@string/w_basic_password_desc"
        android:title="@string/w_basic_password" />

</PreferenceScreen>

這由此PreferenceActivity調用。

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen;
import android.text.TextUtils;

import com.actionbarsherlock.view.Menu;
import com.csipsimple.R;
import com.csipsimple.utils.Log;

public abstract class GenericPrefs extends PreferenceActivity implements
        OnSharedPreferenceChangeListener, IPreferenceHelper {

    private static final String THIS_FILE = "GenericPrefs";
    private static String TAG = "ricky";

    public abstract boolean onCreateOptionsMenu(Menu menu);

    /**
     * Get the xml preference resource for this screen
     * 
     * @return the resource reference
     */
    protected abstract int getXmlPreferences();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        beforeBuildPrefs();
        addPreferencesFromResource(getXmlPreferences());
        afterBuildPrefs();
        Log.d(TAG, "GenericPrefs");
    }

    @Override
    public void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
        updateDescriptions();
    }

    @Override
    public void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        updateDescriptions();
    }

    /**
     * Process update of description of each preference field
     */
    protected abstract void updateDescriptions();

    /**
     * Optional hook for doing stuff before preference xml is loaded
     */
    protected void beforeBuildPrefs() {
        // By default, nothing to do
    }

    /**
     * Optional hook for doing stuff just after preference xml is loaded
     */
    protected void afterBuildPrefs() {
        // By default, nothing to do
    }

    // Utilities for update Descriptions
    /**
     * Get field summary if nothing set. By default it will try to add _summary
     * to name of the current field
     * 
     * @param field_name Name of the current field
     * @return Translated summary for this field
     */
    protected String getDefaultFieldSummary(String field_name) {
        try {
            String keyid = R.string.class.getField(field_name + "_summary").get(null).toString();
            return getString(Integer.parseInt(keyid));
        } catch (SecurityException e) {
            // Nothing to do : desc is null
        } catch (NoSuchFieldException e) {
            // Nothing to do : desc is null
        } catch (IllegalArgumentException e) {
            // Nothing to do : desc is null
        } catch (IllegalAccessException e) {
            // Nothing to do : desc is null
        }

        return "";
    }

    /**
     * Set summary of a standard string field If empty will display the default
     * summary Else it displays the preference value
     * 
     * @param fieldName the preference key name
     */
    public void setStringFieldSummary(String fieldName) {
        PreferenceScreen pfs = getPreferenceScreen();
        SharedPreferences sp = pfs.getSharedPreferences();
        Preference pref = pfs.findPreference(fieldName);

        String val = sp.getString(fieldName, null);
        if (TextUtils.isEmpty(val)) {
            val = getDefaultFieldSummary(fieldName);
            Log.d(TAG, "genericPrefs field summary: "+val);
        }
        setPreferenceSummary(pref, val);
    }

    /**
     * Set summary of a password field If empty will display default summary If
     * password will display a * char for each letter of password
     * 
     * @param fieldName the preference key name
     */
    public void setPasswordFieldSummary(String fieldName) {
        PreferenceScreen pfs = getPreferenceScreen();
        SharedPreferences sp = pfs.getSharedPreferences();
        Preference pref = pfs.findPreference(fieldName);

        String val = sp.getString(fieldName, null);

        if (TextUtils.isEmpty(val)) {
            val = getDefaultFieldSummary(fieldName);
        } else {
            val = val.replaceAll(".", "*");
        }
        setPreferenceSummary(pref, val);
    }

    /**
     * Set summary of a list field If empty will display default summary If one
     * item selected will display item name
     * 
     * @param fieldName the preference key name
     */
    public void setListFieldSummary(String fieldName) {
        PreferenceScreen pfs = getPreferenceScreen();
        ListPreference pref = (ListPreference) pfs.findPreference(fieldName);
        if (pref == null) {
            Log.w(THIS_FILE, "Unable to find preference " + fieldName);
            return;
        }

        CharSequence val = pref.getEntry();
        if (TextUtils.isEmpty(val)) {
            val = getDefaultFieldSummary(fieldName);
        }
        setPreferenceSummary(pref, val);
    }

    /**
     * Safe setSummary on a Preference object that make sure that the preference
     * exists before doing anything
     * 
     * @param pref the preference to change summary of
     * @param val the string to set as preference summary
     */
    protected void setPreferenceSummary(Preference pref, CharSequence val) {
        if (pref != null) {
            pref.setSummary(val);
        }
    }

    /**
     * Hide a preference from the screen so that user can't see and modify it
     * 
     * @param parent the parent group preference if any, leave null if
     *            preference is a root pref
     * @param fieldName the preference key name to hide
     */
    public void hidePreference(String parent, String fieldName) {
        PreferenceScreen pfs = getPreferenceScreen();
        PreferenceGroup parentPref = pfs;
        if (parent != null) {
            parentPref = (PreferenceGroup) pfs.findPreference(parent);
        }

        Preference toRemovePref = pfs.findPreference(fieldName);

        if (toRemovePref != null && parentPref != null) {
            parentPref.removePreference(toRemovePref);
        } else {
            Log.w("Generic prefs", "Not able to find" + parent + " " + fieldName);
        }
    }


    @Override
    public void setPreferenceScreenType(String key, int type) {
        setPreferenceScreenType(getClass(), key, type);
    }

    @Override
    public void setPreferenceScreenSub(String key, Class<?> activityClass, Class<?> fragmentClass, int type) {
        setPreferenceScreenType(activityClass, key, type);
    }

    private void setPreferenceScreenType(Class<?> classObj, String key, int type) {
        Preference pf = findPreference(key);
        Intent it = new Intent(this, classObj);
        it.putExtra(PrefsLogic.EXTRA_PREFERENCE_TYPE, type);
        pf.setIntent(it);
    }

    /* (non-Javadoc)
     * @see android.preference.PreferenceActivity#isValidFragment(java.lang.String)
     */
    public boolean isValidFragment(String fragmentName) {
        // This pref activity does not include any fragment
        return false;
    }
}

我看了很多關於PreferenceScreen教程,xml看起來不錯。 這是以前使用SherlockFragment的項目的一部分,我目前正在升級其代碼的某些部分。 當我編譯應用程序時,由於某種原因summary不會顯示。 dialogMessage顯示得很好。

我已經在AndroidManifest上添加了它。

implementation 'com.android.support:preference-v7:28.0.0'

我今天發現,即使在布局編輯器中,代碼使用的ListView也不顯示任何內容。 我嘗試用RecyclerView替換它,但引發錯誤, Caused by: java.lang.ClassCastException: android.support.v7.widget.RecyclerView cannot be cast to android.widget.ListView 我尋找了諸如findViewById類的東西,但找不到任何東西。 我寧願處理不顯示任何錯誤的問題,因為我覺得它比前一個更加復雜。

這是用於顯示PreferenceScreen的xml。

<?xml version="1.0" encoding="utf-8"?>
<!--
    Copyright (C) 2010 Regis Montoya (aka r3gis - www.r3gis.fr) 

    This file is part of CSipSimple.
    CSipSimple is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    If you own a pjsip commercial license you can also redistribute it
    and/or modify it under the terms of the GNU Lesser General Public License
    as an android library.

    CSipSimple is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with CSipSimple.  If not, see <http://www.gnu.org/licenses/>.
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/settings_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/validation_bar"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/custom_wizard_row"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/listPreferredItemHeight"
            android:orientation="vertical"
            android:visibility="gone">

            <TextView
                android:id="@+id/custom_wizard_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:paddingLeft="12dp"
                android:paddingRight="12dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@android:color/white" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@android:drawable/divider_horizontal_dark"
                android:contentDescription="@string/empty_description" />
        </LinearLayout>

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawSelectorOnTop="false"
            tools:listitem="@layout/adptr_simple"
            android:layoutAnimation="@anim/layout_slide_right"
            android:persistentDrawingCache="animation|scrolling" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/validation_bar"
        style="@style/ButtonBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <Button
            android:id="@+id/cancel_bt"
            style="@style/ButtonBarButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="@string/cancel" />

        <Button
            android:id="@+id/save_bt"
            style="@style/ButtonBarButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="@string/save" />
    </LinearLayout>

</RelativeLayout>

我嘗試更改Theme但仍然不顯示文本。 我嘗試刪除android:layoutAnimationandroid:persistentDrawingCache但仍未顯示summary

這是adptr_simple.xml因為有人要它。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:text="TextView"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="16dp"
        android:text="TextView"
        android:textColor="#FFFFFF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/textView"
        app:layout_constraintHorizontal_bias="0.507"
        app:layout_constraintStart_toStartOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
</android.support.constraint.ConstraintLayout>

猜猜這些行是錯誤的:

String keyid = R.string.class.getField(field_name + "_summary").get(null).toString();
return getString(Integer.parseInt(keyid));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM