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.