2

我有一个 DialogPreference。我想设置它的对话框标题(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>

如何设置此对话框的标题

4

1 回答 1

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
    }
}
于 2013-06-19T05:30:35.187 回答