Android開(kāi)發(fā)中ProgressDialog簡(jiǎn)單用法示例
本文實(shí)例講述了Android開(kāi)發(fā)中ProgressDialog簡(jiǎn)單用法。分享給大家供大家參考,具體如下:
網(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中ProgressDialog與ProgressBar的使用詳解
- 實(shí)例詳解Android自定義ProgressDialog進(jìn)度條對(duì)話框的實(shí)現(xiàn)
- Android自定義ProgressDialog進(jìn)度等待框
- Android ProgressBar進(jìn)度條和ProgressDialog進(jìn)度框的展示DEMO
- Android 自定義ProgressDialog進(jìn)度條對(duì)話框用法詳解
- Android編程實(shí)現(xiàn)加載等待ProgressDialog的方法
- Android 中通過(guò)實(shí)現(xiàn)線程更新Progressdialog (對(duì)話進(jìn)度條)
- Android自定義ProgressDialog加載圖片
- Android ProgressDialog使用總結(jié)
- Android中ProgressDialog的dismiss()與cancel()方法的區(qū)別
- android自定義ProgressDialog加載效果
相關(guān)文章
Android 簡(jiǎn)單封裝獲取驗(yàn)證碼倒計(jì)時(shí)功能
倒計(jì)時(shí)效果相信大家都不陌生,我們可以使用很多種方法去實(shí)現(xiàn)此效果,這里自己采用 CountDownTimer 定時(shí)器簡(jiǎn)單封裝下此效果,方便我們隨時(shí)調(diào)用。下面小編給大家分享android驗(yàn)證碼倒計(jì)時(shí)封裝方法,感興趣的朋友一起看看吧2018-01-01
listview的上滑下滑監(jiān)聽(tīng),上下滑監(jiān)聽(tīng)隱藏頂部選項(xiàng)欄的實(shí)例
下面小編就為大家分享一篇listview的上滑下滑監(jiān)聽(tīng),上下滑監(jiān)聽(tīng)隱藏頂部選項(xiàng)欄的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android RecyclerView設(shè)置下拉刷新的實(shí)現(xiàn)方法
這篇文章主要介紹了Android RecyclerView設(shè)置下拉刷新的實(shí)現(xiàn)方法,希望通過(guò)本文通過(guò)SwipeRefreshLayout方式實(shí)現(xiàn)下拉刷新,需要的朋友可以參考下2017-10-10
Android OkHttp 結(jié)合php 多圖片上傳實(shí)例
本篇文章主要介紹了Android OkHttp 結(jié)合php 多圖片上傳實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Android ListView和Adapter數(shù)據(jù)適配器的簡(jiǎn)單介紹
這篇文章主要介紹了Android ListView和Adapter數(shù)據(jù)適配器的簡(jiǎn)單介紹,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Android自定義引導(dǎo)玩轉(zhuǎn)ViewPager的方法詳解
這篇文章主要給大家介紹了關(guān)于Android自定義引導(dǎo)玩轉(zhuǎn)ViewPager的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Android自定義View實(shí)現(xiàn)兩種二維碼的掃描效果
這篇文章主要為大家詳細(xì)介紹了Android如何自定義View實(shí)現(xiàn)兩種二維碼的掃描效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
Android ExpandableListView雙層嵌套實(shí)現(xiàn)三級(jí)樹(shù)形菜單
這篇文章主要介紹了Android ExpandableListView雙層嵌套實(shí)現(xiàn)三級(jí)樹(shù)形菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11

