一、 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 相对应