在v7支持DialogPreference中找不到onCreateDialogView和onDialogClosed

时间:2015-10-06 15:59:14

标签: android

以前,我正在使用PreferenceFragment。因此,要从PreferenceFragment调用自定义对话框,我将使用DialogPreference,如下所示。

ScanSpeedDialogPreference.java

import android.preference.DialogPreference;

public class ScanSpeedDialogPreference extends DialogPreference {

    public ScanSpeedDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        setDialogLayoutResource(R.layout.scan_speed_dialog_preference);
    }

    public ScanSpeedDialogPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setDialogLayoutResource(R.layout.scan_speed_dialog_preference);
    }   

    @SuppressLint("NewApi")
    @Override
    protected View onCreateDialogView() {
        View view = super.onCreateDialogView();

        // Perform all UI fine tuning and event handler installation...

        return view;
    }

    // http://stackoverflow.com/questions/13851554/why-custom-dialogpreference-doesnt-trigger-on-onsharedpreferencechanged-listener
    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);
        if (positiveResult) {
            persistBoolean(!getPersistedBoolean(true));
        }
    } 
}

最近,我的迁移代码从PreferenceFragmentandroid.support.v7.preference.PreferenceFragmentCompat

因此,我不能再使用DialogPreference了。

我需要一个类,它是android.support.v7.preference.Preference

的子类

我尝试将DialogPreference更改为以下

ScanSpeedDialogPreference.java

import android.support.v7.preference.DialogPreference;

public class ScanSpeedDialogPreference extends DialogPreference {

    public ScanSpeedDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        setDialogLayoutResource(R.layout.scan_speed_dialog_preference);
    }

    public ScanSpeedDialogPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setDialogLayoutResource(R.layout.scan_speed_dialog_preference);
    }   

    @SuppressLint("NewApi")
    @Override
    protected View onCreateDialogView() {
        View view = super.onCreateDialogView();

        // Perform all UI fine tuning and event handler installation...

        return view;
    }

    // http://stackoverflow.com/questions/13851554/why-custom-dialogpreference-doesnt-trigger-on-onsharedpreferencechanged-listener
    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);
        if (positiveResult) {
            persistBoolean(!getPersistedBoolean(true));
        }
    } 
}

然而,出现了2个问题。

  1. v7 DialogPreference没有onCreateDialogView
  2. v7 DialogPreference没有onDialogClosed
  3. 我想知道,在v7 onCreateDialogView中实现onDialogClosedDialogPreference的类似功能的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

使用支持库时,请不要使用DialogPreference。相反,我们应该使用PreferenceDialogFragmentCompat

有关详细信息,请参阅Adding toolbar to PreferenceActivity via AppCompatPreferenceActivity causes overlapping