進度條ProgressBar及ProgressDialog


Main代碼

1
package processdemo.example.administrator.processbardemo; 2 3 import android.app.Dialog; 4 import android.app.ProgressDialog; 5 import android.content.DialogInterface; 6 import android.os.Bundle; 7 import android.support.v7.app.AppCompatActivity; 8 import android.util.Log; 9 import android.view.View; 10 import android.view.Window; 11 import android.widget.Button; 12 import android.widget.ProgressBar; 13 import android.widget.TextView; 14 import android.widget.Toast; 15 16 public class MainActivity extends AppCompatActivity implements View.OnClickListener{ 17 /*ProgressBar 18 簡介:ProgressBar是進度條組件,通常用於向用戶展示某個耗時操作完成的進度,而不讓用戶感覺是程序失去了響應,從而更好地提升用戶界面的友好性 19 課程目標: 20 1、制定ProgressBar顯示風格(系統默認) 21 2、ProgressBar的分類 22 水平進度條,能精確顯示,圓圈進度條,不精確顯示 23 3、標題上ProgressBar的設置 24 4、ProgressBar的關鍵屬性 25 5、ProgressBar的關鍵方法 26 6、ProgressDiglog的基礎使用 27 7、自定義ProgressBar樣式*/ 28 29 private ProgressBar progressBar3; 30 private Button show; 31 private Button add; 32 private Button res; 33 private Button reset; 34 private TextView textView; 35 private ProgressDialog progressDialog; 36 @Override 37 protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 // 啟用窗口特征,啟用帶進度的進度條和不帶進度的進度條, 40 requestWindowFeature(Window.FEATURE_PROGRESS); 41 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 42 setContentView(R.layout.activity_main); 43 setProgressBarVisibility(true); 44 setProgressBarIndeterminateVisibility(false); 45 // 最大值為Max=10000; 46 //setProgress(600); 47 init(); 48 49 } 50 51 private void init() { 52 ; 53 progressBar3= (ProgressBar) findViewById(R.id.progressBar3); 54 show= (Button) findViewById(R.id.show); 55 add= (Button) findViewById(R.id.add); 56 res= (Button) findViewById(R.id.res); 57 reset= (Button) findViewById(R.id.reset); 58 textView= (TextView) findViewById(R.id.textView); 59 int first=progressBar3.getProgress();/*獲取第一進度*/ 60 int second=progressBar3.getSecondaryProgress();/*獲取第二進度*/ 61 int max=progressBar3.getMax();/*獲取最大進度*/ 62 textView.setText("第一進度條百分比"+(int)((first/(float)max)*100)+"%"+"第二進度條百分比"+(int)(second/(float)max*100)+"%"); 63 64 add.setOnClickListener(this); 65 res.setOnClickListener(this); 66 reset.setOnClickListener(this); 67 show.setOnClickListener(this); 68 69 } 70 71 72 @Override 73 public void onClick(View v) { 74 switch (v.getId()){ 75 case R.id.add: 76 progressBar3.incrementProgressBy(10); 77 progressBar3.incrementSecondaryProgressBy(10); 78 break; 79 case R.id.res: 80 progressBar3.incrementProgressBy(-10); 81 progressBar3.incrementSecondaryProgressBy(-10); 82 break; 83 case R.id.reset: 84 progressBar3.setProgress(50); 85 progressBar3.setSecondaryProgress(50); 86 break; 87 case R.id.show: 88 // 新建ProgressDialog對象 89 progressDialog=new ProgressDialog(MainActivity.this); 90 // 設置顯示風格 91 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 92 // 設置標題 93 progressDialog.setTitle("慕課網"); 94 // 設置對話框的內容 95 progressDialog.setMessage("歡迎大家支持慕課網"); 96 // 設置圖標 97 progressDialog.setIcon(R.mipmap.ic_launcher); 98 99 /*設置關於進度條的一些屬性*/ 100 // 設置最大進度 101 progressDialog.setMax(100); 102 // 設置初始化已經增長的進度 103 progressDialog.incrementProgressBy(50); 104 // 設置進度條明確顯示進度 105 progressDialog.setIndeterminate(false); 106 107 /* 設定一個確定按鈕*/ 108 progressDialog.setButton(DialogInterface.BUTTON_POSITIVE,"確定", new Dialog.OnClickListener() { 109 @Override 110 public void onClick(DialogInterface dialog, int which) { 111 Toast.makeText(MainActivity.this,"歡迎大家支持慕課網",Toast.LENGTH_SHORT).show(); 112 } 113 }); 114 // 是否可以通過返回按鈕來取消對話框 115 progressDialog.setCancelable(true); 116 // 顯示ProgressDialog 117 progressDialog.show(); 118 } 119 textView.setText("第一進度條百分比"+(int)((progressBar3.getProgress()/(float)progressBar3.getMax())*100)+"%"+"第二進度條百分比"+(int)(progressBar3.getSecondaryProgress()/(float)progressBar3.getMax()*100)+"%"); 120 } 121 }

 

layout中activity_main.xml代碼

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="processdemo.example.administrator.processbardemo.MainActivity">


    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="112dp" />

    <ProgressBar
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar2"
        android:layout_centerVertical="true"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="256dp" />

    <ProgressBar
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="80"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar3"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/progressBar2"
        android:layout_marginBottom="81dp"
        android:layout_alignTop="@+id/res"
        android:progressDrawable="@layout/progress"/><!--progressDrawable改變樣式-->

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar4"
        android:layout_above="@+id/reset"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/增加"
        android:id="@+id/add"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/減少"
        android:id="@+id/res"
        android:layout_above="@+id/add"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/重置"
        android:id="@+id/reset"

        android:layout_alignBottom="@+id/progressBar3"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="進度"
        android:id="@+id/textView"
        android:layout_below="@+id/progressBar"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/顯示進度條"
        android:id="@+id/show"
        android:layout_below="@+id/progressBar2"
        android:layout_alignParentEnd="true" />
</RelativeLayout>
   <!-- ProgressBar關鍵屬性
    1.android:max -&#45;&#45;最大顯示進度
    2.android:progress -&#45;&#45;第一顯示進度
    3.android:secondaryProgress-&#45;&#45;第二顯示進度
    4.android:isdeterminate -&#45;&#45;設置是否精確顯示(false為精確,true為不精確)-->

 

 

layout中progress.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />
            <gradient
                android:startColor="#76cf76"
                android:centerColor="#125912"
                android:centerY="0.75"
                android:endColor="#212621"
                android:angle="270"
                />
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                    android:startColor="#80b51638"
                    android:centerColor="#80a47d1a"
                    android:centerY="0.75"
                    android:endColor="#a066c3a7"
                    android:angle="270"
                    />
            </shape>
        </clip>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                    android:startColor="#a38c1c"
                    android:centerColor="#55a6b6"
                    android:centerY="0.75"
                    android:endColor="#b59826"
                    android:angle="270"
                    />
            </shape>
        </clip>
    </item>

</layer-list>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM