我在这个论坛上搜索了所有内容,但没有得到我真正需要的东西。我需要在“首选项”中有一个自定义DialogPreference,但DialogPreference不应包含我讨厌的蓝条标题,并且我已经为其他活动准备了一个活动标题模板xml文件,可以用作自定义活动标题。在此对话框上。再加上我想要自定义首选项文件名,但是这里的问题是它创建了两个首选项文件名,一个用于首选项,另一个用于DialogPreference
但是我在这里找到了类似的东西使用带有2个用户输入字段的EditTextPreference
<com.yourdomain.YourDialogPreference
android:title="Title"
android:summary="Summary"
android:key="dialog_preference"/>
到目前为止,我已经做到了。DialogPreference打开得很好,但是我如何将标题模板附加到此自定义DialogPreference
我弄清楚了自己。干得好。
第一个在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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句