Dynamic Implementation of the spinner drop-down menu in Android

Source: Internet
Author: User

Statement

You are welcome to repost, but please keep the original source of the article :)

Focus: http://www.focuser.org

Link: http://www.focuser.org /? P = 270

Due to the limited size of the cell phone screen, we are pursuing the best UI Effect in a limited space. In Android, the spinner drop-down menu is used many times in our development process and is also a technology that every developer must master.

The arraylist collection class is used in the menu to dynamically add and delete the spinner. Java's good brother knows the benefits of the Collection class, through the collection class
Arraylist is dynamically added and deleted. A custom drop-down menu is used to display the display effect. In addition, the animation effect in Android is also used to bind data.
If arrays are used and data in XML files is used, the time relationship will not be shown.

Mainactivity

Package org. focuser. spinner;

Import java. util. arraylist;
Import java. util. List;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. motionevent;
Import Android. View. view;
Import Android. View. animation. animation;
Import Android. View. animation. animationutils;
Import Android. widget. adapterview;
Import Android. widget. arrayadapter;
Import Android. widget. Button;
Import Android. widget. edittext;
Import Android. widget. spinner;
Import Android. widget. textview;

Public class mainactivity extends activity {
Private string [] array = {"Kobe", "James", "o'neill", "Jordan "};
Private textview = NULL;
Private spinner = NULL;
Private edittext = NULL;
Private button button1 = NULL;
Private button button2 = NULL;
Private arrayadapter <string> adapter = NULL;
Private list <string> allcou;
Animation animation;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
// Obtain the control object
Textview = (textview) findviewbyid (R. Id. textview );
Spinner = (spinner) findviewbyid (R. Id. spinner );
Edittext = (edittext) findviewbyid (R. Id. edittext );
Button1 = (button) findviewbyid (R. Id. button1 );
Button2 = (button) findviewbyid (R. Id. button2 );
Allcou = new arraylist <string> ();
For (INT I = 0; I <array. length; I ++)
{
Allcou. Add (array [I]);
}
Button1.setonclicklistener (New button. onclicklistener (){

@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
String newarray = edittext. gettext (). tostring ();
For (INT I = 0; I <Adapter. getcount (); I ++)
{
If (newarray. Equals (adapter. getitem (I )))
{
Return;
}
}
If (! Newarray. Equals ("")
{
Adapter. Add (newarray );
Int position = Adapter. getposition (newarray );
Spinner. setselection (position );
Edittext. settext ("");
}
}
});
Button2.setonclicklistener (New button. onclicklistener (){

@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
If (spinner. getselecteditem (). tostring ()! = NULL)
{
Adapter. Remove (spinner. getselecteditem (). tostring ());
Edittext. settext ("");
If (adapter. getcount () = 0)
{
Edittext. settext ("");
}
}
}
});
// Set the adapter
Adapter = new arrayadapter <string> (this, Android. R. layout. simple_spinner_item, allcou );
// Adapter = new arrayadapter <string> (this, Android. R. layout. simple_spinner_item, array );
// Adapter = arrayadapter. createfromresource (this, R. array. array, Android. R. layout. simple_spinner_item );
// Adapter = new arrayadapter <string> (this, R. array. array, Android. R. layout. simple_spinner_item );
// The following two methods are used to display the drop-down list. 1. Custom styles. 2. Use the internal styles of the system.
Adapter. setdropdownviewresource (R. layout. dropdown );
// Adapter. setdropdownviewresource (Android. R. layout. simple_spinner_dropdown_item );
// Bind a blank spinner to the adapter to pass data
Spinner. setadapter (adapter );
// Add the setonitemselectedlistener event to the spinner control.
Spinner. setonitemselectedlistener (new Spinner. onitemselectedlistener (){

@ Override
Public void onitemselected (adapterview <?> Parent, view,
Int position, long ID ){
// Todo auto-generated method stub
// Display the clicked options in textview
// Textview. settext ("You selected:" + array [position]);
Textview. settext ("You selected:" + parent. getitematposition (position ));
// Sets the visibility of the spinner to visible.
Parent. setvisibility (view. Visible );
}

@ Override
Public void onnothingselected (adapterview <?> Parent ){
// Todo auto-generated method stub

}
});
// Write an animation object and obtain the Custom Animation Style
Animation = animationutils. loadanimation (this, R. anim. animation );
Spinner. setontouchlistener (new Spinner. ontouchlistener (){

@ Override
Public Boolean ontouch (view V, motionevent event ){
// Todo auto-generated method stub
// Run the animation Animation
V. startanimation (animation );
// Sets the visibility of the spinner to invisible.
V. setvisibility (view. Invisible );
Return false;
}
});
Spinner. setonfocuschangelistener (new Spinner. onfocuschangelistener (){

@ Override
Public void onfocuschange (view V, Boolean hasfocus ){
// Todo auto-generated method stub
V. startanimation (animation );
// Sets the visibility of the spinner to visible.
V. setvisibility (view. Visible );
}
});
}
}

Next is the main. xml file.

<? 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/textview"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
<Edittext
Android: Id = "@ + ID/edittext"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
<Button
Android: Id = "@ + ID/button1 ″
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "add"
/>
<Button
Android: Id = "@ + ID/button2 ″
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "reduce"
/>
<Spinner
Android: Id = "@ + ID/Spinner"
Android: prompt = "@ string/pormpt_name"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
</Linearlayout>

There is also a custom drop-down menu style dropdown. xml file

<? XML version = "1.0" encoding = "UTF-8"?>
<Textview
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "24sp"
Android: Id = "@ + ID/textview1 ″
Android: singleline = "true"
Style = "? Android: ATTR/spinnerdropdownitemstyle"
>
</Textview>

Haha, this is the main code used to dynamically implement the spinner drop-down menu. The Master will be over. You can take a look at the menu similar to my level. You can take a closer look at it and learn it!

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.