0

我正在尝试使用AndroidX制作首选项屏幕,但遇到了一个小问题......

当我使用虚拟键盘上的“确定”按钮验证我的输入时,不会验证首选项。(键盘被隐藏了,仅此而已...)

使用AndroidX,我们不能再覆盖showDialog()...EditTextPreference而且我找不到另一种方法来访问对话框以将其关闭。

总之,我希望:虚拟键盘上按钮的 DONE 操作验证 AlertDialog。

在此处输入图像描述

这是我的示例代码:

XML 文件

<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
    <EditTextPreference
        app:key="pref_1"
        app:title="Test"
        app:useSimpleSummaryProvider="true" /> 
</PreferenceScreen> 

偏好活动

public class MySettingsActivity extends AppCompatActivity implements
        PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {

    [...]

    public static class HeaderFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.header_preferences, rootKey);

            EditTextPreference pref1 = getPreferenceManager().findPreference("pref_1");
            pref1.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
                @Override
                public void onBindEditText(@NonNull EditText editText) {
                    editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
                    editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
                }
            });
        }
    }
}
4

2 回答 2

2

如果要验证文本的更新值,则需要使用此侦听器

pref1.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                // put validation here..

                Log.d("Prefrence====>",newValue+"<===  validate you can validate this value");

                return true; //if you pass false here the updated value will not save
            }
        });

这是使用 true false return 的示例

public static class HeaderFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.headerpref, rootKey);

        EditTextPreference pref1 = getPreferenceManager().findPreference("pref_1");

        pref1.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                // put validation here..

                Log.d("Prefrence====>",newValue+"<===  validate you can validate this value");
                if(newValue.toString().length()>0){
                    return  true;
                }else return false;
            }
        });

        pref1.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
            @Override
            public void onBindEditText(@NonNull EditText editText) {
                editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
                editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
                editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                        //Your dialog will dissmiss on ok click
                        if(actionId==EditorInfo.IME_ACTION_DONE)dismissDialog();
                        return false;
                    }
                });

            }
        });


    }
void dismissDialog(){
        for(Fragment fragments : HeaderFragment.this.getActivity().getSupportFragmentManager().getFragments()){
            if(fragments instanceof EditTextPreferenceDialogFragmentCompat){
                EditTextPreferenceDialogFragmentCompat dialog = ((EditTextPreferenceDialogFragmentCompat) fragments);
                dialog.onDialogClosed(true);
                dialog.dismiss();
                return;
            }
        }
    }
}

由于您想手动关闭对话框,您可以这样做,因为没有可用的直接方法来获取对话框并关闭它。

我更新了对话框关闭侦听器,因为您需要保存在onDialogClosed方法中传递 true 的数据将有助于保存您的数据

void dismissDialog(){

        for(Fragment fragments : HeaderFragment.this.getActivity().getSupportFragmentManager().getFragments()){
            if(fragments instanceof EditTextPreferenceDialogFragmentCompat){
                EditTextPreferenceDialogFragmentCompat dialog = ((EditTextPreferenceDialogFragmentCompat) fragments);
                dialog.onDialogClosed(true);
                dialog.dismiss();
                return;
            }
        }
    }
于 2020-04-03T11:55:55.680 回答
0

我找到了另一种方法。我不认为这是最好的,但如果它可以帮助任何人......

PreferenceFragmentCompat 自定义

public class PreferenceFragmentCompatCustom extends PreferenceFragmentCompat {
    private static final String DIALOG_FRAGMENT_TAG =
            "androidx.preference.PreferenceFragment.DIALOG";
    private DialogFragment mDialogFragment;

    @Override
    public void onDisplayPreferenceDialog(Preference preference) {
        if (getActivity() instanceof MySettingsActivity) {
            final DialogFragment f;
            if (preference instanceof EditTextPreference) {
                f = EditTextPreferenceDialogFragmentCompat.newInstance(preference.getKey());
            } else if (preference instanceof ListPreference) {
                f = ListPreferenceDialogFragmentCompat.newInstance(preference.getKey());
            } else {
                throw new IllegalArgumentException("Tried to display dialog for unknown " +
                        "preference type. Did you forget to override onDisplayPreferenceDialog()?");
            }
            f.setTargetFragment(this, 0);

            // Expose the dialog
            mDialogFragment = f;

            f.show(getFragmentManager(), DIALOG_FRAGMENT_TAG);

        }else{
            super.onDisplayPreferenceDialog(preference);
        }
    }

    public DialogFragment getDialog(){ return mDialogFragment;}

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {}
}

偏好活动

public class MySettingsActivity extends AppCompatActivity implements
        PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {

    [...]

    public static class HeaderFragment extends PreferenceFragmentCompatCustom {
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.header_preferences, rootKey);

            EditTextPreference pref1 = getPreferenceManager().findPreference("pref_1");
            pref1.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
                @Override
                public void onBindEditText(@NonNull EditText editText) {
                    editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
                    editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

                    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                        @Override
                        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                            if (actionId == EditorInfo.IME_ACTION_DONE) {
                                // If callOnClick, do not save value here
                                //etpTest.setText(v.getText().toString());
                                // If dismiss, the preference is not set !
                                //getDialog().dismiss();

                                ((AlertDialog) getDialog().getDialog())
                                               .getButton(DialogInterface.BUTTON_POSITIVE)
                                               .callOnClick();
                                return true;
                            }
                            return false;
                        }
                    });
                }
            });
        }
    }
}
于 2020-04-03T15:06:57.277 回答