Android control: Use the drop-down list box -- Spinner

Source: Internet
Author: User

--- Restore content start ---

I. Previous Code

<Spinner android: id = "@ + id/spin" android: paddingTop = "10px" android: layout_width = "fill_parent" android: layout_height = "50sp"/> <Button android: id = "@ + id/addList" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "add"/>

2. Create a New View resource in the drop-down list, that is, the TextView of each Item. we name it dropdown. xml and place it in drawable. The code is:

<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/tv1"    android:layout_width="fill_parent"    android:layout_height="20sp"    android:singleLine="true"    style="?android:attr/spinnerDropDownItemStyle"    />

3. Create an Adapter

Here we will introduce two functions:

1. ArrayAdapter. ArrayAdapter (Context context, int textViewResourcId, List <String> objects)

We can use it to create a new Adapter object. Three parameters are required:

(1) context is the Activity and application of the Adapter.

(2) Resource id of textViewResourcId TextView

(3) The last parameter is the data you want to add to the drop-down List. It can be a static String array or a dynamic List <String>;

2. AdapterView. setOnItemSelectedListener (OnItemSelectedListener listener). This method is used to set a listener for the Spinner object. The parameter here is the OnItemSelectedListener interface, which requires two methods to be rewritten:

(1) public void onItemSelected (AdapterView <?> Parent, View view,Int position, long id)

This method can be used to complete the processing when the option is selected. Meanings of the four parameters:

  • AdapterView <?> Parent. This is similar to context. It only indicates the AdapterView of your current operation in the range comparison.
  • View view. This parameter is the TextView object you click.
  • The int position parameter indicates the position of the view you clicked in the entire AdaterView.
  • Long id click the view id

(2) public void onNothingSelected (AdapterView <?> Parent)

This callback function is called when no options are available in AdapterView.

The complete code is as follows:

Private Spinner spinner; private TextView TV; private ArrayAdapter <String> adapter; private static final String [] years = {"less than 1 year", "1-3 years ", "3-5 years", "more than 5 years"}; private ArrayList <String> array = new ArrayList <String> (); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); if (savedInstanceState = null) {getSuppor TFragmentManager (). beginTransaction (). add (R. id. container, new PlaceholderFragment ()). commit ();} spinner = (Spinner) findViewById (R. id. spin); TV = (TextView) findViewById (R. id. textView1); for (int I = 0; I <years. length; I ++) {array. add (years [I]);} adapter = new ArrayAdapter <String> (this, android. r. layout. simple_spinner_item, array); adapter. setDropDownViewResource (R. drawable. dropdown); spinne R. setAdapter (adapter); spinner. setOnItemSelectedListener (new OnItemSelectedListener () {@ Override public void onItemSelected (AdapterView <?> Parent, View view, int position, long id) {// TODO automatically generated method stub String seleted = array. get (position); TV. setText (seleted); parent. setVisibility (View. VISIBLE) ;}@ Override public void onNothingSelected (AdapterView <?> Arg0) {// TODO automatically generated method stub TV. setText ("You have not selected ");}});

 

--- Restore content end ---

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.