Learning about the Android UI widget Spinner

Source: Internet
Author: User

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>

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.