Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 单击listpreference中的项目时显示警报对话框_Android - Fatal编程技术网

Android 单击listpreference中的项目时显示警报对话框

Android 单击listpreference中的项目时显示警报对话框,android,Android,大家好,我是android开发新手。当用户在首选项活动的列表首选项中选择任何主题时,我想打开一个警报对话框。我在谷歌搜索了很多内容,但没有找到任何合适的答案。这是我的首选项活动 public class Setting extends PreferenceActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bund

大家好,我是android开发新手。当用户在首选项活动的列表首选项中选择任何主题时,我想打开一个警报对话框。我在谷歌搜索了很多内容,但没有找到任何合适的答案。这是我的首选项活动

public class Setting extends PreferenceActivity { 

    /** Called when the activity is first created. */  
        @Override  
        public void onCreate(Bundle savedInstanceState) { 
            Setting.setAppTheme(this);
            super.onCreate(savedInstanceState);  
            addPreferencesFromResource(R.xml.prefs); 

        } 


    String ListPreference;  

        public static void setAppTheme(Activity a)  {  
            // Get the xml/preferences.xml preferences  
            SharedPreferences prefs = PreferenceManager  
                            .getDefaultSharedPreferences(a); 
         int ListPreference = Integer.parseInt(prefs.getString("listPref", "3"));
         if(ListPreference == 0) {
                a.setTheme(R.style.AppBaseThemeDark);
                return;
                } else if(ListPreference == 1){
                    a.setTheme(R.style.AppBaseThemeLight);
                    //Toast.makeText(getApplicationContext(),"TTS Engines not found.\n Install TTS Engins",Toast.LENGTH_LONG).show();
                } else if(ListPreference == 2){
                    a.setTheme(R.style.AppBaseTheme);
                }

   }

        public boolean onCreateOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);
            getActionBar().setDisplayHomeAsUpEnabled(true);

            return true;
        }
     public boolean onOptionsItemSelected(MenuItem item) {
         switch (item.getItemId()) {
                case android.R.id.home:
                        // app icon in action bar clicked; go home
                    getFragmentManager().popBackStack();
                    finish();
                        return true;

            }
            return super.onOptionsItemSelected(item);


}
}

几天前我遇到了同样的问题,为此我实现了一个扩展
ListPreference
的自定义首选项类。这是我实现的类:

public class LogCleanPreference extends ListPreference {
    private int mClickedDialogEntryIndex;
    
    private Context mContext;

    public LogCleanPreference(Context ctxt) {
        this(ctxt, null);
    }

    public LogCleanPreference(Context ctxt, AttributeSet attrs) {
        super(ctxt, attrs);
        
        mContext = ctxt;

        setNegativeButtonText(ctxt.getString(R.string.alert_cancel));
    }

    @Override
    protected void onPrepareDialogBuilder(Builder builder) {
        if (getEntries() == null || getEntryValues() == null) {
            throw new IllegalStateException(
                    "ListPreference requires an entries array and an entryValues array.");
        }

        mClickedDialogEntryIndex = findIndexOfValue(getValue());
        builder.setSingleChoiceItems(getEntries(), mClickedDialogEntryIndex, 
                new DialogInterface.OnClickListener() {
            public void onClick(final DialogInterface dialog, final int which) {
                // In my case I only show the AlertDialog if the user didn't select option number 2
                if(which != 2){
                    // Show AlertDialog
                }
                else{
                    // Save preference and close dialog
                    mClickedDialogEntryIndex = which;
    
                    LogCleanPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                    dialog.dismiss();
                }
            }
        });

        builder.setPositiveButton(null, null);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {

        CharSequence[] mEntryValues = getEntryValues();

        if (positiveResult && mClickedDialogEntryIndex >= 0 && mEntryValues != null) {
            String value = mEntryValues[mClickedDialogEntryIndex].toString();
            if (callChangeListener(value)) {
                setValue(value);
            }
        }
    }
}
以下是我在prefs.xml中使用首选项的方式:

<com.timeondriver.tod.settings.LogCleanPreference
        android:defaultValue="0"
        android:dialogTitle="@string/dialog_title_log_clean"
        android:entries="@array/log_clean"
        android:entryValues="@array/log_clean_values"
        android:key="log_clean_preference"
        android:summary="@string/summary_log_clean_preference"
        android:title="@string/title_log_clean_preference" />

几天前我遇到了同样的问题,为此我实现了一个自定义的首选项类,扩展了
ListPreference
。这是我实现的类:

public class LogCleanPreference extends ListPreference {
    private int mClickedDialogEntryIndex;
    
