8.1 Android Basic 数据存储 Preferences Dialog(使用对话框的Preferences)
Dialog(使用对话框的Preferences)
-
新建项目 PrefsDemo_Dialog 编辑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>
<TableRow>
<TextView android:text=“Text:” android:paddingRight=“5px” />
<TextView android:id=“@+id/text” />
</TableRow>
<TableRow>
<TextView android:text=“List Selection:”
android:paddingRight=“5px” />
<TextView android:id=“@+id/list” />
</TableRow>
</TableLayout>
2. 在res/新建xml目,在xml目录下新建preferneces.xml 布局文件
<?xml version=“1.0” encoding=“utf-8”?>
<PreferenceScreen xmlns:android=“http://schemas.android.com/apk/res/android”>
<PreferenceCategory android:title=“简单的配置“>
<CheckBoxPreference android:summary=“CheckBox说明说明“
android:key=“checkbox” android:title=“Checkbox配置“></CheckBoxPreference>
<RingtonePreference android:title=“铃声设置“
android:key=“ringtone” android:summary=“请选铃声…..”
android:ringtoneType=“ringtone|notification|alarm|all”
android:showDefault=“true” android:showSilent=“true”></RingtonePreference>
</PreferenceCategory>
<PreferenceCategory android:title=“详细页“>
<PreferenceScreen android:key=“detail” android:title=“Detail Screen”
android:summary=“Additional preferences held in another page”>
<CheckBoxPreference android:summary=“另一个CheckBox”
android:key=“checkbox2” android:title=“CheckBox2配置“></CheckBoxPreference>
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android:title=“其他配置“>
<EditTextPreference
android:key=“text”
android:title=“Text Entry Dialog”
android:summary=“Click to pop up a field for entry”
android:dialogTitle=“Enter something useful”
/>
<ListPreference android:title=“Selection Dialog”
android:entryValues=“@array/airport_codes” android:entries=“@array/cities”
android:key=“list” android:summary=“Click to pop up a list a choose from”
android:dialogTitle=“Choose a City”></ListPreference>
</PreferenceCategory>
</PreferenceScreen>
3. 添加EditProferences类继承自ProferencesActivity类:
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. 修改 ProfsDemo_Dialog类:
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_Dialog extends Activity {
private static final int EDIT_ID = Menu.FIRST + 2;
private TextView checkbox = null;
private TextView ringtone = null;
private TextView checkbox2 = null;
private TextView text = null;
private TextView list = 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);
text = (TextView) findViewById(R.id.text);
list = (TextView) findViewById(R.id.list);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, EDIT_ID, Menu.NONE, “Edit Prefs”)
.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”, “<unset>”));
checkbox2.setText(new Boolean(prefs.getBoolean(“checkbox2”, false))
.toString());
text.setText(prefs.getString(“text”, “<unset>”));
list.setText(prefs.getString(“list”, “<unset>”));
}
}
5. 在AndroidManifest.xml中配置EditPreferences Activity
<activity android:name=“EditPreferences”></activity>