Structured(分组的Prefereces)
- 新建项目 ProfsDemo_Structured. 编辑res/layout/main.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TableRow>
<TextView android:text="Checkbox:" android:paddingRight="5px" />
<TextView android:id="@+id/checkbox" />
</TableRow>
<TableRow>
<TextView android:text="Ringtone:" android:paddingRight="5px" />
<TextView android:id="@+id/ringtone" />
</TableRow>
<TableRow>
<TextView android:text="Checkbox #2:" android:paddingRight="5px" />
<TextView android:id="@+id/checkbox2" />
</TableRow>
</TableLayout>
2. 在res/新建目录xml 在xml目录下新建preferences.xml 布局文件:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="简单配置">
<CheckBoxPreference android:summary="CheckBox1 的描述" android:key="checkbox" android:title="CheckBox1"></CheckBoxPreference>
<RingtonePreference android:title="铃声配置"
android:key="ringtone" android:showDefault="true" android:showSilent="true"></RingtonePreference>
</PreferenceCategory>
<PreferenceCategory android:title="详细配置">
<PreferenceScreen android:key="detail" android:title="详细"
android:summary="附加的配置在另一页上">
<CheckBoxPreference android:summary="CheckBox状态描述"
android:key="checkbox2" android:title="另一个CheckBox"></CheckBoxPreference>
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
3. 添加类EditPreferences 继承自 PreferenceActivity类
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class EditPreferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
4. 编辑PrefsDemo_Structured 类
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class PrefsDemo_Structured extends Activity {
private static final int EDIT_ID = Menu.FIRST+2;
private TextView checkbox = null;
private TextView ringtone = null;
private TextView checkbox2 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkbox = (TextView)findViewById(R.id.checkbox);
ringtone = (TextView)findViewById(R.id.ringtone);
checkbox2 = (TextView)findViewById(R.id.checkbox2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE,EDIT_ID,Menu.NONE,"设置").setAlphabeticShortcut('e');
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case EDIT_ID:
startActivity(new Intent(this,EditPreferences.class));
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
checkbox.setText(new Boolean(prefs.getBoolean("checkbox", false)).toString());
ringtone.setText(prefs.getString("ringtone", "未设置"));
checkbox2.setText(new Boolean(prefs.getBoolean("checkbox2", false)).toString());
}
}
创建设置菜单,启动配置Activity屏幕。在onResume()方法中获取配置的信息。
5. 在AndroidMainfest.xml中添加配置
<activity android:name="EditPreferences"></activity>