1

如何找到由框架创建的 PreferenceActivity 实例?

* * *

我的第一堂课。

/** This is MyAppSettings class, The instance of this class will be created by a framework **/

public class MyAppSettings extends PreferenceActivity {
    protected MySeekBarPreference mSeekBarPref;

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        addPreferencesFromResource(R.xml.prefs);

        mSeekBarPref = (MySeekBarPreference)super.findPreference("my_key");
    }
}

* * *

我的第二节课。

public class MySeekBarPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener {

      // in whis code I need to get the instance of the MyAppSettings, which was
      // created by Android framework. (I paste a hypothetical func getTheInstanceOfMyAppSettings() :))
  private MyAppSettings mAppSettings = getTheInstanceOfMyAppSettings(); 

我可以通过身份证或其他方式找到它吗?

4

1 回答 1

0

我猜你正在寻找getPreferenceScreen()

您可以使用它来修改PreferenceScreenxml 中定义的内容。

例如:

private void disableHardKeyboardOptions() {
    Preference pref = getPreferenceScreen().findPreference("pref_hardkeyboard_option");
    pref.setEnabled(false);
    pref.setSummary(custom.getString(R.string.pref_no_hardkeyboard));
}
于 2010-07-11T14:00:05.140 回答