如何正確的使用ProgressBar與ProgessDialog

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)如何正確的使用ProgressBar與ProgessDialog,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

10年的曲沃網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都營銷網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整曲沃建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)公司從事“曲沃網(wǎng)站設(shè)計(jì)”,“曲沃網(wǎng)站推廣”以來,每個客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

一、ProgressBar

1. 常用類型

1.1 不確定式圓形進(jìn)度條

...

沒有顯示進(jìn)度,可作為過場動畫。有大、中、小三種大小,默認(rèn)為中。

1.2 條形進(jìn)度條

...

帶有顯示進(jìn)度。

1.3 標(biāo)題欄不確定式進(jìn)度條

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarIndeterminateVisibility(true);

在標(biāo)題欄右側(cè)顯示的無顯示進(jìn)度的圓形進(jìn)度條。

1.4 標(biāo)題欄條形進(jìn)度條

requestWindowFeature(Window.FEATURE_PROGRESS);
setProgressBarVisibility(true);

在標(biāo)題欄頂部顯示的條形進(jìn)度條,可通過setProgess(int)設(shè)置當(dāng)前進(jìn)度,最大值為10000。

2. 常用控件屬性

<!--最大顯示進(jìn)度-->
android:max
<!--第一顯示進(jìn)度-->
android:progress
<!--第二顯示進(jìn)度-->
android:secondaryProgress
<!--置是否精確顯示;true為不精確,false為精確-->
android:indeterminate
<!--加載自定義樣式-->
android:progressDrawable

3. 自定義樣式

通過控件的android:progressDrawable屬性引用自定義的drawable文件實(shí)現(xiàn)。一般需定義三個內(nèi)容:背景、第一進(jìn)度、第二進(jìn)度。

范例:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

  <!--背景樣式-->
  <item android:id="@android:id/background">
    <shape>
      <!--圓角-->
      <corners android:radius="10dip" />
      <!--填充色-->
      <solid android:color="#dddddd" />
    </shape>
  </item>

  <!--第二進(jìn)度樣式-->
  <item android:id="@android:id/secondaryProgress">
    <clip>
      <shape>
        <corners android:radius="10dip" />
        <solid android:color="#78bb78" />
      </shape>
    </clip>
  </item>

  <!--第一進(jìn)度樣式-->
  <item android:id="@android:id/progress">
    <clip>
      <shape>
        <corners android:radius="10dip" />
        <solid android:color="#55bb55" />
      </shape>
    </clip>
  </item>

</layer-list>

貼張效果圖:

如何正確的使用ProgressBar與ProgessDialog

4. 關(guān)鍵方法

//設(shè)置第一進(jìn)度
setProgress(int)
//設(shè)置第二進(jìn)度
setSecondaryProgress(int)
//獲取第一進(jìn)度
getProgress()
//獲取第二進(jìn)度
getSecondaryProgress()
//增加或減少第一進(jìn)度
incrementProgressBy(int)
//增加或減少第二進(jìn)度
incrementSecondaryProgressBy(int)
//獲取進(jìn)度最大值
getMax()

5. 范例

布局比較簡單,線性布局,豎直排列,這里就不貼代碼了,直接貼張圖:

如何正確的使用ProgressBar與ProgessDialog

Java:

public class ProgessBarActivity extends Activity implements View.OnClickListener{

  private ProgressBar progressBar;
  private TextView text;
  private Button addFirst;
  private Button addSecond;
  private Button subFirst;
  private Button subSecond;
  private Button reset;
  private int first;
  private int second;
  private int max;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_progess_bar);

    init();

  }

  private void init() {
    progressBar = (ProgressBar) findViewById(R.id.progress_bar);
    text = (TextView) findViewById(R.id.text);
    addFirst = (Button) findViewById(R.id.add_first);
    subFirst = (Button) findViewById(R.id.sub_first);
    addSecond = (Button) findViewById(R.id.add_second);
    subSecond = (Button) findViewById(R.id.sub_second);
    reset = (Button) findViewById(R.id.reset);

    //獲取第一、第二、最大進(jìn)度
    first = progressBar.getProgress();
    second = progressBar.getSecondaryProgress();
    max = progressBar.getMax();

    addFirst.setOnClickListener(this);
    addSecond.setOnClickListener(this);
    subFirst.setOnClickListener(this);
    subSecond.setOnClickListener(this);
    reset.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.add_first:
        //第一進(jìn)度加10
        progressBar.incrementProgressBy(10);
        break;
      case R.id.add_second:
        //第二進(jìn)度加10
        progressBar.incrementSecondaryProgressBy(10);
        break;
      case R.id.sub_first:
        progressBar.incrementProgressBy(-10);
        break;
      case R.id.sub_second:
        progressBar.incrementSecondaryProgressBy(-10);
        break;
      case R.id.reset:
        //重置為初始數(shù)值
        progressBar.setProgress(30);
        progressBar.setSecondaryProgress(60);
        break;
    }
    //更新文本內(nèi)容
    text.setText("第一進(jìn)度為" + (int) (1.0*first/max*100) + "%,第二進(jìn)度為" + (int) (1.0*second/max*100) + "%");
  }
}

二、ProgressDialog

1. 構(gòu)造函數(shù)

ProgressDialog(Context context)
ProgressDialog(Context context, int theme)//theme為對話框樣式

2. 關(guān)鍵方法

//設(shè)置進(jìn)度條樣式
setProgressStyle(int style)
//設(shè)置對話框標(biāo)題
setTitle(String title)
//設(shè)置對話框本文信息
setMessage(CharSequence message)
//設(shè)置對話框圖標(biāo)
setIcon(Drawable d)
//設(shè)置按鈕,whichButton為按鈕類型,text為按鈕名稱,listener為監(jiān)聽器
setButton(int whichButton, CharSequence text, OnClickListener listener)
//顯示對話框
show()

此外,除了這幾個方法,ProgressDialog也可使用上面ProgressBar中介紹的方法。

3. 范例

public class ProgressDialogActivity extends Activity {

  private ProgressDialog proDialog;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_progress_dialog);

    findViewById(R.id.show).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        //新建對話框
        proDialog = new ProgressDialog(ProgressDialogActivity.this);
        //設(shè)置進(jìn)度條樣式
        proDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        //設(shè)置對話框標(biāo)題
        proDialog.setTitle("初識ProgressDialog");
        //設(shè)置提示對話框文本
        proDialog.setMessage("好好學(xué)習(xí),天天向上!");
        //設(shè)置對話框顯示圖標(biāo)
        proDialog.setIcon(R.drawable.ic_launcher);
        //設(shè)置進(jìn)度條最大進(jìn)度,默認(rèn)為10000
        proDialog.setMax(100);
        //設(shè)置初始第一進(jìn)度
        proDialog.incrementProgressBy(30);
        //設(shè)定取消按鈕
        proDialog.setButton(DialogInterface.BUTTON_POSITIVE, "取消", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
          }
        });
        //顯示對話框
        proDialog.show();
      }
    });
  }
}

上述就是小編為大家分享的如何正確的使用ProgressBar與ProgessDialog了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

新聞標(biāo)題:如何正確的使用ProgressBar與ProgessDialog
網(wǎng)頁路徑:http://muchs.cn/article0/gppsio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、關(guān)鍵詞優(yōu)化網(wǎng)站收錄、企業(yè)網(wǎng)站制作網(wǎng)站營銷、全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司