我需要从PreferenceFragment到SplashActivity获取值。我该怎么做?
public class SettingsFragment extends PreferenceFragment {
public String cityToLoad;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
ListPreference city = (ListPreference) findPreference("city");
city.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
cityToLoad = newValue.toString();
return true;
}
});
}}
我的preferences.xml只包含ListPreference。
列表首选项中没有onchange事件。了解更多http://developer.android.com/reference/android/preference/PreferenceFragment.html
如果您只是使用preference标签,那么您可以尝试下面的代码。
city.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference arg0) {
Intent newScreen = new Intent(getActivity(), SecondActivity.class);
newScreen.putExtra("Data", data);
startActivity(newScreen);
return true;
}
});