Because of customer demand spinnerpreference, the online search is not, but can only rewrite components, now the process to share everyone.
CustomSpinnerpreferenceOne: First from the expansionPreferencestart: Class files must inherit fromPreferenceand implement the constructor, as follows
Public Myspinnerpreference (Context context) { super (context); } Public Myspinnerpreference (context context, AttributeSet Attrs) { Super (context, attrs); } Public Myspinnerpreference (context context, AttributeSet attrs, int defstyle) { Super (context, attrs, Defstyle); }
Two: Rewriting of custom layout files
<?xml version= "1.0" encoding= "Utf-8"?><!--Layout of a header item in preferenceactivity. --><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent "android:layout_height=" wrap_content "android:background="? Android:attr/activatedbackgroundindicator "Android Oid:gravity= "center_vertical" android:minheight= "48DP" android:paddingend= "Android:attr/scrollbarsize" android:p addingstart= "@dimen/mypreference_margin_start" > <relativelayout android:layout_width= "0DP" Android : layout_height= "wrap_content" android:layout_marginbottom= "6dip" android:layout_marginend= "6dip" Android Oid:layout_marginstart= "2dip" android:layout_margintop= "6dip" android:layout_weight= "1" Android:gravi ty= "Center_vertical" > <textview android:id= "@+android:id/title" android:layout_width= " Wrap_content "android:layout_height=" Wrap_conTent "android:ellipsize=" marquee "android:fadingedge=" horizontal "android:singleline=" Tru E "android:textappearance="? Android:attr/textappearancemedium "/> <textview android:id= "@+android:id/summary" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_alignstart= "@android: Id/title" android:layout_below= "@android: Id/title" Androi D:ellipsize= "End" android:maxlines= "2" android:textappearance= "Android:attr/textappearancesmall"/& Gt </RelativeLayout> <!--Preference should place it actual Preference widget here. --<spinner android:id= "@+id/spinner1" android:layout_width= "Wrap_content" Android:layout_h eight= "wrap_content" android:layout_gravity= "center" android:padding= "8dip"/></linearlayout>
three: quoting a Layout file for which to specify UI, you can implement the following two callback functions:
@Override protected View Oncreateview (viewgroup parent) { //TODO auto-generated method stub return Super.oncreateview (parent); } @Override protected void Onbindview (view view) { Super.onbindview (view); Mspinner = (Spinner) View.findviewbyid (r.id.spinner1); string[] Arraystr = View.getresources (). Getstringarray (r.array.itemspinner_values); Madapter = new Arrayadapter<string> (View.getcontext (), Android. R.layout.simple_spinner_dropdown_item, arraystr); You can also define the style of the adapter yourself //Madapter = new arrayadapter<string> (View.getcontext (), R.layout.preference_ Categoary, mmeausstr); Mspinner.setadapter (madapter); Mspinner.setonitemselectedlistener (this); }
Four: Add the required events for the build here
Implements
Onitemselectedlistener
@Override public void onitemselected (adapterview<?> parent, view view, int position, long id) { if (Callcha Ngelistener (position)) { setValue (position); } } @Override public void onnothingselected (adapterview<?> parent) { }
V: Save or persist our changes
set our changes
public void SetValue (int value) { //always persist/notify the first time. Final Boolean changed =! Textutils.equals (Integer.tostring (Mvalue), integer.tostring (value)); if (changed | |!mvalueset) { mvalue = value; Mvalueset = true; Persistint (value); if (changed) { notifychanged ();}} } @Override public void onitemselected (adapterview<?> parent, view view, int position, long id) { mPos = pos ition; if (Callchangelistener (position)) { setValue (position); } }
Multiple basic data types can be persisted
@Override protected void Onsetinitialvalue (Boolean restorevalue, Object defaultvalue) { SetValue ( Restorevalue? Getpersistedint (Mvalue): (Integer) defaultvalue); }
Six-effect map