如何在我的tablayout中将preferences.xml链接到我的settingsTabFragment?

时间:2018-05-14 17:58:47

标签: java android

我需要将设置添加到我的最终标签中并使数据可编辑,但是我无法这样做而且不知道使用哪种方法。

我关注的教程: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);
    }
}

1 个答案:

答案 0 :(得分:0)

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

同时,试试这个......

在你的

  

preference.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);
   }
}

}