Study Notes for Android

Source: Internet
Author: User

The spinner is equivalent to the drop-down list box in Html. In the following example, we will learn how to use the spinner.

 

Attribute:

 

Attribute name

Description

Android: prompt

This prompt is displayed in the drop-down list dialog box. (Translator's note: The title of the dialog box:

Method


:
Public CharSequence getPrompt ()
Return Value
Prompt displayed when the dialog box is displayed (obtain the title word in the pop-up view)
 
Public void onClick (DialogInterface dialog, int which)
This method is called when you click an item in the pop-up box.
Parameters
Dialog click the pop-up dialog box
Which click the Button (for example, Button) or click the location
 
Public Boolean merge mclick ()
If it is defined, the OnClickListener of this view is called. (for example, a drop-down list is displayed by default during loading ).
Return Value
Is True. A specified OnClickListener is called. If it is false, it is not called.
 
Public void setOnItemClickListener (AdapterView. OnItemClickListener I)
The Spinner does not support item click events. Calling this method causes an exception.
Parameters
L this listener will be ignored
 
Public void setPromptId (CharSequence prompt)
Prompt displayed when the dialog box is displayed (Translator's note: Set the title word in the pop-up view)
Parameters
Prompt for prompt settings
 
Public void setPromptId (int promptId)
Prompt displayed when the dialog box is displayed (Translator's note: Set the title word in the pop-up view)
Parameters
The prompted dialog box displays a message indicating the resource id.
Example:
 
Package android. imageview;
 
Import java. util. ArrayList;
Import java. util. List;
 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. ArrayAdapter;
Import android. widget. Spinner;
 
Public class ImageView1Activity extends Activity {
/** Called when the activity is first created .*/
Private Spinner spinner = null;
Private ArrayAdapter <String> adapter = null;
Private List <String> list = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Spinner = (Spinner) findViewById (R. id. spinner );
Spinner. setPrompt ("Please select the fruit you like? ");

List = new ArrayList <String> ();
List. add ("apple ");
List. add ("banana ");
List. add ("Li ");
List. add ("Peach ");

Adapter = new ArrayAdapter <String> (ImageView1Activity. this, android. R. layout. simple_dropdown_item_1line, list );
Spinner. setAdapter (adapter );
}
}


Another method:


Package android. imageview;
 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. ArrayAdapter;
Import android. widget. Spinner;
 
Public class ImageView1Activity extends Activity {
/** Called when the activity is first created .*/
Private Spinner spinner1 = null;
Private ArrayAdapter <CharSequence> adapter = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Spinner1 = (Spinner) findViewById (R. id. spinner1 );
Spinner1.setPrompt ("Where do you want to work in the future? ");
Adapter = ArrayAdapter. createFromResource (this, R. array. city, android. R. layout. simple_spinner_item );
Spinner1.setAdapter (adapter );
}
}
 
 
[Java] view plaincopy
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
 
<String name = "hello"> Hello World, ImageView1Activity! </String>
<String name = "app_name"> ImageView1 </string>
<String-array name = "city">
<Item> Shandong </item>
<Item> Shanghai </item>
<Item> Beijing </item>
<Item> Dalian </item>
</String-array>
</Resources>




Loading events:
 

 
Package android. imageview;
 
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. Toast;
 
Public class ImageView1Activity extends Activity {
/** Called when the activity is first created .*/
Private Spinner spinner1 = null;
Private ArrayAdapter <CharSequence> adapter = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Spinner1 = (Spinner) findViewById (R. id. spinner1 );
Spinner1.setPrompt ("Where do you want to work in the future? ");
Adapter = ArrayAdapter. createFromResource (this, R. array. city, android. R. layout. simple_spinner_item );
Spinner1.setAdapter (adapter );

Spinner1.setOnItemSelectedListener (new OnItemSelectedListener (){
 
Public void onItemSelected (AdapterView <?> Arg0, View arg1,
Int arg2, long arg3 ){
// TODO Auto-generated method stub
 
Toast. makeText (ImageView1Activity. this, "selected city:" + spinner1.getItemAtPosition (arg2). toString (), Toast. LENGTH_LONG). show ();
 
}
 
Public void onNothingSelected (AdapterView <?> Arg0 ){
// TODO Auto-generated method stub

}

});
}
}



}



 


From sunset hut

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.