- Documents
- Preference Package
http://developer.android.com/reference/android/preference/package-summary.html - Preference xml File Sample
http://developer.android.com/resources/samples/ApiDemos/res/xml/preferences.html
- xxx
- Preference Class
- PreferenceActivity
In general, we use preference in the subclass of PreferenceActivity, and call addPreferencesFromResource to load layout file. If you want to use preference in the subclass of Activity, you must call PreferenceManager.setDefaultValues to load layout file.
- PreferenceManager
If you want to create preference in code, or if you want to use preference in the subclass of Activity, or if you want to get the object of SharedPreferences, you can use this class.
- PreferenceGroup
This is the base class of PreferenceScreen and PreferenceCategory, it is rarely used directly.
- PreferenceScreen
Represent a preference window/screen - PreferenceCategory
Represent a group in the window/screen
- Preference
It is the base class of all preference elements. If you want to customize preference's behavior (such as to pop up yourown screen when the preference is clicked), you can use this class, and register the listener of type Preference.OnPreferenceClickListener, then decide what to do in the methods of Preference.OnPreferenceClickListener.- DialogPreference
Must create a derived class of DialogPreference, and then use the derived clas, as
- CheckBoxPreference
- EditTextPreference
- ListPreference
- On Android 1.6 and previous version, ListPreference has no icon, if you want to show icon on the right of the preference as that on Android 2.0 and later, just replace ListPreference with PreferenceScreen, and process the proper events.
- RingtonePreference
- xxx
- Usage
- Define preference layout in res/xml/ (you can also create preference elements in your code)
- Define subclass of PreferenceActivity, in the method onCreate, call addPreferencesFromResource(R.xml.xxx) to load layout resource file
- Get preference element handle by invoking the method findPreference
- SharedPreferences
You can call getSharedPreferences() in subclass of Context to get shared preference handle, then to get/set preferences.
- Customize Preference
- Define layout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
android:gravity="right" />
android:singleLine="true"
android:password="true"
android:gravity="left" />
android:gravity="right" />
android:singleLine="true"
android:password="true"
android:gravity="left" />
- Define class
public class ChangePasswdDialogPreferenceextends DialogPreference
{
private EditText mNewPasswdView;
private EditText mNewPasswdConfirmView;
public PhoneRebindDialogPreference(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public PhoneRebindDialogPreference(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
@Override
protected View onCreateDialogView()
{
View view = super.onCreateDialogView();
mNewPasswdView = (EditText)view.findViewById(R.id.new_password);
mNewPasswdConfirmView = (EditText)view.findViewById(R.id.new_passwd_confirm);
return view;
}
@Override
protected void showDialog(Bundle state)
{
super.showDialog(state);
Button button;
button = ((AlertDialog)getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
xxxx
}
});
}
//Save & restore state
@Override
protected Parcelable onSaveInstanceState()
{
final Parcelable superState = super.onSaveInstanceState();
if (isPersistent())
{
// No need to save instance state since it's persistent
return superState;
}
final SavedState myState = new SavedState(superState);
myState.mNewPasswd = mNewPasswdView.getText().toString();
myState.mNewPasswdConfirm = mNewPasswdConfirmView.getText().toString();
return myState;
}
@Override
protected void onRestoreInstanceState(Parcelable state)
{
if (state == null || !state.getClass().equals(SavedState.class))
{
// Didn't save state for us in onSaveInstanceState
super.onRestoreInstanceState(state);
return;
}
SavedState myState = (SavedState) state;
super.onRestoreInstanceState(myState.getSuperState());
mNewPasswd.setText(myState.mNewPasswd);
mNewPasswdConfirmView.setText(myState.mNewPasswdConfirm);
}
private static class SavedState extends BaseSavedState
{
String mNewPasswd;
String mNewPasswdConfirm;
public SavedState(Parcel source)
{
super(source);
mNewPasswd = source.readString();
mNewPasswdConfirm = source.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags)
{
super.writeToParcel(dest, flags);
dest.writeString(mNewPasswd);
dest.writeString(mNewPasswdConfirm );
}
public SavedState(Parcelable superState)
{
super(superState);
}
public static final Parcelable.Creator CREATOR =
new Parcelable.Creator()
{
public SavedState createFromParcel(Parcel in)
{
return new SavedState(in);
}
public SavedState[] newArray(int size)
{
return new SavedState[size];
}
};
}
- Use customized preference
android:key="change_contents"
android:title="@string/pref_title_change_contents"
android:dialogLayout="@layout/my_dialog"
android:summary="" />
- xx
- xxx
阅读(1409) | 评论(0) | 转发(0) |