package com.sxt.day05_04; import android.os.Bundle; import android.app.Activity; import android.content.res.Resources; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; import android.widget.AdapterView.OnItemSelectedListener; public class MainActivity extends Activity implements OnItemSelectedListener{ Spinner mspinYear,mspinMonth; ArrayAdapter<CharSequence> mAdapterYear,mAdapterMonth; String[] mYears,mMonths,mDays; int mYear=2000;//初始年份 int mMonth=1;//初始月份 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); initView(); } private void initView() { mspinYear=(Spinner) findViewById(R.id.spinYear); //给mspinYear创建一个适配器,R.array.years是要加入进去的字符串数组资源,android.R.layout.simple_list_item_1是显示的列表项 mAdapterYear=ArrayAdapter.createFromResource(this, R.array.years, android.R.layout.simple_list_item_1); //设置适配器 mspinYear.setAdapter(mAdapterYear); mspinYear.setOnItemSelectedListener(this); } private void initData() { Resources res = getResources();//获取资源中定义的数组 mYears=res.getStringArray(R.array.years); mMonths=res.getStringArray(R.array.months); mDays=res.getStringArray(R.array.days); } @Override //parent是Spinner,view是点击的某一项, public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { switch (parent.getId()) { case R.id.spinYear: mYear=Integer.parseInt(mYears[position]); if(mMonth==2&&(mYear%400==0 || mYear%4==0 && mYear%100!=0)){ Toast.makeText(MainActivity.this, mDays[0]+"天", 2000).show(); }else{ Toast.makeText(MainActivity.this, mDays[mMonth], 2000).show(); } break; case R.id.spinMonth: break; } } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tvYear" android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择年份" /> <Spinner android:id="@+id/spinYear" android:layout_toRightOf="@id/tvYear" android:layout_width="match_parent" android:layout_height="wrap_content" android:prompt="@string/prompt_year" android:spinnerMode="dialog"/> 下拉之后以dialog对话框显示 <TextView android:id="@+id/tvMonth" android:layout_marginTop="10dp" android:layout_below="@id/spinYear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择月份" /> <Spinner android:id="@+id/spinMonth" android:layout_toRightOf="@id/tvMonth" android:layout_below="@id/spinYear" android:layout_width="match_parent" android:layout_height="wrap_content" android:prompt="@string/prompt_year" android:spinnerMode="dialog"/> 下拉之后以dialog对话框显示 </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Day05_04-ArrayAdapterAndSpinnerDemo</string> <string name="action_settings">Settings</string> <string name="prompt_year">选择年份</string> <string name="prompt_month">选择月份</string> <string-array name="years"> <item>2000</item> <item>2001</item> <item>2002</item> <item>2003</item> <item>2004</item> <item>2005</item> <item>2006</item> <item>2007</item> </string-array> <string-array name="months"> <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> </string-array> <string-array name="days"> <item >29</item> <item >31</item> <item >28</item> <item >31</item> <item >30</item> <item >31</item> <item >30</item> <item >31</item> <item >31</item> <item >30</item> <item >31</item> <item >30</item> <item >31</item> </string-array> </resources>