2013-06-19 3 views
1

У меня есть DialogPreference. Я хотел бы установить его заголовок Dialog (setTitle()), но он не работает.DialogPreference set title of Dialog

Мой код:

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 FILE (Preference категория):

<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> 

Как я могу SETTITLE в этом диалоге

+1

Вы пробовали это: общественной недействительным setDialogTitle (CharSequence dialogTitle) Устанавливает заголовок диалогового окна. Это будет показано в последующих диалоговых окнах. –

ответ

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 
    } 
} 
+0

Когда я вызываю setTitle в Preference, он изменит название и название заголовка. Я хочу, чтобы, например, «Нажмите, чтобы сбросить настройки», и «Вы уверены?» в диалоге. – Marek

+0

Я вижу, см. Обновленный код. Спасибо за головы, @ Ankxx13. – ozbek