其他都比较简单,亮度用了一个自定义的PreferenceActivity
配置
xml/setting.xml
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > 开发论坛 <PreferenceCategory android:title="文本框" > <EditTextPreference android:key="name" android:negativeButtonText="取消" android:positiveButtonText="确定" android:summary="dd" android:title="名字" /> </PreferenceCategory> <PreferenceCategory android:title="选择" > <CheckBoxPreference android:key="sount1" android:summaryOff="关" android:summaryOn="开" android:title="提示音" /> <CheckBoxPreference android:key="zd" android:summaryOff="关" android:summaryOn="开" android:title="震动" /> </PreferenceCategory> <PreferenceCategory android:title="列表" > <ListPreference android:dialogTitle="性别" android:entries="@array/value1" android:entryValues="@array/value1" android:key="xb" android:title="性别" /> <MultiSelectListPreference android:entries="@array/xq" android:entryValues="@array/xq" android:key="xq" android:summary="请选择" android:title="兴趣" /> </PreferenceCategory> <PreferenceCategory android:title="其他" > <com.example.fffff.SeekPreference android:key="tjld" android:summary="版本号0.0.1" android:title="调节亮度" /> <Preference android:key="xtsj" android:summary="版本号0.0.1" android:title="系统升级" /> <Preference android:key="gy" android:title="关于" /> </PreferenceCategory> </PreferenceScreen>
values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">fffff</string> <string name="hello_world">Hello world!</string> <string-array name="value1"> <item >男</item> <item >女</item> </string-array> <string-array name="xq"> <item >游泳</item> <item >跑步</item> <item >吃饭</item> <item >喝酒</item> <item >逛街</item> <item >电脑</item> <item >女人</item> </string-array> </resources>
自定义layout activity_tjld.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="15dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="亮度" android:textAppearance="?android:attr/textAppearanceMedium" /> <SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Small Text" android:textAppearance="?android:attr/textAppearanceSmall" /> </LinearLayout>
MainActivity.java
public class MainActivity extends PreferenceActivity implements OnPreferenceChangeListener{ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //加载设置文件 addPreferencesFromResource(R.xml.setting); SharedPreferences sp = getPreferenceScreen().getSharedPreferences(); //获取值 Preference pref1 =findPreference("name"); pref1.setSummary(sp.getString("name", "请输入")); Preference pref2 =findPreference("xb"); pref2.setSummary(sp.getString("xb", "请输入")); Preference pref3 =findPreference("xq"); if(sp.getStringSet("xq",null)!=null){ pref3.setSummary(sp.getStringSet("xq",null).toString()); } Preference pref6 =findPreference("tjld"); //最终会调用onPreferenceChange pref6.setSummary(sp.getInt("tjld", 0)+""); //设置改变事件 pref1.setOnPreferenceChangeListener(this); pref2.setOnPreferenceChangeListener(this); pref3.setOnPreferenceChangeListener(this); pref6.setOnPreferenceChangeListener(this); Preference pref4 =findPreference("xtsj"); pref4.setOnPreferenceClickListener(new OnPreferenceClickListener() { public boolean onPreferenceClick(Preference preference) { Toast.makeText(MainActivity.this, "升级提示", Toast.LENGTH_SHORT).show(); return true; } }); Preference pref5 =findPreference("gy"); pref5.setOnPreferenceClickListener(new OnPreferenceClickListener() { public boolean onPreferenceClick(Preference preference) { Toast.makeText(MainActivity.this, "关于", Toast.LENGTH_SHORT).show(); return true; } }); } @Override public boolean onPreferenceChange(Preference preference, Object newValue) { preference.setSummary(newValue.toString()); return true; } }
SeekPreference.java
public class SeekPreference extends Preference{ Context context; TextView summaryText; int index=0; public SeekPreference(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context=context; } public SeekPreference(Context context, AttributeSet attrs) { super(context, attrs); this.context=context; } public SeekPreference(Context context) { super(context); this.context=context; } protected void onBindView(View view) { summaryText=(TextView)view.findViewById(R.id.textView2); summaryText.setText(getSummary()); SeekBar seekBar=(SeekBar)view.findViewById(R.id.seekBar1); seekBar.setProgress(getPersistedInt(0)); seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO 自动生成的方法存根 } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO 自动生成的方法存根 } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { persistInt(progress); //回调change事件 callChangeListener(progress); Toast.makeText(context, progress+"", Toast.LENGTH_SHORT).show(); } }); } @Override protected View onCreateView(ViewGroup parent) { return LayoutInflater.from(getContext()).inflate(R.layout.activity_tjld, parent, false); } }