【问题标题】:How to create a single preference with multiple fields?如何创建具有多个字段的单个首选项?
【发布时间】:2016-06-16 12:49:05
【问题描述】:

我的应用程序设置中有一个“更改密码”,我希望用户能够输入 3 个文本字段(当前密码、新密码、重复密码),然后只需单击确定按钮即可保存设置。但问题是我找不到(或无法创建)任何可以显示多个项目的首选项(此处为 3 EditTextPreferences)。

下面的代码在点击编辑时只显示一个文本框。

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <EditTextPreference
        android:capitalize="none"
        android:password="true"
        android:defaultValue=""
        android:inputType="textCapWords"
        android:key="example_text"
        android:maxLines="1"
        android:selectAllOnFocus="true"
        android:singleLine="true"
        android:title="@string/pref_default_current_password" />

</PreferenceScreen>

我正在寻找类似的东西:

<Group>
        <EditTextPreference
            android:capitalize="none"
            android:password="true"
            android:defaultValue=""
            android:inputType="textCapWords"
            android:key="example_text"
            android:maxLines="1"
            android:selectAllOnFocus="true"
            android:singleLine="true"
            android:title="@string/pref_default_current_password" />

        <EditTextPreference
            android:capitalize="none"
            android:password="true"
            android:defaultValue=""
            android:inputType="textCapWords"
            android:key="example_text"
            android:maxLines="1"
            android:selectAllOnFocus="true"
            android:singleLine="true"
            android:title="@string/pref_default_new_password" />

        <EditTextPreference
            android:capitalize="none"
            android:password="true"
            android:defaultValue=""
            android:inputType="textCapWords"
            android:key="example_text"
            android:maxLines="1"
            android:selectAllOnFocus="true"
            android:singleLine="true"
            android:title="@string/pref_default_repeat_password" />
</Group>

【问题讨论】:

    标签: android android-preferences


    【解决方案1】:

    您需要创建一个自定义DialogPreference。您的方法在我看来有缺陷,您只希望将一个首选项值存储在 SharedPreferences 中。

    创建一个扩展DialogPreference 的类,返回您的膨胀视图onCreateDialogView(),并在单击肯定按钮时添加一些逻辑。你管理它是一个片段或活动类,只是扩展DialogPreference

    public class CustomPreference extends DialogPreference {
    
        EditText first;
        EditText second;
        EditText third;
    
        protected View onCreateDialogView() {
            View v = LayoutInflater.from(getContext()).inflate(R.layout.three_edit_texts, null);
            first = v.findViewById(...);
            second = v.findViewById(...);
            third = v.findViewById(...);
            return v;
        }
    
    }
    

    然后在 XML 中添加:

    <com.your.package.CustomPreference />
    

    【讨论】:

    • 您的回答似乎是正确的,但由于我对此很陌生,您能举个例子吗?
    【解决方案2】:

    您需要在按下提交按钮时按顺序进行。

    Editor editor = sharedpreferences.edit();
    
    editor.putString("key1", "value1");
    editor.putString("key2", "value2");
    editor.putString("key3", "value3"); 
    editor.commit();
    

    或者更好地创建共享首选项工具以避免此类样板。

    【讨论】:

    • 我的问题(至少目前)不是保存首选项,而是强制用户同时输入多个文本框。想象一下首先显示“当前密码”文本框。当用户提交它时,我应该如何处理它?我需要用户同时填写所有三个文本框。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多