Android tutorial (10) -- Use of the Spinner drop-down box

Source: Internet
Author: User

I am troubled by work problems, so I have not written any blogs for the past few days.

Recently, I felt I was lazy. As a programmer, we had to keep learning when we were charging, so we couldn't leave ourselves alone.

Let's take a look at the use of the Spinner control in the drop-down box.

Step 1: Create a project Ep. Spinner, and use the default values for the rest.

Step 2: edit the view to place a Spinner control on The View.


[Java]
<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: padding = "10dip"
Tools: context = ". MainActivity">
 
<TextView
Android: id = "@ + id/textView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "10dip"
Android: text = "Please select a planet:"/>
 
<Spinner
Android: id = "@ + id/spinner1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignLeft = "@ + id/textView1"
Android: layout_below = "@ + id/textView1"
Android: layout_marginTop = "15dp"
Android: prompt = "@ string/planet_prompt"
/>
 
</RelativeLayout>

<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: padding = "10dip"
Tools: context = ". MainActivity">

<TextView
Android: id = "@ + id/textView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "10dip"
Android: text = "Please select a planet:"/>

<Spinner
Android: id = "@ + id/spinner1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignLeft = "@ + id/textView1"
Android: layout_below = "@ + id/textView1"
Android: layout_marginTop = "15dp"
Android: prompt = "@ string/planet_prompt"
/>

</RelativeLayout>
Step 3: Write the core file.


[Java]
Package com. example. ep. spinner;
 
Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. Menu;
Import android. widget. ArrayAdapter;
Import android. widget. Spinner;
 
Public class MainActivity extends Activity {
 
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

Spinner sp = (Spinner) findViewById (R. id. spinner1 );
ArrayAdapter arr = ArrayAdapter. createFromResource (this, R. array. planets, android. R. layout. simple_spinner_item );

Arr. setDropDownViewResource (android. R. layout. simple_dropdown_item_1line );
Sp. setAdapter (arr );
}
 
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}
 
}

Package com. example. ep. spinner;

Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. Menu;
Import android. widget. ArrayAdapter;
Import android. widget. Spinner;

Public class MainActivity extends Activity {

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

Spinner sp = (Spinner) findViewById (R. id. spinner1 );
ArrayAdapter arr = ArrayAdapter. createFromResource (this, R. array. planets, android. R. layout. simple_spinner_item );

Arr. setDropDownViewResource (android. R. layout. simple_dropdown_item_1line );
Sp. setAdapter (arr );
}

@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}

}

Finally, do not forget to add arrays. xml to the values file that stores the option data in the drop-down box.


[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String-array name = "planets">
<Item> Mercury </item>
<Item> Venus </item>
<Item> Earth </item>
<Item> Mars </item>
<Item> Jupiter </item>
<Item> Saturn </item>
<Item> Uranus </item>
<Item> Neptune </item>
</String-array>
</Resources>

<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String-array name = "planets">
<Item> Mercury </item>
<Item> Venus </item>
<Item> Earth </item>
<Item> Mars </item>
<Item> Jupiter </item>
<Item> Saturn </item>
<Item> Uranus </item>
<Item> Neptune </item>
</String-array>
</Resources>
Okay. Let's get the last running result.

 
 


 

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.