8.1 Android Basic 数据存储 Preferences Xml 描述 Preferences
使用Xml布局文件描述 Preferences
-
新建项目 Prefs_Simple 编辑 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/ringtong"/>
</TableRow>
</TableLayout>
2. 在res/下新建 preferences.xml 布局文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="checkbox"
android:title="Checkbox 配置"
android:summary="Check it on, check it off"/>
<RingtonePreference
android:key="ringtone"
android:title="铃声配置"
android:showDefault="true"
android:showSilent="true"
android:summary="请选择一个铃声"/>
</PreferenceScreen>
3. 新建类 EditPreferences 继承自PreferenceActivity:
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.Menu;
import android.widget.TextView;
public class EditPreferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//加载布局xml
addPreferencesFromResource(R.xml.preferences);
}
}
使用 addPreferencesFromResource()方法加载 Prefernces的布局文件。
4. 编辑PrefsDemo_Simple类:
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_Simple extends Activity {
private static final int EDIT_ID = Menu.FIRST+2;
private TextView checkbox = null;
private TextView ringtone = 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.ringtong);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE,EDIT_ID,Menu.NONE,"Edit Prefs")
.setIcon(R.drawable.misc)
.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() {
super.onResume();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
checkbox.setText(new Boolean(prefs.getBoolean("checkbox", false)).toString());
ringtone.setText(prefs.getString("ringtone", "<unset>"));
}
}
程序创建了一个菜单,单击菜单按钮后进入配置界面。
在onResume方法中获取配置信息, Key 是在配置文件中指定的。
5. 在AndroidManifest.xml中配置activity
<activity android:name="EditPreferences"></activity>