Spinner (android) in the drop-down list)

Source: Internet
Author: User

The drop-down list is a spinner. This is easy to use. Define a tag in xml and specify the ArrayAdapter <String>,

You must specify the drop-down layout for the Adapter. For the drop-down data, the book uses static arrays. I changed it to read from the resource file.

Java code

Package zziss. android. spinnerTest;

Import java. util. Map;

Import android. app. Activity;
Import android. content. res. Resources;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. AdapterView;
Import android. widget. ArrayAdapter;
Import android. widget. Spinner;
Import android. widget. TextView;

Public class SpinnerTestActivity extends Activity {
/** Called when the activity is first created .*/

Private TextView iTv;
Private Spinner iSpinner;
Private String [] items;
Private ArrayAdapter <Map <String, Integer> iAda;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
ITv = (TextView) this. findViewById (R. id. TV );
ISpinner = (Spinner) this. findViewById (R. id. spinner );
Resources res = getResources ();
Items = res. getStringArray (R. array. listItems );
ArrayAdapter <String> ada = new ArrayAdapter <String> (this,
Android. R. layout. simple_spinner_item, items );
Ada. setDropDownViewResource (android. R. layout. simple_spinner_dropdown_item );

ISpinner. setAdapter (ada );
ISpinner. setOnItemSelectedListener (new Spinner. OnItemSelectedListener ()
{

@ Override
Public void onItemSelected (AdapterView <?> Arg0, View arg1,
Int arg2, long arg3 ){
// TODO Auto-generated method stub
ITv. setText (items [arg2]);
}

@ Override
Public void onNothingSelected (AdapterView <?> Arg0 ){
// TODO Auto-generated method stub

}

});
}
}

Main. xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">

<TextView
Android: id = "@ + id/TV"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = ""/>
<Spinner
Android: id = "@ + id/spinner"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerHorizontal = "true"
/>
</LinearLayout>

 

String. xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>

<String name = "hello"> Hello World, SpinnerTestActivity! </String>
<String name = "app_name"> SpinnerTest </string>
<String-array name = "listItems">
<Item> type A </item>
<Item> type B </item>
<Item> AB </item>
<Item> O-type </item>
</String-array>

</Resources>

 

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.