gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:20:59 25 4
gpt4 key购买 nike

大家好,我是 Android 开发的新手。当用户在偏好 Activity 的列表偏好中选择任何主题时,我想打开一个警告对话框。我在谷歌中搜索了很多但没有找到任何合适的答案。这是我的 PrefenceActivity。

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" />

关于android - 单击 listpreference-android 中的项目时显示警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19131628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com