SoFunction
Updated on 2025-04-05

Detailed explanation of the usage example of the trash menu under Android spinner

This article describes the usage of the trash menu under Android spinner. Share it for your reference, as follows:

1. Use arrays as data source

1. In the layout layout file: declare a TextView control and a Spinner control, the code is as follows

<Spinner android:
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"></Spinner>

2. Add the following code to the Activity file

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class SpinnerActivity extends Activity {
  private static final String[] m={"Type A","Type B","O-type","Type AB","other"};
  private TextView view ;
  private Spinner spinner;
  private ArrayAdapter&lt;String&gt; adapter;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    (savedInstanceState);
    setContentView();
    view = (TextView) findViewById();
    spinner = (Spinner) findViewById(.Spinner01);
    //Connect optional content with ArrayAdapter    adapter = new ArrayAdapter&lt;String&gt;(this,.simple_spinner_item,m);
    //Set the style of the drop-down list    (.simple_spinner_dropdown_item);
    //Add adapter to spinner    (adapter);
    //Add event Spinner event listening    (new SpinnerSelectedListener());
    //Set the default value    ();
  }
  //Operate in array form  class SpinnerSelectedListener implements OnItemSelectedListener{
    public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1, int arg2,
        long arg3) {
      ("Your blood type is:"+m[arg2]);
    }
    public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
    }
  }
}

2. Use XML as the data source

1. Create a new file in the values ​​folder: the code is as follows

&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;resources&gt;
  &lt;string-array name="plantes"&gt;
    &lt;item&gt;NOKIA&lt;/item&gt;
    &lt;item&gt;MOTO&lt;/item&gt;
    &lt;item&gt;HTC&lt;/item&gt;
    &lt;item&gt;LG&lt;/item&gt;
    &lt;item&gt;other&lt;/item&gt;
  &lt;/string-array&gt;
&lt;/resources&gt;

2. Add the following code to the Activity file

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class SpinnerActivity extends Activity {
  private TextView view2;
  private Spinner spinner2;
  private ArrayAdapter adapter2;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    (savedInstanceState);
    setContentView();
    spinner2 = (Spinner) findViewById(.spinner02);
    view2 = (TextView) findViewById(.spinnerText02);
    //Connect optional content with ArrayAdapter    adapter2 = (this, , .simple_spinner_item);
    //Set the style of the drop-down list (.simple_spinner_dropdown_item);
    //Add adapter2 to spinner    (adapter2);
    //Add event Spinner event listening    (new SpinnerXMLSelectedListener());
    //Set the default value    ();
  }
  //Operate in XML  class SpinnerXMLSelectedListener implements OnItemSelectedListener{
    public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1, int arg2,
        long arg3) {
      ("What kind of phone do you use:"+(arg2));
    }
    public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
    }
  }
}

For more information about Android related content, please check out the topic of this site:Android programming activity operation skills summary》、《Android resource operation skills summary》、《Android file operation skills summary》、《Summary of Android's SQLite database skills》、《Summary of Android operating json format data skills》、《Android database operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android development introduction and advanced tutorial》、《Android View View Tips Summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.