UI preference摘要字段设置为的preference价值字段、设置为、摘要、价值

2023-09-06 00:34:13 作者:谁能与我同醉

新到Android,我有一些code当用户更改preference我更新摘要字段在UI preference是他们输入的值。但是,创建了preference活动时,我想设置摘要领域是在相应的preferences值。 请指教。谢谢。

 公共类我的preferenceActivity扩展preferenceActivity工具
        OnShared preferenceChangeListener {

    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        加preferencesFromResource(R.xml preference。);
        。共享preferences SP = GET preferenceScreen()getShared preferences();
        sp.registerOnShared preferenceChangeListener(本);
    }

    公共无效onShared preferenceChanged(共享preferences共享preferences,
            字符串键){
        preference preF =找到preference(键);
        如果(preF的instanceof的EditText preference){
            的EditText preference ETP =(EditText上preference)preF;
            pref.setSummary(etp.getText());
        }
    }
}
 

解决方案

我是新太,可能不是最好的code,但这种类似于我在做什么。你可能想注册,您监听onResume和注销的onPause它虽然而非的onCreate。我希望这有助于。

主要是你只需要抓住了preF,在preF值,并设置总结。

 公共类我的preferenceActivity扩展preferenceActivity工具
        OnShared preferenceChangeListener {

    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        加preferencesFromResource(R.xml preference。);
        。共享preferences SP = GET preferenceScreen()getShared preferences();
        的EditText preference EDITTEXT preF =(EditText上preference)找到preference(下称prefKey);
        EDITTEXT preF
                .setSummary(sp.getString(下称prefKey,一些默认的文本));
    }

    保护无效onResume(){
        super.onResume();
        GET preferenceScreen()。getShared preferences()
                .registerOnShared preferenceChangeListener(本);
    }

    保护无效的onPause(){
        super.onPause();
        GET preferenceScreen()。getShared preferences()
                .unregisterOnShared preferenceChangeListener(本);
    }

    公共无效onShared preferenceChanged(共享preferences共享preferences,
            字符串键){
        preference preF =找到preference(键);
        如果(preF的instanceof的EditText preference){
            的EditText preference ETP =(EditText上preference)preF;
            pref.setSummary(etp.getText());
        }
    }
}
 

将 性别 字段值的输入设置为 男 , 女 列表选择.在access设计表时中怎样操作

New to Android, I have some code when the user changes a preference I update the Summary field in the UI preference to be the value they entered. However, when the preference activity is created I'd like to set the Summary fields to be the values in the corresponding preferences. Please advise. Thanks.

public class MyPreferenceActivity extends PreferenceActivity implements
        OnSharedPreferenceChangeListener {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preference);
        SharedPreferences sp = getPreferenceScreen().getSharedPreferences();
        sp.registerOnSharedPreferenceChangeListener(this);
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key) {
        Preference pref = findPreference(key);
        if (pref instanceof EditTextPreference) {
            EditTextPreference etp = (EditTextPreference) pref;
            pref.setSummary(etp.getText());
        }
    }
}

解决方案

I am new too so may not be the best code but this is similar to what I am doing. You probably want to register you listener onResume and unregister it onPause though rather than onCreate. I hope this helps.

Mainly you just need to grab the pref, the pref value and set the summary.

public class MyPreferenceActivity extends PreferenceActivity implements
        OnSharedPreferenceChangeListener {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preference);
        SharedPreferences sp = getPreferenceScreen().getSharedPreferences();
        EditTextPreference editTextPref = (EditTextPreference) findPreference("thePrefKey");
        editTextPref
                .setSummary(sp.getString("thePrefKey", "Some Default Text"));
    }

    protected void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences()
                .registerOnSharedPreferenceChangeListener(this);
    }

    protected void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences()
                .unregisterOnSharedPreferenceChangeListener(this);
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key) {
        Preference pref = findPreference(key);
        if (pref instanceof EditTextPreference) {
            EditTextPreference etp = (EditTextPreference) pref;
            pref.setSummary(etp.getText());
        }
    }
}

 
精彩推荐