setting菜单界面的形成--未优化

 

代码:

first_preference.xml:




    
        

        
    
    
        

        
        
    

    
    

    
    
        
    

    

    
    
        

        
    

arrays.xml:



    
        成都
        北京
        上海
    
    
       
        028  
        010  
        021  
      

MainActivity.java:

package com.zzw.testpreferenceactivity;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.MultiSelectListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;

public class MainActivity extends PreferenceActivity {

    private final String CHECKBOXPREFERENCE_KEY1 = "checkbox_key1";
    private final String EDITTEXTPREFERENCE_KEY1 = "edit_key1";
    private final String LISTPREFERENCE_KEY = "listPreferenc_key";
    private final String MULTISELECTLISTPREFERENCE_KEY = "multiSelectListPreference_key";
    private final String SWITCHPREFERENCE_KEY = "switchPreference_key1";

    private SharedPreferences mSharedPreferences;
    private OnSharedPreferenceChangeListener mOnSharedPreferenceChangeListener;

    private CheckBoxPreference mCheckBoxPreference;
    private EditTextPreference mEditTextPreference;
    private ListPreference mListPreference;
    private MultiSelectListPreference mMultiSelectListPreference;
    private SwitchPreference mSwitchPreference;

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

        // 添加设置的选项
        addPreferencesFromResource(R.xml.first_preference);

        mCheckBoxPreference = (CheckBoxPreference) findPreference(CHECKBOXPREFERENCE_KEY1);
        mEditTextPreference = (EditTextPreference) findPreference(EDITTEXTPREFERENCE_KEY1);
        mListPreference = (ListPreference) findPreference(LISTPREFERENCE_KEY);
        mMultiSelectListPreference = (MultiSelectListPreference) findPreference(MULTISELECTLISTPREFERENCE_KEY);
        mSwitchPreference = (SwitchPreference) findPreference(SWITCHPREFERENCE_KEY);

        // 获取SharedPreferences对象
        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        // 通用的读取设置的某个值的方法
        // boolean b = mSharedPreferences.getBoolean("checkbox_key1", false);
        // Log.d("checkbox_key1", b+"");
        //
        // String s = mSharedPreferences.getString("edit_key1", "暂时为空");
        // Log.d("edit_key1", s);

        mOnSharedPreferenceChangeListener = new MyOnSharedPreferenceChangeListener();
    }

    // 数据发生变化时候的监听类设置
    private class MyOnSharedPreferenceChangeListener implements OnSharedPreferenceChangeListener {

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

            if (CHECKBOXPREFERENCE_KEY1.equals(key)) {

                boolean b = sharedPreferences.getBoolean(CHECKBOXPREFERENCE_KEY1, false);
                mCheckBoxPreference.setSummary(b + "");
                // Toast.makeText(getApplicationContext(), "checkBox变化了,当前值--" +
                // b, 0).show();

            } else if (EDITTEXTPREFERENCE_KEY1.equals(key)) {

                String s = sharedPreferences.getString(EDITTEXTPREFERENCE_KEY1, "null");
                mEditTextPreference.setSummary(s);
                // Toast.makeText(getApplicationContext(), "editText变化了,当前值--" +
                // s, 0).show();

            } else if (LISTPREFERENCE_KEY.equals(key)) {

                String e = mListPreference.getEntry() + "";
                String v = mListPreference.getValue();
                mListPreference.setSummary(e + " : " + v);

            } else if (MULTISELECTLISTPREFERENCE_KEY.equals(key)) {
                // String s_entries="entries:";
                //
                // CharSequence
                // []entries=mMultiSelectListPreference.getEntries();
                // for(CharSequence c:entries){
                // s_entries+=c+""+"\t";
                // }
                //
                // Set values=mMultiSelectListPreference.getValues();
                // Iterator it=values.iterator();
                // String s_values="values:";
                // while(it.hasNext()){
                // s_values+=it.next().toString()+"\t";
                // }
                // mMultiSelectListPreference.setSummary(s_entries+"\n"+s_values);

            } else if (SWITCHPREFERENCE_KEY.equals(key)) {

                boolean b = sharedPreferences.getBoolean(SWITCHPREFERENCE_KEY, false);
                mSwitchPreference.setSummary(b + "");
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 注册数据发生变化时候的监听
        mSharedPreferences.registerOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
        // getPreferenceScreen().getSharedPreferences()
        // .registerOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 取消数据发生变化时候的监听
        mSharedPreferences.unregisterOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
        // getPreferenceScreen().getSharedPreferences()
        // .unregisterOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
    }

}



你可能感兴趣的:(setting菜单界面的形成--未优化)