如何在onCreateView()过程中获取"max“属性?如果我能让attrs.getAttributeCount()起作用,我的问题就解决了。
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/livewallpaper_settings"
android:key="livewallpaper_settings"
>
<com.example.myapp.SeekBarPreference1
android:persistent="true"
android:key="keyItems"
android:title="Items"
android:defaultValue="50"
android:max="200" />
<com.example.myapp.SeekBarPreference1
android:persistent="true"
android:key="keyItemsTwo"
android:title="Items Two"
android:defaultValue="5"
android:max="10" />
</PreferenceScreen> 这是一个简化的类。XmlPullParser不返回任何要解析的内容。
public final class SeekBarPreference1 extends Preference implements OnSeekBarChangeListener {
private static int _maxValue = 0;
private int _currentValue = 0;
private TextView _value;
private SharedPreferences _preferences;
public SeekBarPreference1(Context context, AttributeSet attrs) {
super(context, attrs);
_preferences = PreferenceManager.getDefaultSharedPreferences(context);
//
// These values are correct
//
Log.d("PREFS", " ");
Log.d("PREFS", "SeekBarPreference1");
Log.d("PREFS", "Key: " + getKey());
Log.d("PREFS", "AttrCount: " + attrs.getAttributeCount());
Log.d("PREFS", "####");
}
@Override
protected View onCreateView(ViewGroup parent) {
XmlPullParser attrs = parent.getResources().getXml(R.xml.livewallpaper_settings);
Log.d("PREFS", " ");
Log.d("PREFS", "onCreateView");
Log.d("PREFS", "Key: " + getKey()); // Correct key is output
Log.d("PREFS", "AttrCount: " + attrs.getAttributeCount()); // -1
Log.d("PREFS", "####");
}
}日志输出
SeekBarPreference1
Key: keyItems
AttrCount: 7
####
SeekBarPreference1
Key: keyItemsTwo
AttrCount: 7
####
onCreateView
Key: keyItems
AttrCount: -1
####
onCreateView
Key: keyItemsTwo
AttrCount: -1
####在我看来,如果我可以(从onCreateView)获得正确的键、标题、摘要等,那么应该有一种简单的方法来获得其他属性。
如果我尝试存储SeekBarPreference1()中的属性,那么一旦调用onCreateView(),这些属性就会丢失。
Summery:从当前的"this“onCreateView()中获取属性;
发布于 2010-12-21 10:52:15
我想通了。
public SeekBarPreference1(Context context, AttributeSet attrs) {
super(context, attrs);
_preferences = PreferenceManager.getDefaultSharedPreferences(context);
// Save your shared prefs here
// I saved the seekbar max && current value
// Something like below
for (int i = 0; i < attrs.getAttributeCount(); i++) {
if (attrs.getAttributeName(i).equals("max"))
_preferences.edit().putString(getKey() +"Max", ""+
attrs.getAttributeValue(i)).commit();
}
}然后检索数据onCreateView()
protected View onCreateView(ViewGroup parent) {
// Retrieve the data
// Now you will always have the correct data
_maxValue = Integer.parseInt(_preferences.getString(getKey() +"Max", ""));
}现在我可以多次使用同一个类了,终于...
https://stackoverflow.com/questions/4491494
复制相似问题