选中条目android spinner的使用
最近應(yīng)用開發(fā)的過程中出現(xiàn)了一個(gè)小問題,順便記錄一下原因和方法--選中條目
????spinner控件由以下幾部分構(gòu)成:
????1)String[]——也就是一個(gè)String數(shù)組,用于存儲我們需要顯示的條目。
????2)Spinner——重要控件,未幾說了。
????3)ArrayAdapter<String>——適配器,用于將我們定義的條目數(shù)組綁定到Spinner上。
????需要注意的是多個(gè)Spinnner綁定同一個(gè)Listener,onItemSelected(AdapterView<?> arg0, View arg1, int arg2, ?long arg3)方法的第一個(gè)參數(shù)才是我們需要區(qū)分的Spinner View,arg2是選中的條目序號。這也是選中后會觸發(fā)的事件,因此可以在此取出選中的值。示例代碼如下:
public class RegisterActivity extends Activity { private EditText register_password; private EditText register_username; private EditText register_email; private EditText register_realname; private EditText register_stunumber; private static final String[] string_year={"1980","1981","1982","1983","1984","1985","1986","1987","1988","1989","1990","1991","1992","1993","1994","1995","1996","1997","1998","1999","2000","2001","2002","2003","2004","20050","2006","2007","2008","2009","2010","2011","2012","2013"}; private static final String[] string_month={"1","2","3","4","5","6","7","8","9","10","11","12"}; private static final String[] string_day={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"}; private static final String[] string_group={"先生","本科生","碩士","博士"}; private static final String[] string_department={"計(jì)算機(jī)學(xué)院","數(shù)學(xué)院","外語學(xué)院","機(jī)械學(xué)院","自動化學(xué)院","物理學(xué)院","化學(xué)院","藝術(shù)學(xué)院","文學(xué)院"}; private Spinner register_year; private Spinner register_month; private Spinner register_day; private Spinner register_group; private Spinner register_department; private ArrayAdapter<String> register_year_adapter; private ArrayAdapter<String> register_month_adapter; private ArrayAdapter<String> register_day_adapter; private ArrayAdapter<String> register_group_adapter; private ArrayAdapter<String> register_department_adapter; private String selected_year="1980"; private String selected_month="1"; private String selected_day="1"; private String selected_group="先生"; private String selected_department="計(jì)算機(jī)院";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_register);register_password=(EditText)findViewById(R.id.register_password);register_password.setInputType(InputType.TYPE_CLASS_TEXT| InputType.TYPE_TEXT_VARIATION_PASSWORD);register_month = (Spinner) findViewById(R.id.register_month); register_month_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,string_month); register_month_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); register_month.setAdapter(register_month_adapter); register_month.setOnItemSelectedListener(new SpinnerSelectedListener()); register_month.setVisibility(View.VISIBLE); register_year = (Spinner) findViewById(R.id.register_year); register_year_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,string_year); register_year_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); register_year.setAdapter(register_year_adapter); register_year.setOnItemSelectedListener(new SpinnerSelectedListener()); register_year.setVisibility(View.VISIBLE); register_day = (Spinner) findViewById(R.id.register_day); register_day_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,string_day); register_day_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); register_day.setAdapter(register_day_adapter); register_day.setOnItemSelectedListener(new SpinnerSelectedListener()); register_day.setVisibility(View.VISIBLE); register_group = (Spinner) findViewById(R.id.register_group); register_group_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,string_group); register_group_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); register_group.setAdapter(register_group_adapter); register_group.setOnItemSelectedListener(new SpinnerSelectedListener()); register_group.setVisibility(View.VISIBLE); register_department = (Spinner) findViewById(R.id.register_department); register_department_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,string_department); register_department_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); register_department.setAdapter(register_department_adapter); register_department.setOnItemSelectedListener(new SpinnerSelectedListener()); register_department.setVisibility(View.VISIBLE); }@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.register, menu);return true;}class SpinnerSelectedListener implements OnItemSelectedListener{ public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { if(arg0==register_year)selected_year=string_year[arg2];else if(arg0==register_month)selected_month=string_month[arg2];else if(arg0==register_day)selected_day=string_day[arg2];else if(arg0==register_group)selected_group=string_group[arg2];else if(arg0==register_department)selected_department=string_department[arg2];} public void onNothingSelected(AdapterView<?> arg0) { } } } 每日一道理盈盈月光,我掬一杯最清的;落落余輝,我擁一縷最暖的;灼灼紅葉,我拾一片最熱的;萋萋芳草,我摘一束最燦的;漫漫人生,我要采擷世間最重的———毅力。
????布局文件如下:
<LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="1dp"android:text="生 日"android:textSize="25px" /><Spinner android:id="@+id/register_year" android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_marginLeft="0dp"android:layout_marginRight="0dp"/><Spinner android:id="@+id/register_month" android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_marginLeft="0dp"android:layout_marginRight="0dp"/><Spinner android:id="@+id/register_day" android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_marginLeft="0dp"android:layout_marginRight="0dp"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="10dp"android:text="分 組"android:textSize="25px" /><Spinner android:id="@+id/register_group" android:layout_width="match_parent" android:layout_height="wrap_content"/></LinearLayout> <LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="10dp"android:text="院 系"android:textSize="25px" /><Spinner android:id="@+id/register_department" android:layout_width="match_parent" android:layout_height="wrap_content"/></LinearLayout>????才疏學(xué)淺,難免有疏漏錯(cuò)誤,請指正!想交流可以留言,獨(dú)特學(xué)習(xí)先進(jìn)。
文章結(jié)束給大家分享下程序員的一些笑話語錄: 這個(gè)世界上只有10種人:懂得二進(jìn)制的和不懂得二進(jìn)制的。
--------------------------------- 原創(chuàng)文章 By
選中和條目
---------------------------------
轉(zhuǎn)載于:https://www.cnblogs.com/xinyuyuanm/archive/2013/05/30/3109044.html
總結(jié)
以上是生活随笔為你收集整理的选中条目android spinner的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ObservableCollection
- 下一篇: Oracle命令--数据文件被误删后的处