Android ProgressDialog使用示例
進度條用于顯示任務的進度。例如。當你從互聯網上上傳或下載的東西,這更好地顯示下載進度/上傳給用戶。
在Android中有一類叫做ProgressDialog,允許創建進度條。為了做到這一點,需要實例化這個類的一個對象。其語法如下:
ProgressDialog progress = new ProgressDialog(this);
現在,可以設置此對話框的某些屬性。比如,它的風格,文本等
progress.setMessage("Downloading Music :) "); progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progress.setIndeterminate(true);
除了這些方法,ProgressDialog類中還提供的其它方法:
Sr. NO | 標題與描述 |
---|---|
1 | getMax() 此方法返回進度的最大值 |
2 | incrementProgressBy(int diff) 此方法增加了進度條由值作為參數傳遞的區別 |
3 | setIndeterminate(boolean indeterminate) 此方法設置進度指示確定或不確定 |
4 | setMax(int max) 此方法設置進度對話框的最大值 |
5 | setProgress(int value) 此方法用于更新對話框進度某些特定的值 |
6 | show(Context context, CharSequence title, CharSequence message) 這是一個靜態方法,用來顯示進度對話框 |
示例
這個例子說明使用對話框水平進度,事實上這是一個進度條。它在按下按鈕時顯示進度條。
為了測試這個例子,需要按照以下步驟開發應用程序后,在實際設備上運行。
Steps | 描述 |
---|---|
1 | 使用Android Studio創建Android應用程序,并將其命名為ProgressDialogDemo。在創建這個項目時,確保目標SDK和編譯在Android SDK最新版本和使用更高級別的API |
2 | 修改src/MainActivity.java文件中添加進度代碼顯示對話框進度 |
3 | 修改res/layout/activity_main.xml文件中添加相應的XML代碼 |
4 | 修改res/values/string.xml文件,添加一個消息作為字符串常量 |
5 | 運行應用程序并選擇運行Android設備,并在其上安裝的應用并驗證結果。 |
以下是修改后的主活動文件的內容 src/com.yiibai.progressdialog/MainActivity.java.
package com.example.progressdialog;import com.example.progressdialog.R;
import android.os.Bundle; import android.app.Activity; import android.app.ProgressDialog; import android.view.Menu; import android.view.View;
public class MainActivity extends Activity {
private ProgressDialog progress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progress = new ProgressDialog(this); }
public void open(View view){ progress.setMessage("Downloading Music :) "); progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //progress.setIndeterminate(true); progress.show();
final int totalProgressTime = 100;
final Thread t = new Thread(){
@Override public void run(){
int jumpTime = 0; while(jumpTime < totalProgressTime){ try { sleep(200); jumpTime += 5; progress.setProgress(jumpTime); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
} }; t.start();
} @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }</pre>
修改 res/layout/activity_main.xml 的內容如下
<RelativeLayout xmlns:android="<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="150dp" android:onClick="open" android:text="@string/download_button" />
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="19dp" android:text="@string/download_text" android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout></pre>
修改 res/values/string.xml 以下內容
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">ProgressDialog</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="download_button">Download</string> <string name="download_text">Press the button to download music</string> </resources>
這是默認的 AndroidManifest.xml 文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.yiibai.progressdialog.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
</application>
</manifest></pre>
讓我們試著運行ProgressDialogDemo應用程序。假設你已經連接實際的Android移動設備到計算機。啟動應用程序之前,會顯示如下窗口,選擇要運行的 Android應用程序的選項。
![]()
選擇移動設備作為一個選項,然后查看移動設備顯示如下界面:
![]()
只需按下按鈕,啟動進度條。按下后,如下面的屏幕顯示:
![]()
它會不斷地自我更新,幾秒鐘后,出現如下圖:
![]()
示例代碼下載: http://pan.baidu.com/s/1qW9IElQ