首页 > 解决方案 > 如何将preferences.xml 链接到我的tablayout 中的settingsTabFragment?

问题描述

我需要将设置添加到我的最终选项卡并让数据可编辑,但是我在这样做时遇到了麻烦并且不知道要使用哪些方法。

我正在关注的教程:https ://github.com/codepath/android_guides/wiki/Settings-with-PreferenceFragment

SettingsTabFragment.java

package com.example.sachin.pedometer;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.takisoft.fix.support.v7.preference.PreferenceFragmentCompat;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link SettingsTabFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link SettingsTabFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SettingsTabFragment extends PreferenceFragmentCompat {

    private ListPreference mListPreference;

    @Override
    public void onCreatePreferencesFix(@Nullable Bundle savedInstanceState, String rootKey) {

        // Indicate here the XML resource you created above that holds the preferences
        setPreferencesFromResource(R.xml.preferences, rootKey);
    }

    /*@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        mListPreference = (ListPreference)  getPreferenceManager().findPreference("preference_key");
    mListPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                // your code here
                return view;
            }
        }

        return inflater.inflate(R.layout.fragment_settings_tab, container, false);
    }
}

标签: javaandroid

解决方案


我真的不知道你的问题具体是什么!您是否未能实施 ListPreference?尝试运行时应用程序是否崩溃?如果是这样,请提供 logcat。

与此同时,试试这个......

在你的

偏好.xml

添加这个

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/root_title">

<ListPreference
        android:key="list_preference"
        android:title="@string/title_list_preference"
        android:summary="@string/summary_list_preference"
        android:entries="@array/entries_list_preference"
        android:entryValues="@array/entryvalues_list_preference"
        android:dialogTitle="@string/dialog_title_list_preference" />


</PreferenceScreen>

在你的

SettingsTabFragment.java

添加这个..

public static class SettingsTabFragment extends PreferenceFragmentCompat 
implements SharedPreferences.OnSharedPreferenceChangeListener{

private ListPreference mListPreference;

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String 
    rootKey) {
    // Load the preferences from an XML resource
    setPreferencesFromResource(R.xml.preferences, rootKey);
    }

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

    setPreferenceSummery();

    }
private void setPreferenceSummery(Preference preference,Object value){

String stringValue = value.toString();

if (preference instanceof ListPreference){
// For list preferences, look up the correct display value in
// the preference's 'entries' list (since they have separate labels/values).
mListPreference = (ListPreference) preference;

int prefIndex = listPreference.findIndexOfValue(stringValue);
//same code in one line
//int prefIndex = ((ListPreference) preference).findIndexOfValue(value);

//prefIndex must be is equal or garter than zero because
//array count as 0 to ....
if (prefIndex >= 0){
mListPreference.setSummary(mListPreference.getEntries()[prefIndex]);
 }
} else {
// For other preferences, set the summary to the value's simple string 
//representation.
preference.setSummary(stringValue);
   }
}

}

推荐阅读