Android ListPreference onClickListener需要两次执行
所以我遇到了一个问题,我的ListPreference需要在它上点击两下,才能真正执行onClick侦听器中的操作。这是我的密码Android ListPreference onClickListener需要两次执行,android,onclicklistener,preference,Android,Onclicklistener,Preference,所以我遇到了一个问题,我的ListPreference需要在它上点击两下,才能真正执行onClick侦听器中的操作。这是我的密码 @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { final ListPreference prefListThemes = (ListPreference) findPreference("prefMateria
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
final ListPreference prefListThemes = (ListPreference) findPreference("prefMaterialThemes");
prefListThemes.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
System.out.println("Test prntln");
// Restart Activity to apply Theme
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
return false;
}
});
正如你所看到的,我尝试做的只是简单地重新启动应用程序来应用我的主题。起初,我认为可能是意图代码导致了问题,但即使我在onclick侦听器中只有一个吐司,您仍然必须单击列表首选项,选择一个选项,然后再次单击首选项,再次选择该选项,以便它实际执行任何操作。如果你能帮忙,那就太好了。谢谢大家!
SettingsActivity,从preferences.xml获取首选项
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.support.v4.content.IntentCompat;
import android.widget.Toast;
import com.iliakplv.notes.R;
import com.iliakplv.notes.gui.main.LauncherActivity;
import com.iliakplv.notes.gui.main.MainActivity;
import java.util.List;
public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final int PREFERENCES = R.xml.preferences;
private final static String PREFS_LIST_OF_THEMES = "prefMaterialThemes";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//noinspection deprecation
addPreferencesFromResource(PREFERENCES);
PreferenceManager.setDefaultValues(this, PREFERENCES, false);
// These 3 lines are required for the onSharedPreferenceChanged method
Context context = getApplicationContext();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
settings.registerOnSharedPreferenceChangeListener(this);
// End
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
final CheckBoxPreference prefDarkThemeCB = (CheckBoxPreference) findPreference("prefDarkTheme");
prefDarkThemeCB.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
if (prefDarkThemeCB.isChecked()) {
// Restart Activity to apply Theme
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
} else if (prefDarkThemeCB.isChecked() == false) {
// Also restart activity to apply default theme
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
return false;
}
});
final ListPreference prefListThemes = (ListPreference) findPreference("prefMaterialThemes");
prefListThemes.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
Toast.makeText(getApplicationContext(), "toast", Toast.LENGTH_SHORT).show();
System.out.println("Toasted");
// Restart Activity to apply Theme
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
return false;
}
});
final CheckBoxPreference prefRequirePasswordCB = (CheckBoxPreference) findPreference("prefEnablePasswordLock");
prefRequirePasswordCB.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
if (prefRequirePasswordCB.isChecked()) {
EditTextPreference prefUsersPass = (EditTextPreference) findPreference("prefPassword");
// Check to see if the users password is empty before allowing them to check the require password box
if (prefUsersPass.getText().toString().isEmpty()) {
prefRequirePasswordCB.setChecked(false);
Toast.makeText(getApplicationContext(), R.string.password_required, Toast.LENGTH_SHORT).show();
//System.out.println("Empty password");
} else if (prefUsersPass.getText().toString().trim().length() == 0) {
// The above line checks to see if the User's pass is all spaces :p
prefRequirePasswordCB.setChecked(false);
Toast.makeText(getApplicationContext(), R.string.password_required, Toast.LENGTH_SHORT).show();
//System.out.println("Trimmed the password because they used all spaces probably and still empty");
} else {
Toast.makeText(getApplicationContext(), R.string.password_has_been_set, Toast.LENGTH_SHORT).show();
//System.out.println("kapow! they have a password and its: " + prefUsersPass.getText().toString());
}
}
return false;
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/security_pref_category">
<CheckBoxPreference
android:defaultValue="false"
android:key="prefEnablePasswordLock"
android:summary="@string/pref_require_password_for_app_description"
android:title="@string/pref_require_password_for_app" />
<EditTextPreference
android:defaultValue=""
android:key="prefPassword"
android:summary="@string/pref_password_description"
android:title="@string/pref_password_title" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/look_and_feel_pref_category">
<CheckBoxPreference
android:defaultValue="false"
android:key="prefDarkTheme"
android:summary="@string/pref_dark_theme_description"
android:title="@string/pref_dark_theme" />
<ListPreference
android:defaultValue="1"
android:entries="@array/pref_app_themes"
android:entryValues="@array/pref_app_theme_values"
android:key="prefMaterialThemes"
android:summary="@string/pref_change_theme_description"
android:title="@string/pref_change_theme" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/misc_pref_category">
<CheckBoxPreference
android:defaultValue="true"
android:key="linkify_note_text"
android:summary="@string/settings_linkify_subtitle"
android:title="@string/settings_linkify_title" />
<Preference
android:key="contactDevKey"
android:summary="@string/pref_contact_developer_description"
android:title="@string/pref_contact_developer">
<intent
android:action="android.intent.action.VIEW"
android:data="mailto:support@oxpheen.com?subject=Support *Notes">
<extra
android:name="android.intent.extra.TEXT"
android:value="" />
<!-- Value is whats in the body, blank for now -->
</intent>
</Preference>
</PreferenceCategory>
</PreferenceScreen>
Preferences.xml
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.support.v4.content.IntentCompat;
import android.widget.Toast;
import com.iliakplv.notes.R;
import com.iliakplv.notes.gui.main.LauncherActivity;
import com.iliakplv.notes.gui.main.MainActivity;
import java.util.List;
public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final int PREFERENCES = R.xml.preferences;
private final static String PREFS_LIST_OF_THEMES = "prefMaterialThemes";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//noinspection deprecation
addPreferencesFromResource(PREFERENCES);
PreferenceManager.setDefaultValues(this, PREFERENCES, false);
// These 3 lines are required for the onSharedPreferenceChanged method
Context context = getApplicationContext();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
settings.registerOnSharedPreferenceChangeListener(this);
// End
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
final CheckBoxPreference prefDarkThemeCB = (CheckBoxPreference) findPreference("prefDarkTheme");
prefDarkThemeCB.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
if (prefDarkThemeCB.isChecked()) {
// Restart Activity to apply Theme
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
} else if (prefDarkThemeCB.isChecked() == false) {
// Also restart activity to apply default theme
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
return false;
}
});
final ListPreference prefListThemes = (ListPreference) findPreference("prefMaterialThemes");
prefListThemes.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
Toast.makeText(getApplicationContext(), "toast", Toast.LENGTH_SHORT).show();
System.out.println("Toasted");
// Restart Activity to apply Theme
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
return false;
}
});
final CheckBoxPreference prefRequirePasswordCB = (CheckBoxPreference) findPreference("prefEnablePasswordLock");
prefRequirePasswordCB.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
if (prefRequirePasswordCB.isChecked()) {
EditTextPreference prefUsersPass = (EditTextPreference) findPreference("prefPassword");
// Check to see if the users password is empty before allowing them to check the require password box
if (prefUsersPass.getText().toString().isEmpty()) {
prefRequirePasswordCB.setChecked(false);
Toast.makeText(getApplicationContext(), R.string.password_required, Toast.LENGTH_SHORT).show();
//System.out.println("Empty password");
} else if (prefUsersPass.getText().toString().trim().length() == 0) {
// The above line checks to see if the User's pass is all spaces :p
prefRequirePasswordCB.setChecked(false);
Toast.makeText(getApplicationContext(), R.string.password_required, Toast.LENGTH_SHORT).show();
//System.out.println("Trimmed the password because they used all spaces probably and still empty");
} else {
Toast.makeText(getApplicationContext(), R.string.password_has_been_set, Toast.LENGTH_SHORT).show();
//System.out.println("kapow! they have a password and its: " + prefUsersPass.getText().toString());
}
}
return false;
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/security_pref_category">
<CheckBoxPreference
android:defaultValue="false"
android:key="prefEnablePasswordLock"
android:summary="@string/pref_require_password_for_app_description"
android:title="@string/pref_require_password_for_app" />
<EditTextPreference
android:defaultValue=""
android:key="prefPassword"
android:summary="@string/pref_password_description"
android:title="@string/pref_password_title" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/look_and_feel_pref_category">
<CheckBoxPreference
android:defaultValue="false"
android:key="prefDarkTheme"
android:summary="@string/pref_dark_theme_description"
android:title="@string/pref_dark_theme" />
<ListPreference
android:defaultValue="1"
android:entries="@array/pref_app_themes"
android:entryValues="@array/pref_app_theme_values"
android:key="prefMaterialThemes"
android:summary="@string/pref_change_theme_description"
android:title="@string/pref_change_theme" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/misc_pref_category">
<CheckBoxPreference
android:defaultValue="true"
android:key="linkify_note_text"
android:summary="@string/settings_linkify_subtitle"
android:title="@string/settings_linkify_title" />
<Preference
android:key="contactDevKey"
android:summary="@string/pref_contact_developer_description"
android:title="@string/pref_contact_developer">
<intent
android:action="android.intent.action.VIEW"
android:data="mailto:support@oxpheen.com?subject=Support *Notes">
<extra
android:name="android.intent.extra.TEXT"
android:value="" />
<!-- Value is whats in the body, blank for now -->
</intent>
</Preference>
</PreferenceCategory>
</PreferenceScreen>
Arrays.xml(在preference.xml文件中用于获取列表首选项的条目)
违约
黑暗的
阿莫德
红色
粉红色
紫色
深紫色
蓝色
浅蓝色
青色
水鸭
绿色
浅绿色
石灰
黄色的
琥珀色
橙色
深橙色
棕色的
灰色
蓝灰色
1.
2.
3.
4.
5.
6.
7.
8.
9
10
11
12
13
14
15
16
17
18
19
20
21
onSharedPreferenceChanged在首选项更改后被调用。当发生这种情况时,您可以设置OnPreferenceClickListener,一旦再次更改该值,它将被触发。这就是为什么只有在两次更改主题值时才会执行代码
将其放在onCreate中:
final ListPreference prefListThemes = (ListPreference) findPreference("prefMaterialThemes");
prefListThemes.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
Toast.makeText(getApplicationContext(), "toast", Toast.LENGTH_SHORT).show();
System.out.println("Toasted");
// Restart Activity to apply Theme
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
return true;
}
});
onSharedPreferenceChanged在首选项更改后调用。当发生这种情况时,您可以设置OnPreferenceClickListener,一旦再次更改该值,它将被触发。这就是为什么只有在两次更改主题值时才会执行代码 将其放在onCreate中:
final ListPreference prefListThemes = (ListPreference) findPreference("prefMaterialThemes");
prefListThemes.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
Toast.makeText(getApplicationContext(), "toast", Toast.LENGTH_SHORT).show();
System.out.println("Toasted");
// Restart Activity to apply Theme
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
return true;
}
});
onSharedPreferenceChanged在首选项更改后调用。当发生这种情况时,您可以设置OnPreferenceClickListener,一旦再次更改该值,它将被触发。这就是为什么只有在两次更改主题值时才会执行代码 将其放在onCreate中:
final ListPreference prefListThemes = (ListPreference) findPreference("prefMaterialThemes");
prefListThemes.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
Toast.makeText(getApplicationContext(), "toast", Toast.LENGTH_SHORT).show();
System.out.println("Toasted");
// Restart Activity to apply Theme
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
return true;
}
});
onSharedPreferenceChanged在首选项更改后调用。当发生这种情况时,您可以设置OnPreferenceClickListener,一旦再次更改该值,它将被触发。这就是为什么只有在两次更改主题值时才会执行代码 将其放在onCreate中:
final ListPreference prefListThemes = (ListPreference) findPreference("prefMaterialThemes");
prefListThemes.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
Toast.makeText(getApplicationContext(), "toast", Toast.LENGTH_SHORT).show();
System.out.println("Toasted");
// Restart Activity to apply Theme
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
return true;
}
});
实际上,您不需要将代码放入onCreate,只需将retune类型更改为true 正式文件:
True以使用新值更新首选项的状态。实际上,您不需要将代码放入onCreate中,只需将retune类型更改为True即可 正式文件:
True以使用新值更新首选项的状态。实际上,您不需要将代码放入onCreate中,只需将retune类型更改为True即可 正式文件:
True以使用新值更新首选项的状态。实际上,您不需要将代码放入onCreate中,只需将retune类型更改为True即可 正式文件:
如果为True,则使用新值更新首选项的状态。我也尝试过这样做。它做完全相同的事情。我必须选择两次才能让它工作。我相信你,因为我以前也用过它,但这是第一次发生在我身上。我所有的其他偏好都很好,除了这一个出于某种原因。深入研究每一寸代码,试图找到任何能让它做到这一点的东西。事实上,我只是在我的一个应用程序中测试了它,每当列表首选项发生变化时,它就会被调用(显示祝酒词)。你发布的代码肯定不起作用,所以请尝试我的代码,即使你以前有过。也许你的(略有)不同?如果它不起作用,请再次发布你的(新)代码以及填充列表首选项的代码(或xml,如果值来自于此),我尝试了你的代码,它也在这样做。我用我的preferenceactivity、preferences.xml和arrays.xml更新了这篇文章。我也试过这样做。它做完全相同的事情。我必须选择两次才能让它工作。我相信你,因为我以前也用过它,但这是第一次发生在我身上。我所有的其他偏好都很好,除了这一个出于某种原因。深入研究每一寸代码,试图找到任何能让它做到这一点的东西。事实上,我只是在我的一个应用程序中测试了它,每当列表首选项发生变化时,它就会被调用(显示祝酒词)。你发布的代码肯定不起作用,所以请尝试我的代码,即使你以前有过。也许你的(略有)不同?如果它不起作用,请再次发布你的(新)代码以及填充列表首选项的代码(或xml,如果值来自于此),我尝试了你的代码,它也在这样做。我用我的preferenceactivity、preferences.xml和arrays.xml更新了这篇文章。我也试过这样做。它做完全相同的事情。我必须选择两次才能让它工作。我相信你,因为我以前也用过它,但这是第一次发生在我身上。我所有的其他偏好都很好,除了这一个出于某种原因。深入研究每一寸代码,试图找到任何能让它做到这一点的东西。事实上,我只是在我的一个应用程序中测试了它,每当列表首选项发生变化时,它就会被调用(显示祝酒词)。你发布的代码肯定不起作用,所以请尝试我的代码,即使你以前有过。也许你的(稍微)不一样?如果它不起作用的话