android SwitchPreference ListPreference使用

有这种的不多说 ,直接 SwitchPreference

package  xxxx.com.pad.ui.activity.progressbar;

import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.RingtonePreference;
import android.preference.SwitchPreference;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sdsmdg.tastytoast.TastyToast;

import xxxxx.com.pad.R;

import static android.os.ParcelFileDescriptor.MODE_WORLD_READABLE;

public class PrefereFragment extends PreferenceFragment {

    private SwitchPreference mMistakeTouchPreference;
    private static final String MISTAKE_TOUCH_MODE_KEY = "nice";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_general);

//        bindPreferenceSummaryToValue(findPreference("example_switch"));
        bindPreferenceSummaryToValue(findPreference("example_text"));
        bindPreferenceSummaryToValue(findPreference("example_list"));
//        bindPreferenceSummaryToValue(findPreference("nice"));
        initMistakeTouchPreference();
    }

    private void initMistakeTouchPreference() {
        mMistakeTouchPreference = (SwitchPreference)findPreference(MISTAKE_TOUCH_MODE_KEY);

        mMistakeTouchPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object objValue) {
                final String key = preference.getKey();
                System.out.print("key--"+key);
                if (MISTAKE_TOUCH_MODE_KEY.equals(key)){
                    if (mMistakeTouchPreference.isChecked() != (Boolean)objValue) {
                        boolean value = (Boolean)(objValue);
                        mMistakeTouchPreference.setChecked(value);
                        //最自己的设置
                    }
                }
                return true;
            }
        });
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    private static void bindPreferenceSummaryToValue(Preference preference) {
        preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
        sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
                PreferenceManager.getDefaultSharedPreferences(preference.getContext()).getString(preference.getKey(), ""));
    }

    private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(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.
                ListPreference listPreference = (ListPreference) preference;
                int index = listPreference.findIndexOfValue(stringValue);

                // Set the summary to reflect the new value.
                preference.setSummary(
                        index >= 0
                                ? listPreference.getEntries()[index]
                                : null);

//                TastyToast.makeText(get,stringValue+"", TastyToast.LENGTH_SHORT, 2);

            } else if (preference instanceof RingtonePreference) {
                // For ringtone preferences, look up the correct display value
                // using RingtoneManager.
                if (TextUtils.isEmpty(stringValue)) {
                    // Empty values correspond to 'silent' (no ringtone).
                    preference.setSummary(R.string.pref_ringtone_silent);

                } else {
                    Ringtone ringtone = RingtoneManager.getRingtone(
                            preference.getContext(), Uri.parse(stringValue));

                    if (ringtone == null) {
                        // Clear the summary if there was a lookup error.
                        preference.setSummary(null);
                    } else {
                        // Set the summary to reflect the new ringtone display
                        // name.
                        String name = ringtone.getTitle(preference.getContext());
                        preference.setSummary(name);
                    }
                }

            } else {
                // For all other preferences, set the summary to the value's
                // simple string representation.
                preference.setSummary(stringValue);
            }
            return true;
        }
    };
}

bindPreferenceSummaryToValue 这个方法支持 自动生成的代码里面说的
只是适用这几个
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.

SwitchPreference 要用的话,单独写Listener 下

pref_general.xml



    

    
    
    

    
    
    



然后添加下这个 PrefereFragment

public class ActivityPreferenceFragmentActivity extends AppCompatActivity {

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

        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.frame_container, new PrefereFragment()).commit();
    }
}

activity_preference_fragment.xml





    




activity 等地方获取具体参数

  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String nide = prefs.getString("example_list","");
        System.out.println(nide);

你可能感兴趣的:(android)