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

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

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

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

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

<?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)目時(shí)建議將文本資源統(tǒng)一定義配置在res下的strings.xml中
android:onClick="begin"/>
</LinearLayout>

Java代碼實(shí)現(xiàn)(通過(guò)線程實(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)度條對(duì)話框(ProgressDialog)
final ProgressDialog pd = new ProgressDialog(this);
pd.setTitle("請(qǐng)稍等");
//設(shè)置對(duì)話進(jìn)度條樣式為水平
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//設(shè)置提示信息
pd.setMessage("正在玩命下載中......");
//設(shè)置對(duì)話進(jìn)度條顯示在屏幕頂部(方便截圖)
pd.getWindow().setGravity(Gravity.TOP);
pd.setMax(100);
pd.show();//調(diào)用show方法顯示進(jìn)度條對(duì)話框
//使用匿名內(nèi)部類(lèi)實(shí)現(xiàn)線程并啟動(dòng)
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)度完成時(shí)對(duì)話框消失
}
}).start();
}
}

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

相關(guān)文章

最新評(píng)論