我正在学习如何在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();
}
}
Run Code Online (Sandbox Code Playgroud)
和我的res/preferences.xml档案
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="BTA"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="editTextPref"
android:title="Numero de telephone"
android:summary="This allows you to enter a string"
android:defaultValue="*"/>
</PreferenceCategory>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)
所以现在我有设置活动的活动.但我想显示EditTextPref的值android:summary.我找到了很多主题,但所有功能都已弃用.
编辑:感谢@Ace_McIntosh,我为想要它的人编辑了我的代码,它现在正在运行.
Rob*_*nde 18
如果您使用的是AndroidX 首选项库,则可以使用属性app:useSimpleSummaryProvider。例如:
<EditTextPreference
app:key="edittext"
app:title="@string/title_edittext_preference"
app:useSimpleSummaryProvider="true"
app:dialogTitle="@string/dialog_title_edittext_preference"/>
Run Code Online (Sandbox Code Playgroud)
您可以在此处阅读完整的示例。
只是覆盖getSummary的EditTextPreference,那么你会得到与显示总结其价值EditTextPreference.
public class EditSummaryPreference extends EditTextPreference {
...// omit constructor
@Override
public CharSequence getSummary() {
return getText();
}
}
Run Code Online (Sandbox Code Playgroud)
只需在所需的 Preference 对象上使用setSummary 方法。在为您希望更新的每个条目(即,EditTextPreference您案例中的所有条目)恢复设置片段时调用它,并OnSharedPreferenceChangeListener在具体SharedPreferences对象上注册一个(以便您可以在更改摘要时更新) - 并传递它所需的 EditTextPreference 值(您可以通过其getText()方法获得)。
MyPreferenceFragment像这样实现它(我不保证它会立即起作用,它的目的只是给你一个想法):
public class MyPreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
SharedPreferences sharedPreferences;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// load the preferences from your XML resource (which I assume you already do anyway)
addPreferencesFromResource(R.xml.preferences);
}
@Override
public void onResume() {
super.onResume();
sharedPreferences = getPreferenceManager().getSharedPreferences();
// we want to watch the preference values' changes
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
Map<String, ?> preferencesMap = sharedPreferences.getAll();
// iterate through the preference entries and update their summary if they are an instance of EditTextPreference
for (Map.Entry<String, ?> preferenceEntry : preferencesMap.entrySet()) {
if (preferenceEntry instanceof EditTextPreference) {
updateSummary((EditTextPreference) preferenceEntry);
}
}
}
@Override
public void onPause() {
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
Map<String, ?> preferencesMap = sharedPreferences.getAll();
// get the preference that has been changed
Object changedPreference = preferencesMap.get(key);
// and if it's an instance of EditTextPreference class, update its summary
if (preferencesMap.get(key) instanceof EditTextPreference) {
updateSummary((EditTextPreference) changedPreference);
}
}
private void updateSummary(EditTextPreference preference) {
// set the EditTextPreference's summary value to its current text
preference.setSummary(preference.getText());
}
}
Run Code Online (Sandbox Code Playgroud)
(在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
}
}
Run Code Online (Sandbox Code Playgroud)
然后可以这样使用:
<<您的包裹名称>.SummarisedEditTextPreference/>
小智 5
在androidx 库中, EditTextPreference 有app:useSimpleSummaryProvider属性。使用该属性,您无需扩展任何类或监听 SharedPreferences 上的任何更改。您可以在https://github.com/googlesamples/android-preferences/blob/master/app/src/main/res/xml/dialogs.xml查看示例代码
| 归档时间: |
|
| 查看次数: |
12056 次 |
| 最近记录: |