我有两个微调项目。一个是我的Day旋转器,另一个是我的Month Spinner。 如果我从月份微调器选择月份2月份,如果我选择白天为30,则不应该这样做。
另一个例子:4月份有30天,所以如果有人选择Month作为April,将day作为31st,那么它是不正确的。请告诉我如何获取一个微调器的值对应于另一个微调器的值。
这是我的代码,我有两个微调器:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*************day spinner**********/
Spinner day = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> dayadapter = ArrayAdapter.createFromResource(
this, R.array.item_day, android.R.layout.simple_spinner_item);
dayadapter.setDropDownViewResource(R.layout.spinner_layout);
day.setAdapter(dayadapter);
/*****************month spinner**********************/
Spinner month = (Spinner) findViewById(R.id.spinner3);
ArrayAdapter<CharSequence> monthadapter = ArrayAdapter.createFromResource(
this, R.array.item_month, android.R.layout.simple_spinner_item);
monthadapter.setDropDownViewResource(R.layout.spinner_layout);
month.setAdapter(monthadapter);
}}
我的Xml定义了微调器:
<Spinner
android:id="@+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spinner3"
android:layout_alignTop="@+id/textView4"
android:layout_toLeftOf="@+id/textView3" />
<Spinner
android:id="@+id/spinner3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner2"
android:layout_toLeftOf="@+id/textView3"
android:layout_toRightOf="@+id/textView5" />
我的spinner_layout:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="12pt"
/>
我的日子阵
<string name="day_picker">Select an iten</string>
<string-array name="item_day">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
<item>11</item>
<item>12</item>
<item>13</item>
<item>14</item>
<item>15</item>
<item>16</item>
<item>17</item>
<item>18</item>
<item>19</item>
<item>20</item>
<item>21</item>
<item>22</item>
<item>23</item>
<item>24</item>
<item>25</item>
<item>26</item>
<item>27</item>
<item>28</item>
<item>29</item>
<item>30</item>
<item>31</item>
</string-array>
我的月份数组
<resources>
<string name="month_picker">Select an item</string>
<string-array name="item_month">
<item>January</item>
<item>February</item>
<item>March</item>
<item>April</item>
<item>May</item>
<item>June</item>
<item>July</item>
<item>August</item>
<item>September</item>
<item>October</item>
<item>November</item>
<item>December</item>
</string-array>
</resources>
如果我选择任何一个月,我想得到相应的号码。白天自动旋转的日子 注意:单击按钮后我不希望出现任何错误消息。提前谢谢。
答案 0 :(得分:2)
这很简单。在月份微调器上放置一个OnItemSelectedListener(),添加自己的侦听器,使用正确的值初始化日期微调器
答案 1 :(得分:1)
month.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
List<String> s = Arrays.asList(getResources().getStringArray(R.array.item_day));
if (pos == 0 || pos == 2 || pos == 4 || pos == 8 || pos == 9
|| pos == 11) {
ArrayAdapter<String> dayadapter = new ArrayAdapter<String>(Latlondemo.this, android.R.layout.simple_spinner_item,s);
day.setAdapter(dayadapter);
} else if (pos == 1) {
s = s.subList(0,28);
ArrayAdapter<String> dayadapter = new ArrayAdapter<String>(Latlondemo.this, android.R.layout.simple_spinner_item,s);
day.setAdapter(dayadapter);
} else {
s = s.subList(0,30);
ArrayAdapter<String> dayadapter = new ArrayAdapter<String>(Latlondemo.this, android.R.layout.simple_spinner_item,s);
day.setAdapter(dayadapter);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});