Android UI component (1)-buttons with progress bars and androidui

Source: Internet
Author: User

Android UI component (1)-buttons with progress bars and androidui

Android UI component (1)-buttons with progress bars



This section introduces:

This series is an advanced series following the Android UI component instance list, in which we will further learn

For Android UI components, we recommend that you read the complete series of UI component instances in this series of front-line learning courses to learn how to use basic components;

Of course, you can also directly learn this series! Okay. Let's get started with the first section! This section demonstrates the following:

Button with progress bar! I believe you can see this on the 360 mobile ASSISTANT:


In this section, you must click the button below to display the progress.



:





Prerequisites:

1. Some Properties of the progress bar:

Background: Set the background image

Max: sets the maximum value of the progress bar.

Progress: Set the progress bar value

Style = "? Android: attr/progressBarStyleHorizontal ": defines the style of the progress bar in the horizontal direction.

ProgressDrawable: if you do not want to use the system's default Progress, you can define one by yourself. This resource file is

To call our own Progress icon, we usually create a. xml file under drawable and use layer-list to group it.

Weave these icons.


2. Handler related methods:

UI operations are either performed in the main thread or handler. Do not operate the UI components directly in the new thread. An exception is reported!

HandleMessage (msg): The main method that Handle needs to override. use msg. what to judge the ID code and perform corresponding operations.

SendEmptyMessage (0x123): sends an empty message to handle. The ID code is 0x123.

SendEmptyMessageDelayed (0x321,500); sends an empty message to handler with an ID code of 0x321. The message is sent after a delay of 500 milliseconds.




Engineering Analysis:




Related code:

MainActivity. java

Package com. jay. uidemo. progressbuttondemo; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. view; import android. widget. button; import android. widget. progressBar; import android. widget. textView; public class MainActivity extends Activity {int I = 0; ProgressBar progressBar = null; Button downLoadBtn = null; Handler handler = new Handler (){ Public void handleMessage (Message msg) {switch (msg. what) {case 0x123: downLoadBtn. setClickable (false); I + = 20; progressBar. setProgress (I); if (I! = 100) {handler. sendEmptyMessageDelayed (0x123,500); downLoadBtn. setText (I + "%");} else if (I = 100) {downLoadBtn. setText ("download completed"); handler. sendEmptyMessageDelayed (0x321,500);} break; case 0x321: downLoadBtn. setText ("open"); downLoadBtn. setClickable (true); downLoadBtn. setBackgroundResource (R. drawable. aa_button_after); handler. sendEmptyMessageDelayed (0x110); break; case 0 x: progressBar. setProgress (0); downLoadBtn. setBackgroundResource (R. drawable. btn_selector); downLoadBtn. setText ("Download"); default: break ;};};@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); TextView tx = (TextView) findViewById (R. id. texttitle); progressBar = (ProgressBar) findViewById (R. id. progressBar); downLoadBtn = (Button) findViewById (R. id. downLoadBtn); downLoadBtn. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {I = 0; handler. sendEmptyMessage (0x123 );}});}}


Btn_selctor.xml in drawable

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:state_pressed="false" android:drawable="@android:color/transparent"></item>    <item android:state_pressed="true" android:drawable="@drawable/aa_button_gray_pressed"></item></selector>


Progress_selctor.xml in drawable

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">    <item android:id="@android:id/progress">        <clip android:gravity="left"            android:clipOrientation="horizontal"            android:drawable="@drawable/aa_button_normal"/>    </item></layer-list>

Activity_main.xml:

<RelativeLayout 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/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/activity_v Ertical_margin "tools: context =" com. jay. uidemo. progressbuttondemo. mainActivity "> <TextView android: id =" @ + id/texttitle "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "button with a graph entry"/> <RelativeLayout android: layout_width = "fill_parent" android: layout_height = "50dp" android: gravity = "bottom" android: layout_centerHorizontal = "true" android: layout_centerVertical = "true"> <ProgressBar android: id = "@ + id/progressBar" style = "? Android: attr/progressBarStyleHorizontal "android: layout_width =" fill_parent "android: layout_height =" fill_parent "android: background =" @ drawable/strong "android: max =" 100 "android: progress = "0" android: progressDrawable = "@ drawable/progress_selector"/> <Button android: id = "@ + id/downLoadBtn" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: text = "Download" android: layout_centerHorizontal = "true" android: layout_centerVertical = "true" android: background = "@ drawable/btn_selector"/> </RelativeLayout>


Download source code:

Http://pan.baidu.com/s/1hql9qOc








For Android, each item in ListView has a progress bar. How can I update different progress bars? Help

The following describes how to use the handle object and callback message mechanism:
Android. OS. Handler
Handler is responsible for sending and processing messages in android. Its main purposes include:
1) send a message as planned or execute a Runnanble (using the POST method );
2) put messages sent from other threads into the message queue to avoid thread conflicts (often used in updating UI threads)
By default, Handler accepts the message loop instances in the current thread (using Handler (loophtling), Handler (loophtling, Handler. callback callback) can specify a thread). At the same time, a message queue can be distributed and processed by multiple objects in the current thread (in the UI thread, the system has an Activity to process, you can initiate several Handler operations ). When Handler is instantiated, logoff can be any thread. As long as there is a Handler pointer, any thread can also sendMessage. Handler does not process messages concurrently. A logoff reads the next Message only after processing a Message. Therefore, the processing of the Message is a blocking (handleMessage () method, and there should be no time-consuming operations, the time-consuming operation can be executed in other threads. After the operation is completed, the Message is sent (by using the sendMessges method), and then the handleMessage () is used to update the UI ).

Handle class usage in android
If we directly put the processing function in OnCreate or OnStart of Activity when processing download or other tasks that require long execution, the entire Activity will not respond during execution, if the time is too long, the program will be suspended. Handler puts these functions in a separate thread for execution, which is independent from Activity.

When you click a button, If you perform a time-consuming operation, poor processing will lead to a system crash and poor user experience, while Android goes further, if any Acitivity does not respond for more than five seconds, it will be forcibly disabled. Therefore, we need to start another thread to handle long-time operations, and the main thread will not be affected, after a time-consuming operation is completed, the message is sent to the main thread, and the main thread then processes the message accordingly. Handler is used for message transmission and asynchronous processing between threads.

In easy language, how do I set button 1? Then, the progress bar starts to go. After the progress bar is completed, a message box is displayed, prompting that the task has been completed?


Remove button events



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.