本文實(shí)例講述了Android開(kāi)發(fā)中ProgressDialog簡(jiǎn)單用法。分享給大家供大家參考,具體如下:
在海倫等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站建設(shè)、成都網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需搭建網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,成都全網(wǎng)營(yíng)銷(xiāo),成都外貿(mào)網(wǎng)站制作,海倫網(wǎng)站建設(shè)費(fèi)用合理。
網(wǎng)上一般對(duì)進(jìn)度條的示例都是如何顯示,沒(méi)有在任務(wù)結(jié)束如何關(guān)閉的文章,參考其他文章經(jīng)過(guò)試驗(yàn)之后把整套進(jìn)度條顯示的簡(jiǎn)單示例如下:
建立android工程等工作都略去,Google一下就可以了。
下面來(lái)介紹主要的Activity
ProgressBarDemo.java
package com.lveyo.android.demo.progressbar; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.TextView; public class ProgressBarDemo extends Activity { private TextView statusTextView; private Button beginBtn; private ProgressDialog progressDialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); statusTextView = (TextView)findViewById(R.id.status); beginBtn = (Button)findViewById(R.id.beginBtn); setListener(); } /** * 用Handler來(lái)更新UI */ private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { //關(guān)閉ProgressDialog progressDialog.dismiss(); //更新UI statusTextView.setText("Completed!"); }}; /** * 點(diǎn)擊按鈕事件listener */ private void setListener(){ beginBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //顯示ProgressDialog progressDialog = ProgressDialog.show(ProgressBarDemo.this, "Loading...", "Please wait...", true, false); //新建線程 new Thread(){ @Override public void run() { //需要花時(shí)間計(jì)算的方法 Calculation.calculate(4); //向handler發(fā)消息 handler.sendEmptyMessage(0); }}.start(); } }); } }
package com.lveyo.android.demo.progressbar; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.TextView; public class ProgressBarDemo extends Activity { private TextView statusTextView; private Button beginBtn; private ProgressDialog progressDialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); statusTextView = (TextView)findViewById(R.id.status); beginBtn = (Button)findViewById(R.id.beginBtn); setListener(); } /** * 用Handler來(lái)更新UI */ private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { //關(guān)閉ProgressDialog progressDialog.dismiss(); //更新UI statusTextView.setText("Completed!"); }}; /** * 點(diǎn)擊按鈕事件listener */ private void setListener(){ beginBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //顯示ProgressDialog progressDialog = ProgressDialog.show(ProgressBarDemo.this, "Loading...", "Please wait...", true, false); //新建線程 new Thread(){ @Override public void run() { //需要花時(shí)間計(jì)算的方法 Calculation.calculate(4); //向handler發(fā)消息 handler.sendEmptyMessage(0); }}.start(); } }); } }
Calculation.java
package com.lveyo.android.demo.progressbar; /** * 示意方法 * @author lveyo * */ public class Calculation { public static void calculate(int sleepSeconds){ try { Thread.sleep(sleepSeconds * 1000); } catch (Exception e) { // TODO: handle exception } } }
package com.lveyo.android.demo.progressbar; /** * 示意方法 * @author lveyo * */ public class Calculation { public static void calculate(int sleepSeconds){ try { Thread.sleep(sleepSeconds * 1000); } catch (Exception e) { // TODO: handle exception } } }
main.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/status" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/beginBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="begin" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/status" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/beginBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="begin" /> </LinearLayout>
在android中,通常我們無(wú)法在單獨(dú)的線程中更新UI,而要在主線程中,這也就是為什么我們要使用 Handler了,當(dāng)handler收到消息中,它會(huì)把它放入到隊(duì)列中等待執(zhí)行,通常來(lái)說(shuō)這會(huì)很快被執(zhí)行。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
分享文章:Android開(kāi)發(fā)中ProgressDialog簡(jiǎn)單用法示例
瀏覽地址:http://jinyejixie.com/article6/ppsgog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、響應(yīng)式網(wǎng)站、網(wǎng)站制作、移動(dòng)網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
全網(wǎng)營(yíng)銷(xiāo)推廣知識(shí)