Android Course---Progress bar and menu learning

Source: Internet
Author: User

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout 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"android:orientation= "Vertical"Tools:context= "Com.hanqi.test5.UIActivity2" > <Button android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "LONG Press trigger context menu"Android:id= "@+id/bt_changan"/> <ProgressBar android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/pb_1"/> <ProgressBar android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"style= "? Android:attr/progressbarstylehorizontal"Android:max= "100"android:progress= "50"android:secondaryprogress= "70"Android:id= "@+id/pb_2"/><!--Max: Max progress: Current progress secondaryprogress: Second progress Value-<ProgressBar android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"style= "@android: Style/widget.progressbar.small"/> <ProgressBar android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"style= "@android: Style/widget.progressbar.large"/> <ProgressBar android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"style= "@android: Style/widget.progressbar.large.inverse"/> <ProgressBar android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"style= "@android: Style/widget.progressbar.inverse"/><!--not draggable-<SeekBar android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:max= "100"android:progress= "20"android:secondaryprogress= "50"Android:id= "@+id/sb_1"/><!--Drag-to-<Ratingbar android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:numstars= "7"android:rating= "3.5"Android:isindicator= "true"/> <!--star progress bar Numstars: Number of stars Rating: current default star Isindicator: whether to allow star rating: True is not modifiable--></linearlayout>
 PackageCom.hanqi.test5;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.ContextMenu;ImportAndroid.view.Menu;ImportAndroid.view.MenuInflater;ImportAndroid.view.MenuItem;ImportAndroid.view.SubMenu;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.ProgressBar;ImportAndroid.widget.SeekBar;ImportAndroid.widget.Toast; Public classUIActivity2extendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.LAYOUT.ACTIVITY_UI2); Button Bt_changan=(Button) Findviewbyid (R.id.bt_changan); Bt_changan.setoncreatecontextmenulistener ( This);/*this represents the current activity, which implements the interface*/        //progress bar//with final, the life cycle is long and the final word is only the current method, and final can prolong the life cycle, or define the member variable        FinalProgressBar pb_1 =(ProgressBar) Findviewbyid (r.id.pb_1); FinalProgressBar pb_2 =(ProgressBar) Findviewbyid (r.id.pb_2); FinalSeekBar sb_1 =(SeekBar) Findviewbyid (r.id.sb_1); Sb_1.setonseekbarchangelistener (NewSeekbar.onseekbarchangelistener () {//onprogresschanged progress bar changes, progress changes will trigger@Override Public voidOnprogresschanged (SeekBar SeekBar,intProgressBooleanFromuser)            {pb_2.setprogress (seekbar.getprogress ()); }            //Onstarttrackingtouch Start dragging@Override Public voidOnstarttrackingtouch (SeekBar SeekBar) {}//Onstoptrackingtouch Stop dragging@Override Public voidOnstoptrackingtouch (SeekBar SeekBar) {pb_2.setprogress (seekbar.getprogress ()); if(seekbar.getprogress () = =Seekbar.getmax ()) {                    //setting is not visiblepb_1.setvisibility (View.gone); /*Invisible (occupied space) visible gone invisible (no space)*/                }                Else{pb_1.setvisibility (view.visible);    }            }        }); } @Override Public voidOncreatecontextmenu (ContextMenu menu, View V, contextmenu.contextmenuinfo menuinfo) {menu.add (0,1,0, "Add"); Menu.add (0,2,1, "Modify"); Menu.add (0, 3, 2, "delete"); submenu M= Menu.addsubmenu (0, 4, 3, "Sub-menu"); M.add (0,41,0, "Sub-menu item 1"); M.add (0,42,1, "Sub-menu item 2"); M.add (0,43,2, "Sub-menu item 3"); M.add (0,44,3, "Sub-menu item 4");//menuinflater mi =getmenuinflater ();//mi.inflate (r.menu.mymenu,menu);        Super. Oncreatecontextmenu (menu, V, menuinfo); } @Override Public Booleanoncontextitemselected (MenuItem item) {Switch(Item.getitemid ()) { CaseR.id.it_2:toast.maketext (UIActivity2. This, "triggered add 1 feature", Toast.length_short). Show ();  Break;  CaseR.id.it_1:toast.maketext (UIActivity2. This, "triggered removal of 1 features", Toast.length_short). Show ();  Break; }        return Super. oncontextitemselected (item); } @Override//overriding methods for creating options menus     Public BooleanOncreateoptionsmenu (Menu menu) {//Add a menu item//Pure Encoding Method//menu.add (0,1,0, "add");//menu.add (0,2,2, "delete");//menu.add (0,3,1, "modify"); //Load Menu File mode//1. Get the menu loaderMenuinflater mi =Getmenuinflater ();        Mi.inflate (R.menu.mymenu,menu); return Super. Oncreateoptionsmenu (menu); } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Toast.maketext (Uiactivity2.this, "The selected menu item is" + Item.gettitle (), Toast.length_short). Show (); //respond to different functions depending on the menu item        Switch(Item.getitemid ()) { Case1: Toast.maketext (UIActivity2. This, "Add feature triggered", Toast.length_short). Show ();  Break;  Case2: Toast.maketext (UIActivity2. This, "triggered delete function", Toast.length_short). Show ();  Break;  Case3: Toast.maketext (UIActivity2. This, "triggered the modification function", Toast.length_short). Show ();  Break;  CaseR.id.it_2:toast.maketext (UIActivity2. This, "triggered add 1 feature", Toast.length_short). Show ();  Break;  CaseR.id.it_1:toast.maketext (UIActivity2. This, "triggered removal of 1 features", Toast.length_short). Show ();  Break; }        return Super. onoptionsitemselected (item); }}

Mymenu.xml

<?xml version= "1.0" encoding= "Utf-8"? ><menu xmlns:android= "Http://schemas.android.com/apk/res/android" >    <item android:id= "@+id/it_1" android:title= "Delete 1"        android:icon= "@drawable/delete" > </item>    <item android:id= "@+id/it_2" android:title= "Add 1" android:icon= "@drawable/open" ></item ></menu>

Effect Implementation diagram:

Android Course---Progress bar and menu learning

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.