android--Menu and progress bar

Source: Internet
Author: User

Xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns: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= "Com.example.chenshuai.test322.UiActivity2"android:orientation= "vertical">    <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "LONG Press trigger context menu"Android:id= "@+id/bt_changan"/>    <!--The progress bar cannot drag the circle -    <ProgressBarAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/pb_1"        />    <!--progress bar Horizontal line -    <ProgressBarAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"style= "? Android:attr/progressbarstylehorizontal"Android:id= "@+id/pb_2"Android:max= "+"android:progress= " the"android:secondaryprogress= "The "        />    <!--The progress bar can be dragged -    <SeekBarAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:max= "+"android:progress= " the"Android:id= "@+id/sbr_1"        />    <!--Star -    <RatingbarAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:numstars= "6"android:rating= "4.5"Android:isindicator= "true"/></LinearLayout>

Java

 Packagecom.example.chenshuai.test322;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;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;/*** Created by Chenshuai on 2016/3/30.*/ Public classUIActivity2extendsappcompatactivity {@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); //progress bar//The final life cycle is the entire class        FinalProgressBar pb_1 =(ProgressBar) Findviewbyid (r.id.pb_1); FinalProgressBar pb_2 =(ProgressBar) Findviewbyid (r.id.pb_2); SeekBar sb_1=(SeekBar) Findviewbyid (r.id.sbr_1); Sb_1.setonseekbarchangelistener (NewSeekbar.onseekbarchangelistener () {@Override Public voidOnprogresschanged (SeekBar SeekBar,intProgressBooleanFromuser)            {pb_2.setprogress (progress); }            //Start dragging@Override Public voidOnstarttrackingtouch (SeekBar SeekBar) {}//End Drag@Override Public voidOnstoptrackingtouch (SeekBar SeekBar) {pb_2.setprogress (seekbar.getprogress ()); if(seekbar.getprogress () = =Seekbar.getmax ()) {                    //set invisible gone not occupying position invisible occupy positionpb_1.setvisibility (View.gone); }                Else                {                    //Visiblepb_1.setvisibility (view.visible);    }            }        }); }    //Menu Context Menu@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,4-1,0, "Sub-menu item One"); M.add (0,4-2,1, "Sub-menu item Two"); M.add (0,4-3,2, "Sub-menu item three"); M.add (0,4-4,3, "Sub-menu item four"); Super. Oncreatecontextmenu (menu, V, menuinfo); } @Override Public Booleanoncontextitemselected (MenuItem 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; }        return Super. oncontextitemselected (item); } @Override//overriding methods for creating options menus     Public BooleanOncreateoptionsmenu (Menu menu) {//Add a menu item//The first type of pure coding method        /*menu.add (0, 1, 0, "add");        Menu.add (0, 2, 2, "delete"); Menu.add (0, 3, 1, "Modify");*/        //The second way to load the menu file//1. Get a menu loaderMenuinflater Menuinflater=Getmenuinflater ();        Menuinflater.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 according to menu items        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.it1:toast.maketext (UIActivity2. This, "triggered removal of a feature", Toast.length_short). Show ();  Break;  CaseR.id.it2:toast.maketext (UIActivity2. This, "triggered add a feature", Toast.length_short). Show ();  Break; }        return Super. onoptionsitemselected (item); }}

android--Menu and progress bar

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.