因为DialogPreference 是public abstract class,所以不能像Preference、EditTextPreference等直接用来定义DialogPreference.
需要定义自己的DialogPreference,例如MyDialogPreference。MyDialogPreference可以直接引用定义DialogPreference类型的preference。
1.自定义MyDialogPreference
public class MyDialogPreference extends DialogPreference{
boolean isBack = false;
public MyDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
}
2.定义MyDialogPreference类型的preference
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<Preference android:key="preference" android:title="preference"/>
<com.android.MyDialogPreference
android:key="myDialogPreference"
android:dialogMessage="message"
android:dialogTitle="title"
android:positiveButtonText="ok"
android:negativeButtonText="cancel"
/>
</PreferenceScreen>
3.引用MyDialogPreference
public class DialogPreferenceActivity extends PreferenceActivity {
private Preference myPreference;
private MyDialogPreference myDialogPreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.main);
PreferenceManager manager = getPreferenceManager();
myPreference = manager.findPreference("preference");
myDialogPreference = (MyDialogPreference )findPreference("dialogExPreference");
myDialogPreference.setSummary("ok");
}
/** Called when the activity is first created. */
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
Preference preference) {
// TODO Auto-generated method stub
if(preference.equals(myPreference)){
;
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
}