日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android学习笔记之ProgressDialog的使用

發(fā)布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android学习笔记之ProgressDialog的使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?????? 在很多PC軟件或手機軟件中,我們都會看見 “加載中...” 類似的對話框,當(dāng)然,在android應(yīng)用程序中也是如此。如果我們想在android應(yīng)用程序中使用這樣的效果,那么就需要用到ProgressDialog。首先,我們來看一下ProgressDialog這個類。

?????? ProgressDialog類繼承自AlertDialog類,同樣存放在android.app包中。ProgressDialog有兩種形式,一種是圓圈旋轉(zhuǎn)形式,一種是水平進度條形式,選擇哪種形式可以通過以下兩個屬性值來設(shè)定:

?

static?intSTYLE_HORIZONTAL
??????????Creates a ProgressDialog with a horizontal progress bar.
static?intSTYLE_SPINNER
??????????Creates a ProgressDialog with a ciruclar, spinning progress bar.

注意,當(dāng)設(shè)置為水平進度條形式時,進度的取值范圍為0—10000。

?

ProgressDialog的構(gòu)造方法有以下兩種:

?

ProgressDialog(Context?context)
???????????
ProgressDialog(Context?context, int?theme)
???????????

?

?除了構(gòu)造方法外,ProgressDialog還提供的如下的靜態(tài)方法返回ProgressDialog對象:

?

static?ProgressDialogshow(Context?context, CharSequence?title, CharSequence?message)
???????????
static?ProgressDialogshow(Context?context, CharSequence?title, CharSequence?message, boolean?indeterminate)
???????????
static?ProgressDialogshow(Context?context, CharSequence?title, CharSequence?message, boolean?indeterminate, boolean?cancelable)
???????????
static?ProgressDialogshow(Context?context, CharSequence?title, CharSequence?message, boolean?indeterminate, boolean?cancelable, DialogInterface.OnCancelListener?cancelListener)

?

需要留意的是第一個參數(shù)必須是目前運行的Activity的Context。

?

?????? android的ProgressDialog必須要在后臺程序運行完畢前,以dismiss()方法來關(guān)閉取得焦點的對話框,否則程序就會陷入無法終止的無窮循環(huán)中。在線程中,不得有任何更改Context或parent View的任何狀態(tài),文字輸出等時間,因為線程里的Context與View并不屬于parent,兩者之間也沒有關(guān)聯(lián)。

?

我們以下面一個簡單的程序來學(xué)習(xí)ProgressDialog的應(yīng)用:

public class MainActivity extends Activity {private Button button=null;public ProgressDialog dialog=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_main);this.button=(Button)super.findViewById(R.id.button);this.button.setOnClickListener(new OnClickListener() { @Overridepublic void onClick(View v) {final CharSequence strDialogTitle=MainActivity.this.getString(R.string.str_dialog_title);final CharSequence strDialogBody=MainActivity.this.getString(R.string.str_dialog_body);//顯示Progress對話框dialog=ProgressDialog.show(MainActivity.this,strDialogTitle,strDialogBody,true);new Thread(){@Overridepublic void run(){try{//表示后臺運行的代碼段,以暫停3秒代替sleep(3000);}catch (InterruptedException e) {e.printStackTrace();}finally{//卸載dialog對象dialog.dismiss();}}}.start();}});}}


該程序布局管理器僅需一個Button組件(id為button)即可,此處不再給出。

注意,為了代碼更加符合規(guī)范,本程序在strings.xml中定義了如下字符串資源:

<?xml version="1.0" encoding="utf-8"?> <resources><string name="app_name">demo2</string><string name="action_settings">Settings</string><string name="hello_world">Hello world!</string><string name="execute">執(zhí)行</string><string name="str_dialog_title">請稍等片刻</string><string name="str_dialog_body">正在執(zhí)行...</string></resources>


程序運行效果截圖:

?

轉(zhuǎn)載于:https://www.cnblogs.com/dyllove98/archive/2013/06/09/3130042.html

總結(jié)

以上是生活随笔為你收集整理的android学习笔记之ProgressDialog的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。