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; ArrayAdaptermAdapterYear,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 }}
下拉之后以dialog对话框显示 下拉之后以dialog对话框显示
Day05_04-ArrayAdapterAndSpinnerDemo Settings 选择年份 选择月份 - 2000
- 2001
- 2002
- 2003
- 2004
- 2005
- 2006
- 2007
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 29
- 31
- 28
- 31
- 30
- 31
- 30
- 31
- 31
- 30
- 31
- 30
- 31