欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 中通過實(shí)現(xiàn)線程更新Progressdialog (對話進(jìn)度條)

 更新時間:2016年11月23日 09:25:30   作者:潘侯爺  
這篇文章主要介紹了Android 中通過實(shí)現(xiàn)線程更新Progressdialog (對話進(jìn)度條)的相關(guān)資料,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下

作為開發(fā)者我們需要經(jīng)常站在用戶角度考慮問題,比如在應(yīng)用商城下載軟件時,當(dāng)用戶點(diǎn)擊下載按鈕,則會有下載進(jìn)度提示頁面出現(xiàn),現(xiàn)在我們通過線程休眠的方式模擬下載進(jìn)度更新的演示,如圖(這里為了截圖方便設(shè)置對話進(jìn)度條位于屏幕上方):

layout界面代碼(僅部署一個按鈕):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下載"http://真正項(xiàng)目時建議將文本資源統(tǒng)一定義配置在res下的strings.xml中
android:onClick="begin"/>
</LinearLayout>

Java代碼實(shí)現(xiàn)(通過線程實(shí)現(xiàn)模擬下載進(jìn)度更新):

public class ProgressBarDemo extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progressbar);
}
public void begin(View v) {
//實(shí)例化進(jìn)度條對話框(ProgressDialog)
final ProgressDialog pd = new ProgressDialog(this);
pd.setTitle("請稍等");
//設(shè)置對話進(jìn)度條樣式為水平
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//設(shè)置提示信息
pd.setMessage("正在玩命下載中......");
//設(shè)置對話進(jìn)度條顯示在屏幕頂部(方便截圖)
pd.getWindow().setGravity(Gravity.TOP);
pd.setMax(100);
pd.show();//調(diào)用show方法顯示進(jìn)度條對話框
//使用匿名內(nèi)部類實(shí)現(xiàn)線程并啟動
new Thread(new Runnable() {
int initial = 0;//初始下載進(jìn)度
@Override
public void run() {
while(initial<pd.getMax()){//設(shè)置循環(huán)條件
pd.setProgress(initial+=40);//設(shè)置每次完成40
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
pd.dismiss();//進(jìn)度完成時對話框消失
}
}).start();
}
}

以上所述是小編給大家介紹的Android 中通過實(shí)現(xiàn)線程更新Progressdialog (對話進(jìn)度條),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論