Advanced article-user interface: 4. Common components in Android

Source: Internet
Author: User

1. Drop-down menu

In web development, HTML provides a drop-down list implementation that uses the <select> element to implement a drop-down list in which each drop-down list item uses the <option> representation. This is an essential interactive component in web development, and the corresponding implementation in Android is spinner.

Method One:

Static display of Spinner options in resource mode:

<spinner     android:layout_width= "match_parent"    android:layout_height= "wrap_content"    android:id= "@ +id/spinner2 "    android:entries=" @array/spingarr "    />

Data files:

<?xml version= "1.0" encoding= "Utf-8"?><resources> <string-array    name= "Spingarr" >     < Item> Beijing </item>      <item> Shanghai </item>      <item> Guangzhou </item>      <item> Shenzhen </item>      </string-array></resources>

Usage 2:

Dynamically display the Spinner option in code mode to add data to Spinner with the adapter

Package Com.example.testspanner;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 MainActivity Extends Activity {private Spinner spinner;private list<string> data_list;private arrayadapter<string> arr_    Adapter        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.spinner);            Spinner = (spinner) Findviewbyid (R.id.spinner);        Data Data_list = new arraylist<string> ();        Data_list.add ("Beijing");        Data_list.add ("Shanghai");        Data_list.add ("Guangzhou");                Data_list.add ("Shenzhen"); Adapter arr_adapter= New Arrayadapter<string> (this, Android.        R.layout.simple_spinner_item, data_list); Set style Arr_adapter.setdropdownviewresource (Android.        R.layout.simple_spinner_dropdown_item); Load Adapter Spinner. Setadapter (Arr_adapter); }}

Layout file added:

< Spinner      Android:layout_width = "Match_parent"     android:layout_height= "Wrap_content"    android:id= "@+id/spinner"    />

2. Date picker

Btnchoosedata.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {NewDatepickerdialog (mainactivity. This,NewDatepickerdialog.ondatesetlistener () {@Override Public voidOndateset (DatePicker DatePicker,intIintI1,intI2) {System.out.println (String.Format ("%d-%d-%d", I,i1,i2)); }                },2016,1,1). Show (); }        });

Click the button to trigger a calendar selector.

3. Date Picker

Btnchoosedata.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {NewTimepickerdialog (mainactivity. This,NewTimepickerdialog.ontimesetlistener () {@Override Public voidOntimeset (Timepicker Timepicker,intIintI1) {System.out.println (String.Format ("%d:%d", I,I1)); }                },0,0,true). Show ();    }        }); }

Click on the button to trigger a time picker

4. Individual selection

 <RadiogroupAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content">        <RadioButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "New RadioButton1"Android:id= "@+id/rba" />        <RadioButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "New RadioButton2"Android:id= "@+id/rbb" />        <RadioButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "New RadioButton3"Android:id= "@+id/rbc" />        <RadioButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "New RadioButton4"Android:id= "@+id/rbd" />    </Radiogroup>

If you want to determine which option is selected in the radio group:

System.out.println (rba.ischecked ());

5. Multiple options

Just pay attention to multiple selection of objects there is a method oncheckedchanged ();

Advanced article-user interface: 4. Common components in Android

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.