в вашем xml используйте следующее вместо DialogPreference:
<Preference
android:title="This acts as a button"
android:key="button"
android:summary="This can act like a button to create it's own dialog"/>
Затем в Java:
Preference button = (Preference)findPreference("button");
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference arg0) {
showDialog(MY_DIALOG); // let's say MY_DIALOG is 'final int MY_DIALOG = 1;' in the class body
return false;
}
});
Затем добавьте в тело вашего класса:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case SHOW_APP_STRING:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mylayout = inflater.inflate(R.layout.mylayout, null);
final AlertDialog myDialog = new AlertDialog.Builder(this)
.setView(mylayout)
.show();
//The buttons are below the dialog so you can close the dialog within your button listeners
Button save = (Button)myLayout.findViewById(R.id.save);
Button cancel = (Button)myLayout.findViewById(R.id.cancel);
//set onClickListeners for both of your buttons
return myDialog;
}
}
Я не уверен, что это лучший метод, но это то, как я это делаю, и он работает.