[7 basics of Android]-common effect 2

Source: Internet
Author: User
Tags event listener

Disclaimer: The book "Secrets of Android Application Development", which records the logs of the book, references the relevant code and summary, and has no commercial use, it is completely a record of self-learning, and many problems will inevitably occur in learning just now. If there are any mistakes, please criticize them a lot.

On Sunday, I continued to learn about the secrets of Android Application Development and learned about the common effects of the previous article;

1. progress bar)

The progress bar is a good credential for the user during background program processing to show how the current program is processed and how the progress is. There are two style progress bars in Android: A long progress bar and a circular progress bar. Some programs can also display progress bars in the title bar.

 

When editing the main. xml file in the android program developed by ECLIPSE, a graphical interface is also provided, such:

Instance analysis: click a start button to display the progress of the circle and long progress bar.

 

Key source code:

Main. xml layout file:

<ProgressBar    android:id="@+id/ProgressBar01"style="?android:attr/progressBarStyleHorizontal"    android:layout_width="200dp"    android:layout_height="wrap_content"    android:visibility="gone"  />  <ProgressBar   android:id="@+id/ProgressBar02"        android:layout_width="wrap_content"         android:layout_height="wrap_content"        style="?android:attr/progressBarStyleLarge"        android:max="100"        android:progress="50"        android:secondaryProgress="70"android:visibility="gone"  />

[Note] The key of this instance is the control of progressbar. In the previous example, if the handlemessage () method of the handler instance is used to trigger the handlemessage (Message mesg) method:

// Start execution when the button is pressed. mbutton01.setonclicklistener (New button. onclicklistener () {public void onclick (view v) {m_progressbar.setvisibility (view. visible); // set the progressbar to the visible State m_progressbar2.setvisibility (view. visible); m_progressbar.setmax (100); // sets the maximum m_progressbar.setprogress (0) of progressbar; // sets the current value of progressbar m_progressbar2.setprogress (0 ); // change the value of progressbar through a thread. New thread (New runnable () {public void run () {For (INT I = 0; I <10; I ++) {try {intcounter = (I + 1) * 20; thread. sleep (1000); if (I = 4) {message m = new message (); M. what = activity01.gui _ stop_notifier; activity01.this. mymessagehandler. sendmessage (m); break;} else {message m = new message (); M. what = activity01.gui _ threading_notifier; activity01.this. mymessagehandler. sendmessage (m) ;}} catch (exception e) {e. printstacktrace ();}}}}). start () ;}}) ;}handl Er mymessagehandler = new handler () {public void handlemessage (Message MSG) {Switch (MSG. what) {Case activity01.gui _ stop_notifier: // progressbar is already against m_m_progressbar.setvisibility (view. gone); m_progressbar2.setvisibility (view. gone); thread. currentthread (). interrupt (); break; Case activity01.gui _ threading_notifier: If (! Thread. currentthread (). isinterrupted () {m_progressbar.setprogress (intcounter); // change the current value of progressbar m_progressbar2.setprogress (intcounter); setprogress (intcounter * 100 ); // set a progress bar value setsecondaryprogress (intcounter * 100) in the title bar; // set a progress bar value} break;} super. handlemessage (MSG );}};

Instance effect:

2. Drag a bar (seekbar)

Drag bars are mainly used to adjust certain properties in a program, such as the sound effect size. It is relatively easy to implement the seekbar control in Android, and you only need to listen to the three events of the control:

Value Change (onprogresschanged );

Start dragging (onstarttrackingtouch );

Onstoptrackingtouch );

 

Its Control Configuration is also relatively simple:

<SeekBar android:id="@+id/seek"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:max="100"        android:progress="50"android:secondaryProgress="75" />

:

3. Status Bar prompt (notification, icationicationmanager)

When there is a phone call or short message, a small icon is displayed in the status bar on the top of the phone to show whether the user has handled the news. Icationicationmanager is used to manage the status bar information, while notification is used to process the news information.

The gersystenservice method is used to obtain the icationicationmanager object. The notification object can set attributes such as its content, icon, and title. Then, execute a Notification using the notify y method.

Instance analysis: when a user clicks a button, a notification is sent. This is the prompt message displayed on the status bar at the top of the mobile phone. Expand the status bar and click the news to go to the processing page.

Key source code:

// Initialize the icationicationmanager object m_notificationmanager = (notificationmanager) This. getsystemservice (icationication_service); // obtain the four button objects mbutton1 = (button) This. findviewbyid (R. id. button01); mbutton2 = (button) This. findviewbyid (R. id. button02); mbutton3 = (button) This. findviewbyid (R. id. button03); mbutton4 = (button) This. findviewbyid (R. id. button04); // The transfer content m_intent = new intent (activity01.this, activity02.class) when the notification is clicked; // It mainly sets the class m_pendingintent = pendingintent for the content displayed when the notification is clicked. getactivity (activity01.this, 0, m_intent, 0); // construct the notification object m_notification = new notification (); mbutton1.setonclicklistener (New button. onclicklistener () {public void onclick (view v) {// set the icon m_icationication.icon = R. drawable. img1; // The content displayed when we click notification m_icationication.tickertext = "button1 notification content .......... "; // The default sound m_icationication.defaults = notification. default_sound; // set the notification display parameter m_icationication.setlatesteventinfo (activity01.this, "button1", "button1 notification", m_pendingintent); // it can be interpreted as executing the notification m_icationicationmanager.y y (0, m_notification );}});

The notify () method:

Public void every Y (int id, notification)

Post a notification to be shown in the status bar. if a notification with the same ID has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

Parameters

ID

An identifier for this notification unique within your application.

Notification

A notification object describing what to show the user. Must not be null.

 

Instance:

4. progress bar in the dialog box (progressdialog)

The progress bar in the dialog box. You can set attributes such as icons and content.

 

Instance analysis: Click two buttons to display two progress bars in the dialog box.

Key source code:

// Set mbutton01 event listening mbutton01.setonclicklistener (New button. onclicklistener () {public void onclick (view v) {m_pdialog = new progressdialog (alog); // create a progressdialog object // set the progress bar style to circle and rotate m_pdialog.setprogressstyle (progressdialog. style_spinner); m_pdialog.settitle ("prompt"); // set progressdialog title m_pdialog.setmessage ("this is a circular progress bar dialog box"); // set progressdialog prompt information m_pdialog.seticon (R. drawable. img1); // Set the progressdialog title icon m_pdialog.setindeterminate (false); // set whether the progress bar of progressdialog is ambiguous m_pdialog.setcancelable (true ); // set whether prgoressdialog can cancel m_pdialog.setbutton ("OK", new dialoginterface. onclicklistener () {// set a buttonpublic void onclick (dialoginterface dialog, int which) {dialog. cancel () ;}}); // display the m_ssdialog m_pdialog.show () ;}}); // set the mbutton02 event listener mbutton02.setonclicklistener (New Button. onclicklistener () {public void onclick (view v) {m_count = 0; m_pdialog = new progressdialog (examples_04_24activity.this); // create the progressdiener object m_pdig.alosetprogressstyle (progressdialog. style_horizontal); // set the progress bar style. The style is long ...... m_pdialog.show (); // Let progressdialog display new thread () {public void run () {try {While (m_count <= 100) {// The progress is controlled by the thread. M_pdialog.setprogress (m_count ++); thread. sleep (100);} m_pdialog.cancel ();} catch (interruptedexception e) {m_pdialog.cancel ();}}}. start ();}});

Instance effect:

Today, the learning of the Instance effect is over. For Android, after the layout learning is completed, the basic learning is over. Come on!

 

Related Article

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.