3

我搜索了所有圍繞此論壇,但沒有得到我確切需要的東西。我需要一個自定義DialogPreference的偏好,但該DialogPreference不應該有藍色條頭,我討厭,而且我已經準備好一個活動頭模板xml文件的其他活動,可以用作自定義活動header.so我想用在這個dialogPrerence上。另外,我想自定義首選項文件的名稱,但這裏的問題是,它創建了兩個偏好文件名,一個偏好,另一個用於DialogPreference如何在偏好內製定一個自定義對話偏好

,但我發現這樣的事情在這裏Using EditTextPreference with 2 user input fields

<com.yourdomain.YourDialogPreference 
     android:title="Title" 
     android:summary="Summary" 
     android:key="dialog_preference"/> 

我已經這樣做與此相距甚遠。 DialogPreference可以打開,但是我怎樣才能將我的標題模板附加到這個自定義的DialogPreference中

回答

4

我想出了我的自我。幹得好。

1包括在DialogPreference XML

<include layout="@layout/activity_header_template" /> 

的頁眉模板下面一行,並準備自己的自定義對話框佈局就像正常的自定義對話框模板。真正需要的是,我想自定義DialogPreference,我要爲密碼1和密碼2.兩個輸入(只是爲了確認密碼)

這是我ListPreference XML代碼

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 

    <PreferenceCategory android:title="@string/preference_header_encryption"> 

     <CheckBoxPreference 
      android:key="prefkey_use_passcode" 
      android:title="@string/preference_name_set_passcode" 
      android:summary="@string/preference_summary_set_passcode" /> 

     <!-- This is how you need to attach CustomDialogPrefernce, by using the class name --> 
     <!-- Please ignore title here. Title will come from DialogPreference Constructor --> 
     <com.nerds.notes.SettPassword 
      android:key="prefkey_set_passcode" 
      android:summary="@string/preference_app_protection" 
      android:dialogMessage="@string/action_delete" 
      android:positiveButtonText="@string/passcode_ok_button_text" 
      android:negativeButtonText="@string/passcode_cancel_button_text" 
      android:dependency="prefkey_use_passcode" /> 

     <CheckBoxPreference 
      android:key="prefkey_app_protection" 
      android:title="@string/preference_app_protection" 
      android:summary="@string/preference_summary_app_protection" 
      android:dependency="prefkey_use_passcode" /> 

    </PreferenceCategory> 

</PreferenceScreen> 

以下行很重要的是,DialogPreference構造

public SettPassword(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    setPersistent(false); 
    setTitle(R.string.preference_name_set_passcode); // This will override ListPreference Title 
    setDialogLayoutResource(R.layout.passcode_set_dialog_template); 
} 

以下行應在ListPreference onCreate方法進行編碼,具有自定義首選項文件名

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    PreferenceManager manager = getPreferenceManager(); 
    manager.setSharedPreferencesName("Your Preference File Name"); 
    manager.setSharedPreferencesMode(MODE_PRIVATE); 

    addPreferencesFromResource(R.xml.settings); // ListPreference XML file from XML Folder 
} 
+0

我在這裏使用的一些方法是折舊 – Kirk