Android [beginner's Guide] Article 4 "Spinner control"

Source: Internet
Author: User

I followed the previous article. This time, the goblin told me not to use the RadioButton control. It's hard to see it, but it still occupies a place. It takes up four places to catch a person, then I only need to take a place to catch a person, so I used the Spinner control to catch one at a time.

Main. xml is as follows

View plain
</Pre> <pre class = "html" name = "code"> <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Spinner android: layout_height = "wrap_content" android: id = "@ + id/spinner"
Android: layout_width = "wrap_content"> </Spinner>
<TextView android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: text = "@ string/hello"
Android: id = "@ + id/text"> </TextView>
</LinearLayout>

The java code of the Activity is as follows:

View plain
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. AdapterView;
Import android. widget. AdapterView. OnItemSelectedListener;
Import android. widget. ArrayAdapter;
Import android. widget. Spinner;
Import android. widget. TextView;
 
Public class ButtonDemoActivity extends Activity implements OnItemSelectedListener
{
Private TextView text = null;
Private Spinner spinner;
Private String [] item = {"Tang Seng", "Sun Wukong", "", "Sha Monk "};
 
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
 
// Find the TextView control in main. xml by ID
Text = (TextView) findViewById (R. id. text );
 
// Find the Spinner control in main. xml by ID
Spinner = (Spinner) findViewById (R. id. spinner );

// Set an Array adapter to put Array data into the adapter
ArrayAdapter adapter = new ArrayAdapter (this,
Android. R. layout. simple_spinner_item, item );
// Set the style of the drop-down list
Adapter. setDropDownViewResource (android. R. layout. simple_spinner_dropdown_item );

// Adapt the Spinner.
Spinner. setAdapter (adapter );

// Listener for event selection in the Spinner
Spinner. setOnItemSelectedListener (this );
 
}
 
Private void updateText (String string)
{
// Set the text information to display in the TextView control.
Text. setText (string );
}
 
@ Override
Public void onItemSelected (AdapterView <?> Arg0, View arg1, int position,
Long arg3)
{
String str = "this time the goblin grabbed" + item [position] +! ";
UpdateText (str );

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

}
 
}
There are a lot of things you need to say about the goblin. It will be like this for a while. In our current words, it is called "difficult to make a public speech". After all, there are more monsters,


 

From: kangkangz4 Column

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.