Android Development-drop-down list

Source: Internet
Author: User

The drop-down list of the Android interface mainly uses the <Spinner> component, where the creation of the array is mainly divided into static creation and dynamic creation, the following is the main write static array creation; Part of the code is as follows.

xml:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
xmlns:tools= "Http://schemas.android.com/tools"
android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
tools:context= ". Mainactivity ">

<textview
android:id= "@+id/txt01"
android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>
<spinner
android:id= "@+id/sp01"
android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:entries= "@array/city"
android:layout_torightof= "@+id/txt01"
/>
<textview
android:id= "@+id/txt02"
android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "You chose to be:"
android:layout_below= "@+id/txt01"
android:layout_margin= "50DP"
/>

</RelativeLayout>

Resource File Code:

<resources>
<string-array name= "City" >
<item > Beijing </item>
<item > Shanghai </item>
<item > Guangzhou </item>
<item > Shenzhen </item>
<item > Chengdu </item>
<item > XI ' an </item>
<item > Hong Kong </item>
        
</string-array>
    
</resources>

Java code:


import Android.os.Bundle;
import android.app.Activity;
import Android.view.Menu;
import Android.view.View;
import Android.widget.AdapterView;
import Android.widget.AdapterView.OnItemSelectedListener;
import Android.widget.Spinner;
import Android.widget.TextView;

Public class Mainactivity extends Activity {
private TextView txt2;
private Spinner SP1;
Private StringBuilder result=new StringBuilder ();
@Override
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
txt2= (TextView) This.findviewbyid (r.id.txt02);
sp1= (Spinner) This.findviewbyid (R.ID.SP01);
final string[] cities_list=getresources (). Getstringarray (r.array.city);
final String Wy=txt2.gettext (). toString ();
Sp1.setonitemselectedlistener (New Onitemselectedlistener () {

@Override
Public void onitemselected (adapterview<?> arg0, View arg1,
int arg2, long arg3) {
//TODO auto-generated method stub
result.delete (0, Result.length ());
String CI=CITIES_LIST[ARG2];
Result.append (wy.tostring () +ci.tostring ());
Txt2.settext (result);
            }

@Override
Public void onnothingselected (adapterview<?> arg0) {
//TODO auto-generated method stub
                
            }
        });
        
    }


@Override
Public Boolean Oncreateoptionsmenu (Menu menu) {
//inflate the menu; This adds items to the action bar if it is present.
getmenuinflater (). Inflate (R.menu.main, menu);
return true;
    }
    
}

Android Development-drop-down list

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.