這篇文章將為大家詳細講解有關進度條ProgressBar及ProgressDialog的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
廢話不多說,直接上代碼
Main代碼 package processdemo.example.administrator.processbardemo; import android.app.Dialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ /*ProgressBar 簡介:ProgressBar是進度條組件,通常用于向用戶展示某個耗時操作完成的進度,而不讓用戶感覺是程序失去了響應,從而更好地提升用戶界面的友好性 課程目標: 1、制定ProgressBar顯示風格(系統默認) 2、ProgressBar的分類 水平進度條,能精確顯示,圓圈進度條,不精確顯示 3、標題上ProgressBar的設置 4、ProgressBar的關鍵屬性 5、ProgressBar的關鍵方法 6、ProgressDiglog的基礎使用 7、自定義ProgressBar樣式*/ private ProgressBar progressBar3; private Button show; private Button add; private Button res; private Button reset; private TextView textView; private ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 啟用窗口特征,啟用帶進度的進度條和不帶進度的進度條, requestWindowFeature(Window.FEATURE_PROGRESS); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.activity_main); setProgressBarVisibility(true); setProgressBarIndeterminateVisibility(false); // 最大值為Max=10000; //setProgress(600); init(); } private void init() { ; progressBar3= (ProgressBar) findViewById(R.id.progressBar3); show= (Button) findViewById(R.id.show); add= (Button) findViewById(R.id.add); res= (Button) findViewById(R.id.res); reset= (Button) findViewById(R.id.reset); textView= (TextView) findViewById(R.id.textView); int first=progressBar3.getProgress();/*獲取第一進度*/ int second=progressBar3.getSecondaryProgress();/*獲取第二進度*/ int max=progressBar3.getMax();/*獲取最大進度*/ textView.setText("第一進度條百分比"+(int)((first/(float)max)*100)+"%"+"第二進度條百分比"+(int)(second/(float)max*100)+"%"); add.setOnClickListener(this); res.setOnClickListener(this); reset.setOnClickListener(this); show.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.add: progressBar3.incrementProgressBy(10); progressBar3.incrementSecondaryProgressBy(10); break; case R.id.res: progressBar3.incrementProgressBy(-10); progressBar3.incrementSecondaryProgressBy(-10); break; case R.id.reset: progressBar3.setProgress(50); progressBar3.setSecondaryProgress(50); break; case R.id.show: // 新建ProgressDialog對象 progressDialog=new ProgressDialog(MainActivity.this); // 設置顯示風格 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // 設置標題 progressDialog.setTitle("慕課網"); // 設置對話框的內容 progressDialog.setMessage("歡迎大家支持慕課網"); // 設置圖標 progressDialog.setIcon(R.mipmap.ic_launcher); /*設置關于進度條的一些屬性*/ // 設置最大進度 progressDialog.setMax(100); // 設置初始化已經增長的進度 progressDialog.incrementProgressBy(50); // 設置進度條明確顯示進度 progressDialog.setIndeterminate(false); /* 設定一個確定按鈕*/ progressDialog.setButton(DialogInterface.BUTTON_POSITIVE,"確定", new Dialog.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"歡迎大家支持慕課網",Toast.LENGTH_SHORT).show(); } }); // 是否可以通過返回按鈕來取消對話框 progressDialog.setCancelable(true); // 顯示ProgressDialog progressDialog.show(); } textView.setText("第一進度條百分比"+(int)((progressBar3.getProgress()/(float)progressBar3.getMax())*100)+"%"+"第二進度條百分比"+(int)(progressBar3.getSecondaryProgress()/(float)progressBar3.getMax()*100)+"%"); } }
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 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 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 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 ---最大顯示進度 2.android:progress ---第一顯示進度 3.android:secondaryProgress---第二顯示進度 4.android:isdeterminate ---設置是否精確顯示(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>
關于“進度條ProgressBar及ProgressDialog的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。