自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android 中的進(jìn)度條示例 ProgressDialog

移動(dòng)開發(fā)
進(jìn)度條用于顯示任務(wù)的進(jìn)度。例如。當(dāng)你從互聯(lián)網(wǎng)上上傳或下載的東西,這更好地顯示下載進(jìn)度/上傳給用戶。在Android中有一類叫做ProgressDialog,允許創(chuàng)建進(jìn)度條。為了做到這一點(diǎn),需要實(shí)例化這個(gè)類的一個(gè)對(duì)象。

進(jìn)度條用于顯示任務(wù)的進(jìn)度。例如。當(dāng)你從互聯(lián)網(wǎng)上上傳或下載的東西,這更好地顯示下載進(jìn)度/上傳給用戶。

在Android中有一類叫做ProgressDialog,允許創(chuàng)建進(jìn)度條。為了做到這一點(diǎn),需要實(shí)例化這個(gè)類的一個(gè)對(duì)象。其語法如下:

  1. ProgressDialog progress = new ProgressDialog(this); 

現(xiàn)在,可以設(shè)置此對(duì)話框的某些屬性。比如,它的風(fēng)格,文本等

  1. progress.setMessage("Downloading Music :) "); 
  2. progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
  3. progress.setIndeterminate(true); 

除了這些方法,ProgressDialog類中還提供的其它方法:

Sr. NO 標(biāo)題與描述
1 getMax()
此方法返回進(jìn)度的***值
2 incrementProgressBy(int diff)
此方法增加了進(jìn)度條由值作為參數(shù)傳遞的區(qū)別
3 setIndeterminate(boolean indeterminate)
此方法設(shè)置進(jìn)度指示確定或不確定
4 setMax(int max)
此方法設(shè)置進(jìn)度對(duì)話框的***值
5 setProgress(int value)
此方法用于更新對(duì)話框進(jìn)度某些特定的值
6 show(Context context, CharSequence title, CharSequence message)
這是一個(gè)靜態(tài)方法,用來顯示進(jìn)度對(duì)話框

示例

這個(gè)例子說明使用對(duì)話框水平進(jìn)度,事實(shí)上這是一個(gè)進(jìn)度條。它在按下按鈕時(shí)顯示進(jìn)度條。

為了測試這個(gè)例子,需要按照以下步驟開發(fā)應(yīng)用程序后,在實(shí)際設(shè)備上運(yùn)行。

Steps 描述
1 使用Android Studio創(chuàng)建Android應(yīng)用程序,并將其命名為ProgressDialogDemo。在創(chuàng)建這個(gè)項(xiàng)目時(shí),確保目標(biāo)SDK和編譯在Android SDK***版本和使用更高級(jí)別的API
2 修改src/MainActivity.java文件中添加進(jìn)度代碼顯示對(duì)話框進(jìn)度
3 修改res/layout/activity_main.xml文件中添加相應(yīng)的XML代碼
4 修改res/values/string.xml文件,添加一個(gè)消息作為字符串常量
5 運(yùn)行應(yīng)用程序并選擇運(yùn)行Android設(shè)備,并在其上安裝的應(yīng)用并驗(yàn)證結(jié)果。

