Dynamically Add/delete a menu to/from Android

Source: Internet
Author: User

Steps:

 

Step 1: Create a producer Android project: spinnerdemo.

Step 2: Compile the subcategory of the activity: spinnerdemo. The program code is as follows:

 

Package com. a3gs. spinner;

Import java. util. arraylist;

Import java. util. List;

Import Android. App. activity;

Import Android. OS. Bundle;

Import Android. View. view;

Import Android. widget .*;

 

Public class spinnerdemo extends activity {

Private textview mytv;

Private spinner mysp;

Private edittext myet;

Private button addbtn, delbtn;

Private final string [] items = {"Beijing", "Shanghai", "Tianjin", "Fuzhou "};

Private arrayadapter <string> adapter;

Private list <string> allitems;

/** Called when the activity is first created .*/

@ Override

Public void oncreate (bundle savedinstancestate ){

Super. oncreate (savedinstancestate );

Setcontentview (R. layout. Main );

Mytv = (textview) findviewbyid (R. Id. mytv );

Myet = (edittext) findviewbyid (R. Id. myet );

Addbtn = (button) findviewbyid (R. Id. addbtn );

Delbtn = (button) findviewbyid (R. Id. delbtn );

Mysp = (spinner) findviewbyid (R. Id. myspinner );

Mysp. setvisibility (view. Visible );

Allitems = new arraylist <string> ();

For (INT I = 0; I <items. length; I ++ ){

Allitems. Add (items [I]);

}

Adapter = new arrayadapter <string> (this, Android. R. layout. simple_spinner_item, allitems );

Adapter. setdropdownviewresource (Android. R. layout. simple_spinner_dropdown_item );

Mysp. setadapter (adapter );

Mysp. setonitemselectedlistener (new Spinner. onitemselectedlistener (){

@ Override

Public void onitemselected (adapterview <?> Arg0, view arg1,

Int arg2, long arg3 ){

// Todo auto-generated method stub

Mytv. settext ("You selected:" + mysp. getselecteditem (). tostring ());

}

@ Override

Public void onnothingselected (adapterview <?> Arg0 ){

// Todo auto-generated method stub

}

});

Addbtn. setonclicklistener (New button. onclicklistener (){

@ Override

Public void onclick (view v ){

// Todo auto-generated method stub

String ettext = myet. gettext (). tostring ();

Int Len = Adapter. getcount ();

// Check whether the added object already exists

For (INT I = 0; I <Len; I ++ ){

If (ettext. Equals (adapter. getitem (I ))){

Return;

}

}

If (! Ettext. Equals ("")){

Adapter. Add (ettext );

Int position = Adapter. getposition (ettext );

Mysp. setselection (position );

Myet. settext ("");

}

}

});

Delbtn. setonclicklistener (New button. onclicklistener (){

@ Override

Public void onclick (view v ){

// Todo auto-generated method stub

If (mysp. getselecteditem ()! = NULL ){

Adapter. Remove (mysp. getselecteditem (). tostring ());

Myet. settext ("");

}

If (adapter. getcount () = 0 ){

Myet. settext ("");

}

}

});

}

}

 

Step 3: Modify Res/layout/Main. XML with the following 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"

>

<Textview

Android: Id = "@ + ID/mytv"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "@ string/hello"

/>

<Edittext

Android: Id = "@ + ID/myet"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

/>

<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: Orientation = "horizontal"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

>

<Button

Android: Id = "@ + ID/addbtn"

Android: layout_width = "160sp"

Android: layout_height = "wrap_content"

Android: text = "@ string/btn_text1"

/>

<Button

Android: Id = "@ + ID/delbtn"

Android: layout_width = "160sp"

Android: layout_height = "wrap_content"

Android: text = "@ string/btn_text2"

/>

</Linearlayout>

<Spinner

Android: Id = "@ + ID/myspinner"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

/>

</Linearlayout>

Step 4: Modify Res/layout/spinner_dropdown.xml with the following code:

 

<? XML version = "1.0" encoding = "UTF-8"?>

<Textview

Xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: Id = "@ + ID/text"

Android: layout_width = "wrap_content"

Android: layout_height = "25sp"

Android: singleline = "true"

Style = "? Android: ATTR/spinnerdropdownitemstyle"

/>

 

Extended learning

 

Setdropdownviewresource is mainly used to set the drop-down menu style that appears after the user clicks the spinner. In addition to the previous example, the custom mode is used to change the textview content, Android also provides two basic styles:

Android. R. layout. simple_spinner_item: drop-down menu of textview.

Android. R. layout. simple_spinner_dropdown_item: In addition to textview, there is a radio drop-down menu on the right.

 

View simple_spinner_drop, and down_item.xml In the android source code. The content is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>

<Textview

Xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: Id = "@ Android: ID/text1"

Android: layout_width = "fill_parent"

Android: layout_height = "? Android: ATTR/listpreferreditemheight"

Android: singleline = "true"

Style = "? Android: ATTR/spinnerdropdownitemstyle"

/>

After the custom modifications are made, layout applies to the spinner:

<? XML version = "1.0" encoding = "UTF-8"?>

<Textview

Xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: Id = "@ Android: ID/text1"

Android: layout_width = "fill_parent"

Android: layout_height = "12sp"

Android: singleline = "true"

Style = "? Android: ATTR/spinnerdropdownitemstyle"

Android: textsize = "10sp"

/>

 

 

Example:

 

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.