我在android中开发Application 如果用户从首选项屏幕检查checkboxpreference,我想显示AlertDialog。 那我该怎么做..?
答案 0 :(得分:16)
试试这个...
public class MyPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
final Preference preference) {
if(preference.equals("MyCheckboxPreferenceKey")) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your Message");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//action on dialog close
}
});
builder.show();
}
}
答案 1 :(得分:1)
覆盖onSharedPreferenceChanged
课程中的PreferenceActivity
:
public class MyPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
...
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("MyCheckboxPreferenceKey")) {
//Show your AlertDialog here!
}
}
答案 2 :(得分:0)
public class MyPreferences extends PreferenceActivity{
...
//getting current context for builder
AlertDialog.Builder build = new AlertDialog.Builder(this);
//setting some title text
build.setTitle("SomeTitle");
//setting radiobuttons list
build.setSingleChoiceItems(new String[]{"One", "Two"}, 0, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Some behavior here
}
});
build.setNegativeButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Might be empty
}
});
//creating dialog and showing
AlertDialog dialog = build.create();
dialog.show();
}
就是这样。