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