我正在学习如何在Android中开发并想要进行设置活动,
我的设置活动
public class Main extends Activity { protected SettingsFragment settingsFragment; @SuppressLint("NewApi") @TargetApi(11) public class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); setSummaries(); } @Override public void onResume() { final SharedPreferences sh = getPreferenceManager().getSharedPreferences() ; super.onResume(); sh.registerOnSharedPreferenceChangeListener(this); } @Override public void onPause() { final SharedPreferences sh = getPreferenceManager().getSharedPreferences() ; super.onPause(); sh.unregisterOnSharedPreferenceChangeListener(this); } @SuppressLint("NewApi") public void setSummaries(){ final SharedPreferences sh = getPreferenceManager().getSharedPreferences() ; //Pref1 Preference stylePref = findPreference("editTextPref"); stylePref.setSummary(sh.getString("editTextPref", "")); //here the other preferences.. } @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (key.equals("editTextPref")) { Preference pref = settingsFragment.findPreference(key); // Set summary to be the user-description for the selected value pref.setSummary(sharedPreferences.getString(key, "")); } //here the others preferences } }//End fragment @SuppressLint("NewApi") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); settingsFragment = new SettingsFragment(); getFragmentManager().beginTransaction() .replace(android.R.id.content, settingsFragment) .commit(); } }
和我的res/preferences.xml
档案
所以现在我有设置活动的活动.但我想显示EditTextPref的值android:summary
.我找到了很多主题,但所有功能都已弃用.
编辑:感谢@Ace_McIntosh,我为想要它的人编辑了我的代码,它现在正在运行.
只是覆盖getSummary
的EditTextPreference
,那么你会得到与显示总结其价值EditTextPreference.
public class EditSummaryPreference extends EditTextPreference { ...// omit constructor @Override public CharSequence getSummary() { return getText(); } }
(在Kotlin中)我希望做一些更简单的事情,只是创建一个扩展EditTextPreference的类:
import android.content.Context import android.support.v7.preference.EditTextPreference import android.util.AttributeSet /** * This class was created by Anthony M Cannon on 17/04/2018. */ class SummarisedEditTextPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : EditTextPreference(context, attrs) { private var mOnChangeListener: OnPreferenceChangeListener? = null init { super.setOnPreferenceChangeListener { preference, newValue -> summary = newValue as String mOnChangeListener?.onPreferenceChange(preference, newValue) ?: true } } /** * Called when this Preference has been attached to a Preference hierarchy. * Make sure to call the super implementation. * * @param preferenceManager The PreferenceManager of the hierarchy. */ override fun onAttachedToHierarchy(preferenceManager: PreferenceManager) { super.onAttachedToHierarchy(preferenceManager) summary = sharedPreferences.getString(key, null) } /** * Sets the callback to be invoked when this Preference is changed by the * user (but before the internal state has been updated). * * @param onPreferenceChangeListener The callback to be invoked. */ override fun setOnPreferenceChangeListener( onPreferenceChangeListener: OnPreferenceChangeListener) { mOnChangeListener = onPreferenceChangeListener } }
然后可以这样使用:
<
<您的包裹名称>.SummarisedEditTextPreference/>