Preference 实现设置界面


Preference 能简便地实现设置界面,且主要优点是设置的值会自动通过 SharedPreferences 方式保存下来,可直接取用。


一、简易实现


res/xml/prefs.xml:




    
        
        
    

    
        
        
        
    

    
        

            
            
        

        
            
        

        
            
        
    

    
        
        
    

res/values/array.xml:



    
        a
        b
        c
        d
        e
    
    
        1
        2
        3
        4
        5
    

PrefsFragment:

package com.ttt.zhihudaily.fragment;

import android.os.Bundle;
import android.preference.PreferenceFragment;

import com.ttt.zhihudaily.R;

public class PrefsFragment extends PreferenceFragment {

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

        addPreferencesFromResource(R.xml.prefs);
    }
}
  • Activity 中的 addPreferencesFromResource() 已经废弃,现在都推荐用 Fragment。

PrefsActivity:

package com.ttt.zhihudaily.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;

import com.ttt.zhihudaily.R;
import com.ttt.zhihudaily.fragment.PrefsFragment;

public class PrefsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prefs);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

*       getFragmentManager()
*               .beginTransaction()
*               .replace(R.id.prefs_frame,new PrefsFragment())
*               .commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
            default:
                break;
        }
        return true;
    }
}
Preference 实现设置界面_第1张图片

二、获取设置值


prefs.xml 中有设置 android:key 属性,就可以通过这个 key 从 SharedPreferences 中取值。其他 Activity 或 Fragment 等都可以这样取到值。
public class PrefsFragment extends PreferenceFragment {

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

        addPreferencesFromResource(R.xml.prefs);

        SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(getActivity());
        Boolean checkbox=prefs.getBoolean("checkbox_preference",false);
        Toast.makeText(getActivity(), ""+checkbox, Toast.LENGTH_SHORT).show();
    }
}

三、设置点击事件和值改变事件


package com.ttt.zhihudaily.fragment;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;
import android.widget.CheckBox;
import android.widget.Toast;

import com.ttt.zhihudaily.R;

public class PrefsFragment extends PreferenceFragment
        implements Preference.OnPreferenceChangeListener,Preference.OnPreferenceClickListener{

    private SharedPreferences prefs;

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

        addPreferencesFromResource(R.xml.prefs);

        prefs=PreferenceManager.getDefaultSharedPreferences(getActivity());

        CheckBoxPreference cp=(CheckBoxPreference)findPreference("checkbox_preference");
        SwitchPreference sp=(SwitchPreference)findPreference("switch_preference");
        cp.setOnPreferenceClickListener(this);
        sp.setOnPreferenceChangeListener(this);
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        Toast.makeText(getActivity(), "开关按钮2的值是:"+newValue, Toast.LENGTH_SHORT).show();
        return true;
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        Boolean checkboxValue=prefs.getBoolean(preference.getKey(),false);
        Toast.makeText(getActivity(), "开关按钮1的值是:"+checkboxValue, Toast.LENGTH_SHORT).show();
        return true;
    }
}

你可能感兴趣的:(Preference 实现设置界面)