问题描述
|
我正在开发Android中的应用程序
如果用户从首选项屏幕中选中checkBoxpreference,我想显示AlertDialog。
所以我该怎么做..?
解决方法
在您的
PreferenceActivity
类中覆盖onSharedPreferenceChanged
:
public class MyPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
...
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {
if (key.equals(\"MyCheckboxPreferenceKey\")) {
//Show your AlertDialog here!
}
}
,试试这个...
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();
}
}
,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\"},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();
}
就这样。