如果你想选择偏好,这里是你可以做到的。
1) 声明要存储在 XML 文件中的首选项,例如res/xml/preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="pref_name"
android:title="Name"
android:defaultValue="" />
<EditTextPreference
android:key="pref_address"
android:title="Address"
android:defaultValue="" />
<EditTextPreference
android:key="pref_support"
android:title="Support"
android:defaultValue="" />
</PreferenceScreen>
2) 扩展PreferenceActivity
以显示允许修改这些首选项的屏幕:
public class TestActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
3) 使用这些首选项发送电子邮件,例如:
private void sendEmail(String emailAddress) {
String name = getPreferences(0).getString("pref_name", "");
String address = getPreferences(0).getString("pref_address", "");
String support = getPreferences(0).getString("pref_support", "");
String mailBody = "Name: " + name + "\nAddress: " + address + "\nSupport: " + support;
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_EMAIL, emailAddress);
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, mailBody);
startActivity(Intent.createChooser(intent, "Send Email"));
}