我正在开发一个可以帮助租赁业务的应用程序。我有一个微调器,它从数组中提取它的内容......
从 datalist.xml :
<string-array name="dur">
<item>@string/dur1</item>
<item>@string/dur2</item>
<item>@string/dur3</item>
<item>@string/dur4</item>
<item>@string/dur5</item>
<item>@string/dur6</item>
<item>@string/dur7</item>
<item>@string/dur8</item>
<item>@string/dur9</item>
<item>@string/dur10</item>
<item>@string/dur11</item>
</string-array>
字符串.xml:
<string name="dur1">Half hour</string>
<string name="dur2">1 hour</string>
<string name="dur3">2 hours</string>
<string name="dur4">3 hours</string>
<string name="dur5">4 hours</string>
<string name="dur6">5 hours</string>
<string name="dur7">6 hours</string>
<string name="dur8">Over night</string>
<string name="dur9">24 hours</string>
<string name="dur10">Week</string>
<string name="dur11">Month</string>
java中的微调器代码:
Spinner spinner = (Spinner) findViewById(R.id.spinnerDuration);
ArrayAdapter <CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.dur, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
// storing string resources into Array
dur = getResources().getStringArray(R.array.dur);
}
public void onNothingSelected(AdapterView<?> arg0) {
// do nothing
}
}
);
我该如何设置它,以便当用户从微调器中选择一个选项时,它将该选项保存为一个整数变量,反映小时数,然后可以在此应用程序的其他部分使用?
例如...有人选择“3 小时”并将名为“durnumb”的变量保存为 3。或者有人选择“周”并将 durnumb 的值保存为 168(24 小时 x 7 天)