Tuesday, June 26, 2012

Spinner is a very handy tool if you want to write a drop-down menu 




First to declare a Spinner in the xml 
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Spinner android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/mySpinner" /> </LinearLayout> 

Then initialize the Spinner 
 private Spinner spinner; private ArrayAdapter<String> lunchList; private Context mContext; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mContext = this.getApplicationContext(); spinner = (Spinner)findViewById(R.id.mySpinner); } 

Again to declare an array, you want to put information go Spinner management side of 
 private String[] lunch = {""}; 

Then we want to use ArrayAdapter to assign it to Spinner 
 lunchList = new ArrayAdapter<String>(SpinnerDemo.this, android.R.layout.simple_spinner_item, lunch); spinner.setAdapter(lunchList); 



Then we have to set the how to deal with them at this time we will join the event an item when the user presses the Spinner 
 spinner.setOnItemSelectedListener(new OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) { Toast.makeText(mContext, "你選的是"+lunch[position], Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); 



Code 
http://uploadingit.com/file/rfq2ac02xoanegtk/SpinnerDemo.zip

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © .Hacking Cracking Tricks And Tutorials, Paid Scripts, Latest Exploits, 0Day Vulnerability, - Skyblue - Powered by Blogger - Designed by Johanes Djogan -