Android——使用ProgressBar實現進度條
阿新 • • 發佈:2019-02-03
1.ProgressBar簡介
ProgressBar是進度條元件,通常用於向用戶展示某個耗時操作完成的進度,而不讓使用者感覺是程式失去了響應,從而更好地提升使用者介面的友好性。
2.制定ProgressBar顯示風格
(1)大環形進度條
style="?android:attr/progressBarStyleLarge"
(2)小環形進度條
style="?android:attr/progressBarStyleSmall"
(3)水平進度條
style="?android:attr/progressBarStyleHorizontal"
3.ProgressBar的分類
(1)可以精確顯示進度(可以顯示刻度或者百分比)
(2)不可以精確顯示進度(一直轉啊轉,類似於一個過場動畫)
4.ProgressBar的關鍵屬性
5.ProgressBar的關鍵方法
6.ProgressBar和ProgressDialog的基礎使用
佈局檔案 activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="增加" android:id="@+id/add" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="減少" android:id="@+id/reduce" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="重置" android:id="@+id/reset" /> <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/progressBar" android:max="100" android:progress="50" android:secondaryProgress="80"/> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="顯示對話方塊形式的進度條"/> </LinearLayout>
MainActivity,java
import android.app.Activity; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements View.OnClickListener{ private Button add; private Button reduce; private Button reset; private ProgressBar progressBar; private TextView text; private ProgressDialog progressDialog; private Button show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { add = (Button) findViewById(R.id.add); reduce = (Button) findViewById(R.id.reduce); reset = (Button) findViewById(R.id.reset); progressBar = (ProgressBar) findViewById(R.id.progressBar); text = (TextView) findViewById(R.id.text); show = (Button) findViewById(R.id.show); //獲取第一進度 int first = progressBar.getProgress(); //獲取第二進度 int second = progressBar.getSecondaryProgress(); //獲取最大進度 int max = progressBar.getMax(); //顯示初始進度百分比 text.setText("第一進度百分比為 "+ (int)(first/(float)max*100) + "% 第二進度百分比為 " + (int)(second/(float)max*100) + "%"); add.setOnClickListener(this); reduce.setOnClickListener(this); reset.setOnClickListener(this); show.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.add : progressBar.incrementProgressBy(10); progressBar.incrementSecondaryProgressBy(10); break; case R.id.reduce: progressBar.incrementProgressBy(-10); progressBar.incrementSecondaryProgressBy(-10); break; case R.id.reset: progressBar.setProgress(50); progressBar.setSecondaryProgress(80); 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); /** * 設定關於ProcessBar的一些屬性 */ //設定最大進度 progressDialog.setMax(100); //設定初始化已經增長到的進度 progressDialog.incrementProgressBy(50); //進度條是明確顯示進度的,false表示明確顯示進度,反之. progressDialog.setIndeterminate(false); /** * 設定一個確定按鈕 */ progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"當前進度:" + progressDialog.getProgress(),Toast.LENGTH_SHORT).show(); } }); //是否可以通過返回按鈕退出對話方塊 progressDialog.setCancelable(true); //顯示ProgressDialog progressDialog.show(); break; } text.setText("第一進度百分比為 "+ (int)(progressBar.getProgress()/(float)progressBar.getMax()*100) + "% 第二進度百分比為 " + (int)(progressBar.getSecondaryProgress()/(float)progressBar.getMax()*100) + "%"); } }
7.自定義ProgressBar樣式