- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个很奇怪的问题。当我在我的设置- Activity 中滚动时,一些复选框在不再显示时会发生变化。例如:我取消选中广告复选框,然后向下滚动,当复选框完全消失时,复选框会重置。只有当我取消选中默认值没有任何影响的复选框时才会发生这种情况。
我的 Activity :
public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
/**
* Determines whether to always show the simplified settings UI, where
* settings are presented in a single list. When false, settings are shown
* as a master/detail two-pane view on tablets. When true, a single pane is
* shown on tablets.
*/
private static final boolean ALWAYS_SIMPLE_PREFS = false;
private SharedPreferences prefs;
private Tracker mTracker;
private void analytics() {
if(prefs.getBoolean(Tags.PREF_GOOGLEANALYTICS, true)) {
// Obtain the shared Tracker instance.
AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();
mTracker.setScreenName("SettingsActivity");
mTracker.send(new HitBuilders.ScreenViewBuilder().build());
}
}
private void analytics(String category, String action) {
if(prefs.getBoolean(Tags.PREF_GOOGLEANALYTICS, true)) {
if (mTracker == null) {
AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();
}
mTracker.send(new HitBuilders.EventBuilder()
.setCategory(category)
.setAction(action)
.build());
}
}
private void setLocale(String lang) {
/*Locale myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);*/
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);
finish();
}
@Override
protected void onResume() {
super.onResume();
prefs.registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onPause() {
super.onPause();
prefs.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
analytics();
Toolbar bar;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
LinearLayout root = (LinearLayout) findViewById(android.R.id.list).getParent().getParent().getParent();
bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
root.addView(bar, 0); // insert at top
} else {
ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
ListView content = (ListView) root.getChildAt(0);
root.removeAllViews();
bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
int height;
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
height = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
}else{
height = bar.getHeight();
}
content.setPadding(0, height, 0, 0);
root.addView(content);
root.addView(bar);
}
bar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
setupSimplePreferencesScreen();
}
/**
* Shows the simplified settings UI if the device configuration if the
* device configuration dictates that a simplified, single-pane UI should be
* shown.
*/
private void setupSimplePreferencesScreen() {
if (!isSimplePreferences(this)) {
return;
}
// In the simplified UI, fragments are not used at all and we instead
// use the older PreferenceActivity APIs.
// Add 'general' preferences.
addPreferencesFromResource(R.xml.pref_general);
// Add 'notifications' preferences, and a corresponding header.
PreferenceCategory fakeHeader = new PreferenceCategory(this);
fakeHeader.setTitle(getResources().getString(R.string.pref_notifications));
getPreferenceScreen().addPreference(fakeHeader);
addPreferencesFromResource(R.xml.pref_notification);
PreferenceCategory fakeHeaderAudio = new PreferenceCategory(this);
fakeHeaderAudio.setTitle(getResources().getString(R.string.pref_audio));
getPreferenceScreen().addPreference(fakeHeaderAudio);
addPreferencesFromResource(R.xml.pref_audio);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences to
// their values. When their values change, their summaries are updated
// to reflect the new value, per the Android Design guidelines.
// bindPreferenceSummaryToValue(findPreference(getString(R.string.key_checkbox)));
}
/**
* {@inheritDoc}
*/
@Override
public boolean onIsMultiPane() {
return isXLargeTablet(this) && !isSimplePreferences(this);
}
/**
* Helper method to determine if the device has an extra-large screen. For
* example, 10" tablets are extra-large.
*/
private static boolean isXLargeTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
}
/**
* Determines whether the simplified settings UI should be shown. This is
* true if this is forced via {@link #ALWAYS_SIMPLE_PREFS}, or the device
* doesn't have newer APIs like {@link PreferenceFragment}, or the device
* doesn't have an extra-large screen. In these cases, a single-pane
* "simplified" settings UI should be shown.
*/
private static boolean isSimplePreferences(Context context) {
return ALWAYS_SIMPLE_PREFS
|| (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
|| !isXLargeTablet(context);
}
/**
* {@inheritDoc}
*/
@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onBuildHeaders(List<Header> target) {
if (!isSimplePreferences(this)) {
loadHeadersFromResource(R.xml.pref_headers, target);
}
}
@Override
protected boolean isValidFragment(String fragmentName) {
return NotificationPreferenceFragment.class.getName().equals(fragmentName)
|| GeneralPreferenceFragment.class.getName().equals(fragmentName)
|| AudioPreferenceFragment.class.getName().equals(fragmentName);
}
@Override
public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
if(key.equals(Tags.PREF_NOTIFICATION)
|| key.equals(Tags.PREF_NOTIFICATIONTIME)
|| key.equals(Tags.PREF_AUDIO_AUTODOWNLOAD)) {
if(sharedPreferences.getBoolean(Tags.PREF_NOTIFICATION, true)
|| sharedPreferences.getBoolean(Tags.PREF_AUDIO_AUTODOWNLOAD, true)) {
long time = sharedPreferences.getLong(Tags.PREF_NOTIFICATIONTIME, 60 * 7);
Notifications.setNotifications(this, time * 60 * 1000);
} else
Notifications.removeNotifications(this);
}
if(key.equals(Tags.PREF_NOTIFICATION) || key.equals(Tags.PREF_ADS)
|| key.equals(Tags.PREF_GOOGLEANALYTICS) || key.equals(Tags.PREF_SHOWNOTES)) {
//This toast is shown when I scroll down
Toast.makeText( getApplicationContext(), "Notification || Analytics", Toast.LENGTH_SHORT).show();
analytics("Settings", "Settings: " + key + ", " + sharedPreferences.getBoolean(key, true));
}
//Language change
if(key.equals(Tags.PREF_LANGUAGE)) {
try {
setLocale(sharedPreferences.getString(key, "en"));
} catch (Exception e) {
Log.e("Losungen", "Error changing language: " + e.getMessage());
e.printStackTrace();
}
}
//SD-Card change
if(key.equals(Tags.PREF_AUDIO_EXTERNAL_STORGAE)) {
boolean sd_card = sharedPreferences.getBoolean(key, false);
if(sd_card)
MainActivity.toast(this, this.getString(R.string.still_in_internal), Toast.LENGTH_LONG);
else
MainActivity.toast(this, this.getString(R.string.still_in_external), Toast.LENGTH_LONG);
}
}
/**
* This fragment shows general preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class GeneralPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
}
}
/**
* This fragment shows notification preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class NotificationPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_notification);
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class AudioPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_audio);
}
}
}
还有我的 xml 文件:pref_general.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:key="show_notes"
android:title="@string/pref_notes_title"
android:defaultValue="true" />
<SwitchPreference
android:key="show_ads"
android:title="@string/pref_ads_title"
android:summary="@string/pref_ads_summary"
android:defaultValue="false"
/>
<SwitchPreference
android:key="google_analytics"
android:title="@string/pref_google_title"
android:summary="@string/pref_google_summary"
android:defaultValue="true" />
<ListPreference
android:key="language"
android:title="@string/pref_language"
android:summary="@string/pref_language_summary"
android:entries="@array/pref_language_list_titles"
android:entryValues="@array/pref_language_list_values"
android:negativeButtonText="@null"
android:positiveButtonText="@null" />
</PreferenceScreen>
pref_notification.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- A 'parent' preference, which enables/disables child preferences (below)
when checked/unchecked. -->
<SwitchPreference
android:key="notifications_losung"
android:title="@string/pref_notifications"
android:defaultValue="true" />
<ListPreference
android:key="notifications_art"
android:dependency="notifications_losung"
android:title="@string/pref_notification_art"
android:defaultValue="0"
android:entries="@array/pref_notifications_list_titles"
android:entryValues="@array/pref_notifications_list_values"
android:negativeButtonText="@null"
android:positiveButtonText="@null" />
<de.schalter.losungen.preferences.TimePreference
android:key="notification_time"
android:defaultValue="420"
android:title="@string/pref_time"
android:dependency="notifications_losung"
/>
</PreferenceScreen>
pref_audio.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- oder stream -->
<SwitchPreference
android:key="audio_download"
android:title="@string/pref_audio_download_title"
android:summaryOn="@string/pref_audio_download_summary_on"
android:summaryOff="@string/pref_audio_download_summary_off"
android:defaultValue="true" />
<!-- oder erst bei Nutzer interaktion -->
<SwitchPreference
android:key="audio_autodownload"
android:dependency="audio_download"
android:title="@string/pref_audio_autodownload_title"
android:summaryOn="@string/pref_audio_autodownload_summary_on"
android:summaryOff="@string/pref_audio_autodownload_summary_off"
android:defaultValue="true" />
<ListPreference
android:key="audio_autodownload_network"
android:dependency="audio_autodownload"
android:title="@string/pref_audio_autodownload_network_title"
android:defaultValue="0"
android:entries="@array/pref_audio_autodownload_network_entries"
android:entryValues="@array/pref_audio_autodownload_network_values"
android:negativeButtonText="@null"
android:positiveButtonText="@null" />
<SwitchPreference
android:key="audio_external_storage"
android:dependency="audio_download"
android:title="@string/pref_audio_externalstorage_title"
android:summaryOff="@string/pref_audio_externalstorage_summary_off"
android:summaryOn="@string/pref_audio_externalstorage_summary_on"
android:defaultValue="false" />
</PreferenceScreen>
我不知道为什么会这样。有些评论是德语的,但我想你什么都能听懂。有什么想法吗?
这是一个类似的问题,但没有有用的答案:Android Preferences - On/Off Switch Icons Reset When Scrolled Offscreen
最佳答案
引用Preference items being automatically re-set?上的回答,它建议您仅使用调用 super 的构造函数对 SwitchPreference 进行子类化,然后使用它代替标准 SwitchPreference。我注意到这发生在早期版本的 Android 上(在我们的例子中特别是 4.x),但不是 5+。
我怀疑这是因为 Preference 的构造函数中的这些行:
来自 API 17(需要此修改):
if (!getClass().getName().startsWith("android.preference")) {
// For subclasses not in this package, assume the worst and don't cache views
mHasSpecifiedLayout = true;
}
或来自 API 23(无需修改即可正常工作):
if (!getClass().getName().startsWith("android.preference")
&& !getClass().getName().startsWith("com.android")) {
// For non-framework subclasses, assume the worst and don't cache views.
mCanRecycleLayout = false;
}
通过 PreferenceGroupAdapter 进行跟踪,看起来当它回收 View 时它会设置一些东西,然后通知监听器首选项已更改。我找不到它实际上在哪里/为什么更改了首选项,所以我也不确定为什么更新的 API 中的更改修复了它,但这似乎就是正在发生的事情。
关于Android 偏好 - 在 SettingsActivity 中滚动到屏幕外时 SwtichPrefrence 发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35140581/
Nooby 在这里再次提问。我做了很多研究以在抽屉导航中添加“设置”行以转到我的 SettingsActivity。但未能获得良好的工作解决方案。有人请帮帮我。 private void se
这是我的第二个问题(已经回答):Creating SharedPreferences object inside Fragment . 在弄清楚如何在静态 Fragment 类中创建 SharedPr
我有一个 SettingsActivity,它的顶部栏上有一个“向上”按钮。我的 AettingsActivity 也有 header ,可以加载正确的设置 fragment 。默认情况下,当我按下顶
我的 SettingsActivity 有子偏好屏幕。我想直接从另一个 Activity 打开 SettingsActivity 中嵌套的 PreferenceScreen 之一单击按钮。 基本上,我
我使用 Eclipse 创建了一个设置 Activity 。设置 Activity 会自动创建两个示例类别(除常规设置外):通知和数据与同步。问题是我根本不知道如何在不让我的应用程序崩溃的情况下删除它
这个问题在这里已经有了答案: How to add Action Bar from support library into PreferenceActivity? (8 个答案) 关闭 9 年前。
我正在使用 Preferences-API 创建我的设置屏幕。 截至目前,我的 SettingsActivity 布局 XML 包含一个简单的 LinearLayout,里面有一个 FrameLayo
我正在尝试通过在主菜单中添加电池百分比来修改氰基模组设置 apk,但由于以下错误,我无法这样做:“方法 findPreference(string)”未定义” 我听说你只能从首选项 fragment
我有一个 AppCompatPreferenceActivity.java。但是它显示了这个错误 公共(public)类 SettingsActivity 扩展 AppCompatPreference
我希望用户能够更改应用程序语言(字符串位于 strings-de/strings.xml 或 strings-zh/strings.xml 等文件夹中)。当我将电话设置更改为中文或其他设置时它工作正常
我在主 Activity (称为 ScoreboardActivity)中定义了一些 SharedPreferences。这些值肯定要么被检索,要么正确的默认值正在工作。但是,我现在尝试设置一个 Se
TL;DR:我应该如何将 SettingsActivity 的背景更改为白色或灰白色以匹配 Holo.Light?更改应用程序的主题会更改文本,但会保持黑色背景。 我最近将应用程序的主题从默认 (Ho
我正在 eclipse 上开发 android 启动器,该包包含 MainActivity(启动器本身)和 SettingsActivity。 我的问题是,如果我打开设置页面,转到其他地方,然后单击主
我有一个很奇怪的问题。当我在我的设置- Activity 中滚动时,一些复选框在不再显示时会发生变化。例如:我取消选中广告复选框,然后向下滚动,当复选框完全消失时,复选框会重置。只有当我取消选中默认值
package com.example.mayank.sunshine; import android.os.Bundle; import android.preference.ListPrefere
我是一名优秀的程序员,十分优秀!