以下是修改后的主活動(dòng)文件的內(nèi)容 src/com.yiibai.progressdialog/MainActivity.java.

  1. package com.example.progressdialog; 
  2.  
  3. import com.example.progressdialog.R; 
  4.  
  5. import android.os.Bundle; 
  6. import android.app.Activity; 
  7. import android.app.ProgressDialog; 
  8. import android.view.Menu; 
  9. import android.view.View; 
  10.  
  11. public class MainActivity extends Activity { 
  12.  
  13.    private ProgressDialog progress; 
  14.    @Override 
  15.    protected void onCreate(Bundle savedInstanceState) { 
  16.       super.onCreate(savedInstanceState); 
  17.       setContentView(R.layout.activity_main); 
  18.       progress = new ProgressDialog(this); 
  19.    } 
  20.  
  21.  
  22.    public void open(View view){ 
  23.       progress.setMessage("Downloading Music :) "); 
  24.       progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
  25.       //progress.setIndeterminate(true); 
  26.       progress.show(); 
  27.  
  28.    final int totalProgressTime = 100
  29.  
  30.    final Thread t = new Thread(){ 
  31.  
  32.    @Override 
  33.    public void run(){ 
  34.   
  35.       int jumpTime = 0
  36.       while(jumpTime < totalProgressTime){ 
  37.          try { 
  38.             sleep(200); 
  39.             jumpTime += 5
  40.             progress.setProgress(jumpTime); 
  41.          } catch (InterruptedException e) { 
  42.            // TODO Auto-generated catch block 
  43.            e.printStackTrace(); 
  44.          } 
  45.  
  46.       } 
  47.  
  48.    } 
  49.    }; 
  50.    t.start(); 
  51.  
  52.    } 
  53.    @Override 
  54.    public boolean onCreateOptionsMenu(Menu menu) { 
  55.       // Inflate the menu; this adds items to the action bar if it is present. 
  56.       getMenuInflater().inflate(R.menu.main, menu); 
  57.       return true
  58.    } 

修改 res/layout/activity_main.xml 的內(nèi)容如下

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.    xmlns:tools="http://schemas.android.com/tools" 
  3.    android:layout_width="match_parent" 
  4.    android:layout_height="match_parent" 
  5.    android:paddingBottom="@dimen/activity_vertical_margin" 
  6.    android:paddingLeft="@dimen/activity_horizontal_margin" 
  7.    android:paddingRight="@dimen/activity_horizontal_margin" 
  8.    android:paddingTop="@dimen/activity_vertical_margin" 
  9.    tools:context=".MainActivity" > 
  10.  
  11.    <Button 
  12.       android:id="@+id/button1" 
  13.       android:layout_width="wrap_content" 
  14.       android:layout_height="wrap_content"  
  15.       android:layout_alignParentTop="true" 
  16.       android:layout_centerHorizontal="true" 
  17.       android:layout_marginTop="150dp" 
  18.       android:onClick="open" 
  19.       android:text="@string/download_button" /> 
  20.  
  21.    <TextView 
  22.       android:id="@+id/textView1" 
  23.       android:layout_width="wrap_content" 
  24.       android:layout_height="wrap_content" 
  25.       android:layout_alignParentRight="true" 
  26.       android:layout_alignParentTop="true" 
  27.       android:layout_marginTop="19dp" 
  28.       android:text="@string/download_text" 
  29.       android:textAppearance="?android:attr/textAppearanceLarge" /> 
  30.  
  31. </RelativeLayout> 

修改 res/values/string.xml 以下內(nèi)容

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.    <string name="app_name">ProgressDialog</string> 
  4.    <string name="action_settings">Settings</string> 
  5.    <string name="hello_world">Hello world!</string> 
  6.    <string name="download_button">Download</string> 
  7.    <string name="download_text">Press the button to download music</string> 
  8. </resources> 

這是默認(rèn)的 AndroidManifest.xml 文件

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3.    package="com.yiibai.progressdialog" 
  4.    android:versionCode="1" 
  5.    android:versionName="1.0" > 
  6.  
  7.    <uses-sdk 
  8.       android:minSdkVersion="8" 
  9.       android:targetSdkVersion="17" /> 
  10.  
  11.    <application 
  12.       android:allowBackup="true" 
  13.       android:icon="@drawable/ic_launcher" 
  14.       android:label="@string/app_name" 
  15.       android:theme="@style/AppTheme" > 
  16.       <activity 
  17.          android:name="com.yiibai.progressdialog.MainActivity" 
  18.          android:label="@string/app_name" > 
  19.          <intent-filter> 
  20.             <action android:name="android.intent.action.MAIN" /> 
  21.  
  22.             <category android:name="android.intent.category.LAUNCHER" /> 
  23.          </intent-filter> 
  24.       </activity> 
  25.    </application> 
  26.  
  27. </manifest> 

讓我們試著運(yùn)行ProgressDialogDemo應(yīng)用程序。假設(shè)你已經(jīng)連接實(shí)際的Android移動(dòng)設(shè)備到計(jì)算機(jī)。啟動(dòng)應(yīng)用程序之前,會(huì)顯示如下窗口,選擇要運(yùn)行的 Android應(yīng)用程序的選項(xiàng)。

Anroid

選擇移動(dòng)設(shè)備作為一個(gè)選項(xiàng),然后查看移動(dòng)設(shè)備顯示如下界面:

Android Progress Dialog Tutorial

只需按下按鈕,啟動(dòng)進(jìn)度條。按下后,如下面的屏幕顯示:

Android ProgressDialog

它會(huì)不斷地自我更新,幾秒鐘后,出現(xiàn)如下圖:

And	roid ProgressDialog


示例代碼下載:http://pan.baidu.com/s/1qW9IElQ
責(zé)任編輯:林師授 來源: yiibai
相關(guān)推薦

2015-01-12 09:30:54

Android進(jìn)度條ProgressDia

2015-07-31 11:19:43

數(shù)字進(jìn)度條源碼

2012-07-31 09:53:33

HTML5進(jìn)度條

2010-01-25 18:27:54

Android進(jìn)度條

2011-07-05 15:16:00

QT 進(jìn)度條

2011-02-22 14:53:41

titlebar標(biāo)題欄Android

2024-08-06 14:29:37

2013-03-12 10:35:06

CSS 3

2024-12-02 09:37:51

2015-08-03 11:39:20

擬物化進(jìn)度條

2012-01-17 13:58:17

JavaSwing

2009-06-06 18:54:02

JSP編程進(jìn)度條

2023-12-11 17:15:05

應(yīng)用開發(fā)波紋進(jìn)度條ArkUI

2024-07-25 08:55:47

進(jìn)度條水缸進(jìn)度動(dòng)畫效果

2024-06-13 08:15:00

2022-03-07 15:40:51

Linux軟件代碼

2017-03-14 15:09:18

AndroidView圓形進(jìn)度條

2009-08-17 15:48:47

C# WinForm進(jìn)

2009-08-17 14:41:47

C#進(jìn)度條實(shí)現(xiàn)

2009-12-25 17:58:12

WPF進(jìn)度條
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)