源码及APK下载: http://files.note.sdo.com/7pq1Y~jGuguFnM0b0006dI
Android提供了如下的设置页面,通过PreferenceScreen及PreferenceActivity的组合就可以实现。
1. 创建一个Settings类继承于PreferenceActivity,在onCreate中加载创建好的设置界面即可。
public class Settings extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
2. 设置界面显示 (有自定义DialogPreference)
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/app_name">
<PreferenceCategory android:title="@string/settings_lock"
xmlns:android="http://schemas.android.com/apk/res/android">
<cn.sh.zyh.inote.pref.LockPreference
android:summary="@string/lock_summary"
android:key="@string/lock_key"
android:title="@string/lock_title"
/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/settings_backup"
xmlns:android="http://schemas.android.com/apk/res/android">
<cn.sh.zyh.inote.pref.ExportPreference
android:summary="@string/export_summary"
android:title="@string/export_title"
android:key="@string/export_key"/>
<cn.sh.zyh.inote.pref.RestorePreference
android:summary="@string/restore_summary"
android:title="@string/restore_title"
android:key="@string/restore_key"/>
</PreferenceCategory>
</PreferenceScreen>
public class LockPreference extends DialogPreference {
public LockPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.lock_pop); //设置弹出窗口的layout
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
updateUI(view); //更新界面
}
private void updateUI(View view) {
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
//点击确定按钮后,可执行的处理
}
}
}
下图密码窗口的layout文件(lock_pop)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5px"
android:paddingRight="5px">
<TextView android:text="@string/curpwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_height="wrap_content"
android:id="@+id/curpwd"
android:layout_width="fill_parent"
android:enabled="false"
android:password="true"
android:inputType="number"
/>
<TextView android:text="@string/newpwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_height="wrap_content"
android:id="@+id/newpwd"
android:layout_width="fill_parent"
android:password="true"
android:inputType="number"/>
<TextView android:text="@string/repeatpwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_height="wrap_content"
android:id="@+id/repeatpwd"
android:layout_width="fill_parent"
android:password="true"
android:inputType="number"/>
<CheckBox android:id="@+id/cancelpwd"
android:layout_height="wrap_content"
android:text="@string/cancelpwd"
android:layout_width="wrap_content"
android:checked="false"/>
</LinearLayout>