Implementation of the android drop-down menu (Spinner), Android spinner
I. First deliver:
Ii. xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:background="@drawable/blue" android:id="@+id/main" android:padding="0dp" > <Spinner android:id="@+id/Spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:prompt="@string/Color" /></RelativeLayout>
Iii. Code implementation:
Package com. example. spinnerdemo; import java. util. arrayList; import java. util. list; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; import android. view. window; import android. widget. adapterView; import android. widget. adapterView. onItemSelectedListener; import android. widget. arrayAdapter; import android. widget. relativeLayout; import android. widget. spinn Er; import android. widget. toast; public class MainActivity extends Activity {private Spinner spinner; private RelativeLayout relativeLayout; private ArrayAdapter <String> adapter; private int [] resource = {R. drawable. blue, R. drawable. red, R. drawable. green}; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. l Ayout. activity_main); relativeLayout = (RelativeLayout) findViewById (R. id. main); spinner = (Spinner) this. findViewById (R. id. spinner); spinner. setPrompt ("Set background color"); adapter = new ArrayAdapter <String> (this, android. r. layout. simple_spinner_item, getData (); adapter. setDropDownViewResource (android. r. layout. simple_spinner_dropdown_item); spinner. setAdapter (adapter); spinner. setSelection (0, true); // jump directly to a specified data Spinner. setOnItemSelectedListener (new SelectedListener ();}/*** data source * @ return */private List <String> getData () {List <String> items = new ArrayList <String> (); items. add ("blue"); items. add ("red"); items. add ("green"); return items;}/*** Click Event * @ author Administrator **/final class SelectedListener implements OnItemSelectedListener {public void onItemSelected (AdapterView <?> Arg0, View arg1, int arg2, long arg3) {List <String> list = getData (); relativeLayout. setBackgroundResource (resource [arg2]); toast (list. get (arg2);}/*** the callback function executed when all the entries in the selected state in the view disappear. * Enabling the touch function or an empty adapter may cause the selected entries to disappear. */Public void onNothingSelected (AdapterView <?> Arg0) {}}/*** custom toast * @ param msg */public void toast (String msg) {Toast. makeText (this, msg, Toast. LENGTH_SHORT ). show () ;}// Menu public boolean onCreateOptionsMenu (menu) {// Inflate the Menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}