自訂 Android Preference——SpinnerPreference的私人定製

來源:互聯網
上載者:User

標籤:android   preference   spinner   widget   布局   

因客戶需求SpinnerPreference,網上各種搜尋不到,無奈只能重寫組件,現將過程分享大家。

自訂SpinnerPreference一:首先從擴充preference開始:類檔案必須繼承自Preference並實現建構函式,如下
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);    }

二:自訂布局檔案的重寫

<?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:gravity="center_vertical"    android:minHeight="48dp"    android:paddingEnd="?android:attr/scrollbarSize"    android:paddingStart="@dimen/mypreference_margin_start" >    <RelativeLayout        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_marginBottom="6dip"        android:layout_marginEnd="6dip"        android:layout_marginStart="2dip"        android:layout_marginTop="6dip"        android:layout_weight="1"        android:gravity="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="true"            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"            android:ellipsize="end"            android:maxLines="2"            android:textAppearance="?android:attr/textAppearanceSmall" />    </RelativeLayout>    <!-- Preference should place its actual preference widget here. -->    <Spinner        android:id="@+id/spinner1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:padding="8dip" /></LinearLayout>

三:引用一個layout檔案為其指定UI,可以通過實現如下兩個回呼函數:

  @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);        //  也可一自己定義適配器的樣式        // mAdapter = new ArrayAdapter<String>(view.getContext(), R.layout.preference_categoary, mMeausStr);        mSpinner.setAdapter(mAdapter);        mSpinner.setOnItemSelectedListener(this);    }

四:為組建添加需要的事件這裡 implements OnItemSelectedListener

    @Override    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {        if (callChangeListener(position)) {            setValue(position);        }    }    @Override    public void onNothingSelected(AdapterView<?> parent) {    }

五:儲存或持久化我們的改動

設置我們的改動

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 = position;        if (callChangeListener(position)) {            setValue(position);        }    }

可以持久化多種基礎資料型別 (Elementary Data Type)


    @Override    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {        setValue(restoreValue ? getPersistedInt(mValue) : (Integer) defaultValue);    }

六效果圖







聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.