AndroidUI control series: Spinner (drop-down list)

Source: Internet
Author: User
When registering an account on a website, the website will ask us to provide information such as gender, birthday, and city. for convenience, a drop-down list is provided for us to choose from, this function is also available in Android. this is the Spinner drop-down list. When an account is registered on a website, the website will ask us to provide information such as gender, birthday, and city. for convenience, A drop-down list is provided for us to choose from. This function is also available in Android, which is the Spinner drop-down list.

At the same time of encoding, you must first set the Spinner component in the layout, then connect the optional content through the ArrayAdapter and the drop-down list, and finally obtain the selected options, we need to design the event listener setOnItemSelectedListener and implement onItemSelected to get the content selected by the user. Finally, set the current display item through setVisibility.

SpinnerTest. java

Package org. hualang. spinner; import android. app. activity; 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 SpinnerTest extends Activity {/** Called when the activity is first created. */private static final String [] citys = {"Hangzhou", "Beijing", "Chengdu", "Dalian", "Shenzhen", "Nanjing"}; private TextView text; private Spinner spinner; private ArrayAdapter
 
  
Adapter; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); text = (TextView) findViewById (R. id. text); spinner = (Spinner) findViewById (R. id. spinner); // connect the available content to the ArrayAdapter adapter. adapter = new ArrayAdapter
  
   
(This, android. r. layout. simple_spinner_item, citys); // sets the style adapter of the drop-down list. setDropDownViewResource (android. r. layout. simple_spinner_dropdown_item); // add the adapter to the spinner. setAdapter (adapter); // add a Spinner event to listen to the spinner. setOnItemSelectedListener (new Spinner. onItemSelectedListener () {@ Override public void onItemSelected (AdapterView
   Arg0, View arg1, int arg2, long arg3) {// TODO Auto-generated method stub text. setText ("your city is:" + citys [arg2]); // you can set arg0.setVisibility (View. VISIBLE) ;}@ Override public void onNothingSelected (AdapterView
   Arg0) {// TODO Auto-generated method stub }});}}
  
 

Main. xml

   
   
    
    
 

Running result:

The above is the content of the Android UI control series: Spinner (drop-down list). For more information, see PHP Chinese network (www.php1.cn )!

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.