问题描述
我正在尝试在 Android 应用中实施基本设置活动,但出现空白屏幕或崩溃.我看到的文档和示例没有帮助,因为它们要么陈旧要么不一致.例如,根据您查看的位置,设置活动应该扩展 Activity、PreferenceActivity 或 AppCompatPreferenceActivity(文件的一部分>新建>活动>设置活动).
I'm trying to implement a basic settings activity in an Android app and either get a blank white screen or a crash. The documentation and samples I've seen aren't helping because they're either old or inconsistent. For example, depending on where you look, the settings activity should either extend Activity, PreferenceActivity, or AppCompatPreferenceActivity (part of the File>New>Activity>Settings Activity).
developer.android.com 说您应该实现以下内容:
developer.android.com says you should implement the following:
public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
}
然而,Android Studio 中生成的 Settings Activity 不会对其创建的三个片段中的任何一个进行此调用.它使用首选项标头.
Yet, the Settings Activity generated in Android Studio uses does not make this call for any of the three fragments it creates. It uses preference headers.
这里是我的问题:
- 如果您使用的是带有单个 PreferenceFragment 的简单的单个首选项.xml 文件,并且不需要 API 19 之前的兼容性,那么 SettingsActivity 应该扩展哪个类?Activity、PreferenceActivity 或 AppCompatPreferenceActivity(对于其所有支持方法和委托)?
- 是否需要在 SettingsActivity.onCreate() 中调用
getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit()
? - 通过各种组合,我要么得到一个没有操作栏的空白设置屏幕,要么崩溃.在显示应用操作栏的 Activity 中设置单个 PreferencesFragment 的正确方法是什么?
推荐答案
对我有用的是扩展 AppCompatActivity
.
static final String ANIMATION = "animation" ;
static final String COUNTDOWN_ON_OFF = "countdown_on_off";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (getFragmentManager().findFragmentById(android.R.id.content) == null)
{
getFragmentManager().beginTransaction().add(android.R.id.content, new Prefs()).commit();
}
}
我删除了所有与首选项头相关的生成代码,并为我的 PreferenceFragment
I kicked out all the generated code related to preference headers and kept some template methods/ variables (which Android Studio generated in some earlier version) for my PreferenceFragment
public static class Prefs extends PreferenceFragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
// 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.
// findPreference() uses android:key like in preferences.xml !
bindPreferenceSummaryToValue(findPreference(ANIMATION));
}
}
我的 Activity
类中的一个静态方法(改编自模板).您可能需要检查其他偏好类型:
A static method in my Activity
class (adapted from the template). You may want to check for other preference types:
/**
* Binds a preference's summary to its value. More specifically, when the
* preference's value is changed, its summary (line of text below the
* preference title) is updated to reflect the value. The summary is also
* immediately updated upon calling this method. The exact display format is
* dependent on the type of preference.
*
* @see #sBindPreferenceSummaryToValueListener
*/
private static void bindPreferenceSummaryToValue(Preference preference)
{
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
// Trigger the listener immediately with the preference's
// current value.
if (preference instanceof CheckBoxPreference)
{
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getBoolean(preference.getKey(), true));
}
else
{
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
}
最后,将 Preference.OnPreferenceChangeListener
作为 Activity
中的静态变量(也改编自模板):
And finally, the Preference.OnPreferenceChangeListener
as static variable in the Activity
(also adapted from the template):
/**
* A preference value change listener that updates the preference's summary
* to reflect its new value.<br>
* template by Android Studio minus Ringtone 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);
}
else if (preference instanceof RingtonePreference)
{
// my app didn't need that
return true;
}
else if (preference instanceof CheckBoxPreference)
{
Context ctx = preference.getContext();
boolean isChecked = (Boolean) value;
if (preference.getKey().equals(ANIMATION))
{
preference.setSummary(isChecked ? ctx.getString(R.string.sOn) : ctx.getString(R.string.sOff));
}
else if (preference.getKey().equals(COUNTDOWN_ON_OFF))
{
preference.setSummary(isChecked ? ctx.getString(R.string.sWhenPaused) : ctx.getString(R.string.sNever) );
}
}
else
{
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
};
}
这篇关于设置 Android PreferenceFragment 的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!