Package com. android. liu. ui;
Import com. android. liu. R;
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 SpinnerActivity extends Activity {
Private static final String [] m_Countries = {"A", "B", "AB", "O", "other "};
Private TextView m_TextView;
Private Spinner m_Spinner;
Private ArrayAdapter <String> adapter;
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. spinner );
M_TextView = (TextView) this. findViewById (R. id. SpinnerText );
M_Spinner = (Spinner) this. findViewById (R. id. spinner );
// Connect the available content to the ArrayAdapter
Adapter = new ArrayAdapter <String> (this, android. R. layout. simple_spinner_item, m_Countries );
// Set the drop-down Style of the List
Adapter. setDropDownViewResource (android. R. layout. simple_spinner_dropdown_item );
M_Spinner.setAdapter (adapter );
M_Spinner.setOnItemSelectedListener (new Spinner. OnItemSelectedListener (){
@ Override
Public void onItemSelected (AdapterView <?> Arg0, View arg1,
Int arg2, long arg3 ){
M_TextView.setText ("your blood type is:" + m_Countries [arg2]);
// Display the currently selected items
Arg0.setVisibility (View. VISIBLE );
}
@ Override
Public void onNothingSelected (AdapterView <?> Arg0 ){
// TODO Auto-generated method stub
}});
}
}
<? 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"
>
<TextView
Android: id = "@ + id/SpinnerText"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "select the correct answer"
> </TextView>
<Spinner
Android: id = "@ + id/spinner"
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: layout_centerHorizontal = "true"
> </Spinner>
</LinearLayout>