DialogPreference设置Dialog的标题

时间:2013-06-19 04:57:31

标签: java android dialog listener preference

我有一个DialogPreference。我想设置它的Dialog标题(setTitle()),但它不起作用。

我的代码:

public class RestoreDefaultApperanceDialogPreference extends DialogPreference {

    Dialog dialog;

    public RestoreDefaultApperanceDialogPreference(Context context,
            AttributeSet attrs) {
        super(context, attrs);

        dialog = getDialog();
        //dialog.setTitle("Restore default colors?"); //<- NOT WORKING!
        // TODO Auto-generated constructor stub
    }

    public RestoreDefaultApperanceDialogPreference(Context context,
            AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }
}

XML文件(首选项类别):

<PreferenceCategory android:title="Appearance settings">
        <com.example.dictionary.ColorSelectPreference
            android:key="onlineRecognitionColor"
            android:title="Online recognision results color" 
            android:summary="Customize color of online recognition results."
        />
        <com.example.dictionary.ColorSelectPreference
            android:key="offlineRecognitionColor"
            android:title="Offline recognition results color" 
            android:summary="Customize color of offline recognition results."
        />
        <com.example.dictionary.RestoreDefaultApperanceDialogPreference
            android:key="restoreDefaultApperance"
            android:title="Restore default apperance" 
            android:summary="Choose this option to restore default apperance settings." 
        />
 </PreferenceCategory>

如何设置此对话框的标题

1 个答案:

答案 0 :(得分:2)

使用setDialogTitle()并覆盖onDialogClosed()以捕获按钮点击并执行操作。

public RestoreDefaultApperanceDialogPreference(Context context,
        AttributeSet attrs) {
    super(context, attrs);

    setDialogTitle("Restore default colors?"); // <- this should work
}


@Override
protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);

    if (positiveResult) {
        // OK button is pressed
    } else {
        // Cancel button is pressed
    }
}