    private Context mContext;

    public LogCleanPreference(Context ctxt) {
        this(ctxt, null);
    }

    public LogCleanPreference(Context ctxt, AttributeSet attrs) {
        super(ctxt, attrs);
        
        mContext = ctxt;

        setNegativeButtonText(ctxt.getString(R.string.alert_cancel));
    }

    @Override
    protected void onPrepareDialogBuilder(Builder builder) {
        if (getEntries() == null || getEntryValues() == null) {
            throw new IllegalStateException(
                    "ListPreference requires an entries array and an entryValues array.");
        }

        mClickedDialogEntryIndex = findIndexOfValue(getValue());
        builder.setSingleChoiceItems(getEntries(), mClickedDialogEntryIndex, 
                new DialogInterface.OnClickListener() {
            public void onClick(final DialogInterface dialog, final int which) {
                // In my case I only show the AlertDialog if the user didn't select option number 2
                if(which != 2){
                    // Show AlertDialog
                }
                else{
                    // Save preference and close dialog
                    mClickedDialogEntryIndex = which;
    
                    LogCleanPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                    dialog.dismiss();
                }
            }
        });

        builder.setPositiveButton(null, null);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {

        CharSequence[] mEntryValues = getEntryValues();

        if (positiveResult && mClickedDialogEntryIndex >= 0 && mEntryValues != null) {
            String value = mEntryValues[mClickedDialogEntryIndex].toString();
            if (callChangeListener(value)) {
                setValue(value);
            }
        }
    }
}
以下是我在prefs.xml中使用首选项的方式:

<com.timeondriver.tod.settings.LogCleanPreference
        android:defaultValue="0"
        android:dialogTitle="@string/dialog_title_log_clean"
        android:entries="@array/log_clean"
        android:entryValues="@array/log_clean_values"
        android:key="log_clean_preference"
        android:summary="@string/summary_log_clean_preference"
        android:title="@string/title_log_clean_preference" />

首先,创建一个更改侦听器:

private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object value) {
            String stringValue = value.toString();

            if (preference instanceof ListPreference) {
                // For list preferences, look up the correct display value in
                // the preference's 'entries' list.
                ListPreference listPreference = (ListPreference) preference;
                int index = listPreference.findIndexOfValue(stringValue);

                // Set the summary to reflect the new value.
                preference
                        .setSummary(index >= 0 ? listPreference.getEntries()[index]
                                : null);

            }
            return true;
        }
    };
第二,将偏好绑定到其值:

// Set the listener to watch for value changes.
        preference
                .setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);

        // Trigger the listener immediately with the preference's
        // current value.
        sBindPreferenceSummaryToValueListener.onPreferenceChange(
                preference,
                PreferenceManager.getDefaultSharedPreferences(
                        preference.getContext()).getString(preference.getKey(),
                        ""));

第3步,在第一步中,您可以插入代码以启动带有if条件的对话框。

1、创建更改侦听器:

private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object value) {
            String stringValue = value.toString();

            if (preference instanceof ListPreference) {
                // For list preferences, look up the correct display value in
                // the preference's 'entries' list.
                ListPreference listPreference = (ListPreference) preference;
                int index = listPreference.findIndexOfValue(stringValue);

                // Set the summary to reflect the new value.
                preference
                        .setSummary(index >= 0 ? listPreference.getEntries()[index]
                                : null);

            }
            return true;
        }
    };
第二,将偏好绑定到其值:

// Set the listener to watch for value changes.
        preference
                .setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);

        // Trigger the listener immediately with the preference's
        // current value.
        sBindPreferenceSummaryToValueListener.onPreferenceChange(
                preference,
                PreferenceManager.getDefaultSharedPreferences(
                        preference.getContext()).getString(preference.getKey(),
                        ""));

第三,在第一步中,您可以插入代码来启动带有if条件的对话框。

当所述类嵌套在我的原始类中时,我似乎很难让它工作。有什么提示吗?这可能是个老问题,但我在代码中发现了一个错误:
if(getEntries()==null | | getEntries()==null)
应该替换为
if(getEntries()==null | | getEntryValues()==null)
当所述类嵌套在我原来的类中时,我似乎很难让它工作。有什么提示吗?这可能是个老问题,但我在代码中发现了一个错误:
if(getEntries()==null | | getEntries()==null)
应该替换为
if(getEntries()==null | | getEntryValues()==null)