"Android" drop-down list, drag bar, star rating bar and label Text Touch event

Source: Internet
Author: User

Drop-down lists, drag bars, and star rating bars are some of the basic components of Android.

Write a small program to illustrate its usage.

Such as:


When the drop-down list has a value selected, the corresponding prompt message pops up and which value is selected. The star rating strip also gets the number of stars selected by the user.

The label text is touched to show that it is touched.

The slider bar, when dragged, displays its value in the label text. Because, if the pop-up message shows its sliding value, it is not smooth.

First, change the res\values\strings.xml to the following code:

<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name=    "App_name" > Progress bar and other components </ string>    <string name= "action_settings" >Settings</string>    <string name= "TextView1" > Drop-down list:</string>    <string name= "TextView2" > Drag bar:</string> <string name=    "TEXTVIEW3" > Star rating Bar:</string></resources>
After that, create a new Array.xml file in Res\values and write the following code, which is the option to populate the drop-down list:

<?xml version= "1.0" encoding= "Utf-8"?><resources> <string-array    name= "Spinner1" >        < item> options 1</item>        <item> options 2</item>        <item> options 3</item>    </ String-array></resources>
Then, in the Res\layout\activity_main.xml layout, layout ideas are as follows:


So the code for Activity_main.xml is given the ID of the component to be manipulated as follows:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <linearlayout Android:layout_widt H= "Match_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" > <TextV Iew android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:text = "@string/textview1"/> <spinner android:id= "@+id/spinner1" android:layout_width= "Wrap_ Content "android:layout_height=" wrap_content "android:entries=" @array/spinner1 "/> </linear layout> <textview android:id= "@+id/textview2" android:layout_width= "Wrap_content" Android:la        yout_height= "Wrap_content" android:text= "@string/textview2"/> <seekbar android:id= "@+id/seekbar1" Android:layout_width="Match_parent" android:layout_height= "wrap_content"/> <textview android:layout_width= "wrap_content "Android:layout_height=" wrap_content "android:text=" @string/textview3 "/> <ratingbar androi D:id= "@+id/ratingbar1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Andro id:numstars= "5"/></linearlayout>
The star rating Bar is numstars The maximum number of stars in the attribute.

Then add listeners to the individual components in Mainactivity.java:

Package Com.progresscomponent;import Android.os.bundle;import Android.app.activity;import android.view.Menu;import Android.view.motionevent;import Android.view.view;import Android.view.view.ontouchlistener;import Android.widget.adapterview;import Android.widget.adapterview.onitemselectedlistener;import Android.widget.ratingbar;import Android.widget.ratingbar.onratingbarchangelistener;import Android.widget.SeekBar ; Import Android.widget.seekbar.onseekbarchangelistener;import Android.widget.spinner;import Android.widget.textview;import Android.widget.toast;public class Mainactivity extends Activity {private Spinner Spinner1;private TextView textview2;private SeekBar seekbar1;private ratingbar ratingBar1; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); /Get each component spinner1= (Spinner) Findviewbyid (r.id.spinner1); textView2 = (TextView) Findviewbyid (R.ID.TEXTVIEW2); seekBar1 = (SeekBar) Findviewbyid (R.ID.SEEKBAR1); ratingbar1= (Ratingbar) Findviewbyid (R.ID.RATINGBAR1);//change listener for drop-down list Spinner1.setonitemselectedlistener (new Onitemselectedlistener () {@Overridepublic void onitemselected (adapterview<?> parent, View arg1,int Pos, Long Arg3 ) {String result=parent.getitematposition (POS). toString (); Toast.maketext (Mainactivity.this, "drop-down list is changed," +result+ "is selected", Toast.length_long). Show (); @Overridepublic void onnothingselected (adapterview<?> arg0) {//TODO auto-generated Method stub}});// The label text is touched by the listener Textview2.setontouchlistener (new Ontouchlistener () {@Overridepublic Boolean OnTouch (View arg0, Motionevent arg1) {toast.maketext (Mainactivity.this, "Drag bar" These words are touched!) ", Toast.length_long). Show (); return false;}); /slide bar dragged Listener Seekbar1.setonseekbarchangelistener (new Onseekbarchangelistener () {@Overridepublic void Onstoptrackingtouch (SeekBar arg0) {//TODO auto-generated method stub} @Overridepublic void Onstarttrackingtouch ( SeekBar arg0) {//TODO auto-generated method stub} @Overridepublic void Onprogresschanged (SeekBar arg0, int result, Boolean arg2) {//TODO auto-generated method Stubtextview2.settext ("slider, Current value:" +result);}); /star rating Bar scored listener Ratingbar1.setonratingbarchangelistener (new Onratingbarchangelistener () {@Overridepublic void Onratingchanged (Ratingbar arg0, float result, Boolean arg2) {Toast.maketext (mainactivity.this), "star rating bar is scored with a value of:" +result, Toast.length_long). Show ();}}); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}



"Android" drop-down list, drag bar, star rating bar and label Text Touch event

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.