Drop-down list box spinner

Source: Internet
Author: User

In the androidapp Application, the drop-down list box Spinner frequency is quite high, if you are unfamiliar with Spinner , you will not the Select in HTML is unfamiliar, their role is the same, are more than select one. We need to learn the basic controls in Android drop-down list box Spinner.

First, the Design login window

Open the "res/layout/activity_main.xml" file.

 1 activity 1 spinner 1 buttons button 2 All controls are from form widgets

2, new province.xml pieces.

Create a new province.xml file in the "res/values" location .

(1) The location of the Province.xml file is as follows:

Province.xml content is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>

<resources>


<string-array name= "Provarray" >

<item> Henan Province </item>

<item> Hebei province </item>

<item> Shandong Province </item>

<item> Shanxi Province </item>

</string-array>

</resources>

3.Open the activity_main.xml file.

We have modified the auto-generated code to the following code, specifically:

(1 spinner id modified to province

Note: android:entries= "@array/provarray" , which means spinner items using province.xml Span style= "font-family: ' The song Body '; The provarray

(2)buttion 's ID is modified to saveand its text is modified to " Save " .

<spinner

Android:id= "@+id/province"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

Android:layout_alignparentright= "true"

Android:layout_centervertical= "true"

android:entries= "@array/provarray"/>

<button

Android:id= "@+id/save"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

android:layout_alignleft= "@+id/province"

Android:layout_alignparentbottom= "true"

Android:layout_marginbottom= "76DP"

android:layout_marginleft= "23DP"

android:text= "@string/save"/>

Second, click event

Open the "Src/com.genwoxue.spinnerapp/mainactivity.java" file.

Then enter the following code:

Package COM.EXAMPLE.HW;


Import android.app.Activity;

Import Android.os.Bundle;

Import Android.view.Menu;

Import Android.view.View;

Import Android.view.View.OnClickListener;

Import Android.widget.Button;

Import Android.widget.Spinner;

Import Android.widget.Toast;


public class Mainactivity extends Activity {

Private Button btnsave = null;

Private Spinner spinprovice = null;

@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_main);

Btnsave = (Button) Super.findviewbyid (R.id.save);

Spinprovice = (Spinner) Findviewbyid (r.id.province);

Btnsave.setonclicklistener (New Onclicklistener () {

public void OnClick (View v) {

String SPro = "";

String sinfo = "";

SPro = Spinprovice.getselecteditem (). toString ();

Sinfo = "province:" +spro;

Toast.maketext (Getapplicationcontext (), Sinfo, Toast.length_short). Show ();

}

});

}


@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;

}

}

In the above code, we focus on a light blue background section.

  1 , section ① Part

Import The packages associated with the Spinner.

  2 , section ② Part

Declares a 2 control variable.

  3 , section ③ Part

(1)theFindviewbyid () method completes The capture of 2 controls.

(2)" save " button to add click Listen Event:Btnsave.setonclicklistener (new Saveonclicklistener ()).

  4 , section ④ Part

(1) We create a new anonymous inner class Onclicklistener to enable click event Snooping.

(2)spinner.getselecteditem () Gets the value of the current selection.

(3)toast.maketext (Getapplicationcontext (), Sinfo,toast.length_short). Show () Tips for displaying selections: Shandong Province, for example.


This article is from the "No Water Fish" blog, please be sure to keep this source http://javaqun.blog.51cto.com/10687700/1705472

Drop-down list box spinner

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.