Spinner控件简单应用

一、     Activity部份代码

// Generate spinner entries using XML arrays

int visiblelimitValuesId = R.array.account_settings_down_count_values;

int visiblelimitEntriesId = R.array.account_settings_down_count_entries;

CharSequence[] visiblelimitValues = getResources().getTextArray(visiblelimitValuesId);

CharSequence[] visiblelimitEntries = getResources().getTextArray(visiblelimitEntriesId);

 

// Now create the array used by the Spinner

SpinnerOption[] checkVisiblelimits = new SpinnerOption[visiblelimitEntries.length];

for (int i = 0; i < visiblelimitEntries.length; i++) {

        checkVisiblelimits[i] = new SpinnerOption(

                    Integer.valueOf(visiblelimitValues[i].toString()), visiblelimitEntries[i].toString());

}

 

ArrayAdapter<SpinnerOption> checkVisiblelimitsAdapter = new ArrayAdapter<SpinnerOption (this,android.R.layout.simple_spinner_item, checkVisiblelimits);

 checkVisiblelimitsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

downloadCount.setAdapter(checkVisiblelimitsAdapter);

       

SpinnerOption.setSpinnerOptionValue(downloadCount,  VISIBLE_LIMIT_DEFAULT);

二、           SpinnerOption代码

public class SpinnerOption {

    public final Object value;

 

    public final String label;

 

    public static voidsetSpinnerOptionValue(

Spinner spinner, Object value) {

        for (int i = 0, count = spinner.getCount(); i < count; i++) {

            SpinnerOption so = (SpinnerOption)spinner.getItemAtPosition(i);

            if (so.value.equals(value)) {

                spinner.setSelection(i, true);

                return;

            }

        }

    }

 

    public SpinnerOption(Object value, String label) {

        this.value = value;

        this.label = label;

    }

 

    @Override

    public String toString() {

        return label;

}

三、     如何获取Spinner选中项值

int visibelLimit = (Integer)((SpinnerOption)downloadCount.getSelectedItem()).value;

 

四、

<PreferenceCategory android:title="@string/account_settings_download_title">

   

       <ListPreference

            android:layout="?android:attr/preferenceLayoutChild"

            android:dependency="account_down"

            android:key="account_settings_down"

            android:defaultValue="never"

            android:title="@string/account_settings_download_title"

            android:entries="@array/account_settings_down_entries"

            android:entryValues="@array/account_settings_down_values"

         android:dialogTitle="@string/account_settings_download_title"

/>

 

  </PreferenceCategory>

小结:

  Spinner控件对比PreferenceActivity 学习,entries entryValues 相对应

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值