Android攻城狮 progressBarprogressDialog
生活随笔
收集整理的這篇文章主要介紹了
Android攻城狮 progressBarprogressDialog
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ProgressBar的關鍵屬性
android:max="100" ——最大顯示進度
android:progress="50" ——第一顯示進度
android:secondaryProgress="80" ——第二顯示進度
android:indeterminate="false" 設置是否模糊顯示(indeterminate:模糊的、不明確的)
true表示不精確顯示進度,false表示精確顯示進度
----------------------------
關鍵方法:
(1)setProgress(int) 設置第一進度
(2)setSecondaryProgress(int) 設置第二進度
(3)getProgress() 獲取第一進度
(4)getSecondaryProgress() 獲取第二進度
(5)incrementProgress(int) 增加或減少第一進度
(6)incrementSecondaryProgress(int) 增加或減少第二進度
(7)getMax() 獲取最大進度
1 /進度條: 監聽和ProgressDialog對話框 2 //控制進度條的增加,減少,重置 3 4 5 public class MainActivity extends Activity implements OnClickListener { 6 private ProgressBar progressBar, progressBar2; 7 private TextView textView; 8 private Button add, reduce, reset; 9 private ProgressDialog progressDialog; 10 private Button button; 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 //-----------標題欄的進度條---------------- 16 //啟用窗口特征,啟用帶進度和不帶進度的進度條 17 requestWindowFeature(Window.FEATURE_PROGRESS); 18 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 19 setContentView(R.layout.fragment_main); 20 //顯示兩種進度條 21 setProgressBarVisibility(true); 22 setProgressBarIndeterminateVisibility(true); 23 //max=10000 24 setProgress(8000); 25 //--------------------progressBar------------------------ 26 27 progressBar = (ProgressBar) findViewById(R.id.progressBar1); 28 progressBar2 = (ProgressBar) findViewById(R.id.progressBar2); 29 textView = (TextView) findViewById(R.id.textView1); 30 add = (Button) findViewById(R.id.add); 31 reduce = (Button) findViewById(R.id.reduce); 32 reset = (Button) findViewById(R.id.reset); 33 34 button = (Button) findViewById(R.id.button1); 35 button.setOnClickListener(this); 36 37 // 獲取第一條進度條的進度----正在播放時的 38 int first = progressBar2.getProgress(); 39 40 // 獲取第二條進度條的進度----緩沖 41 int second = progressBar2.getSecondaryProgress(); 42 // 獲取進度條最大進度 43 int max = progressBar2.getMax(); 44 textView.setText("第一進度百分百:" 45 + (int) (first / (float) max * 100) + "%" 46 + (int) (second / (float) max * 100) + "%"); 47 add.setOnClickListener(this); 48 reduce.setOnClickListener(this); 49 reset.setOnClickListener(this); 50 } 51 52 @Override 53 public void onClick(View v) { 54 // TODO Auto-generated method stub 55 switch (v.getId()) { 56 case R.id.add: { 57 // 增加第一進度第二進度10刻度 58 progressBar2.incrementProgressBy(10); 59 progressBar2.incrementSecondaryProgressBy(10); 60 break; 61 } 62 63 case R.id.reduce: { 64 // 減少第一進度第二進度10刻度 65 progressBar2.incrementProgressBy(-10); 66 progressBar2.incrementSecondaryProgressBy(-10); 67 break; 68 } 69 70 case R.id.reset: { 71 progressBar2.setProgress(50); 72 progressBar2.setSecondaryProgress(80); 73 break; 74 } 75 case R.id.button1: { 76 // 頁面風格: 77 // 1.新建progressDialog對象 78 progressDialog = new ProgressDialog(MainActivity.this); 79 progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL); 80 // 設置標題 81 progressDialog.setTitle("下載;"); 82 // 設置對話框里的文字信息 83 progressDialog.setMessage("歡迎大家下載"); 84 // 設置圖標 85 progressDialog.setIcon(R.drawable.ic_launcher); 86 87 // progreBar的一些屬性 88 progressDialog.setMax(100); 89 // 設置初始化的進度 90 progressDialog.incrementProgressBy(50); 91 // 進度條是明確顯示進度的 92 progressDialog.setIndeterminate(false); 93 // 設置一個確定按鈕 94 progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "確定", 95 new DialogInterface.OnClickListener() { 96 97 @Override 98 public void onClick(DialogInterface dialog, int which) { 99 // TODO Auto-generated method stub 100 Toast.makeText(MainActivity.this, "hahahha,你來了", 1) 101 .show(); 102 } 103 }); 104 // 是否可以通過返回按鈕退出對話框 105 progressDialog.setCancelable(true); 106 // 顯示progressDialog 107 progressDialog.show(); 108 break; 109 } 110 } 111 textView.setText("第一進度百分百:" 112 + (int) (progressBar2.getProgress()/ (float) progressBar2.getMax() * 100) 113 + "%" 114 + (int) (progressBar2.getSecondaryProgress()/ (float) progressBar2.getMax() * 100) + "%"); 115 } 116 117 }
自定義進度條:
在main.xml文件下的<Progress>的屬性style改成:style="@android:style/Widget.ProgressBar.Horizontal" 然后,按住Ctrl,左擊"@android:style/Widget.ProgressBar.Horizontal",就進入 styles.xml,這時將看到<style name="Widget.ProgressBar.Horizontal">,在其屬性中有<item name="progressDrawable">@drawable/progress_horizontal</item> ,再次按住Ctrl并左擊該屬性,就進入了進度條樣式的文件。創建完樣式后,在 main.xml中的<Progress>里面添加屬性: android:progressDrawable="@drawable/progress_bar" 就會覆蓋原有的樣式,實現了自定義進度條樣式的效果。1 /進度條: 監聽和ProgressDialog對話框 2 //控制進度條的增加,減少,重置 3 4 5 public class MainActivity extends Activity implements OnClickListener { 6 private ProgressBar progressBar, progressBar2; 7 private TextView textView; 8 private Button add, reduce, reset; 9 private ProgressDialog progressDialog; 10 private Button button; 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 //-----------標題欄的進度條---------------- 16 //啟用窗口特征,啟用帶進度和不帶進度的進度條 17 requestWindowFeature(Window.FEATURE_PROGRESS); 18 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 19 setContentView(R.layout.fragment_main); 20 //顯示兩種進度條 21 setProgressBarVisibility(true); 22 setProgressBarIndeterminateVisibility(true); 23 //max=10000 24 setProgress(8000); 25 //--------------------progressBar------------------------ 26 27 progressBar = (ProgressBar) findViewById(R.id.progressBar1); 28 progressBar2 = (ProgressBar) findViewById(R.id.progressBar2); 29 textView = (TextView) findViewById(R.id.textView1); 30 add = (Button) findViewById(R.id.add); 31 reduce = (Button) findViewById(R.id.reduce); 32 reset = (Button) findViewById(R.id.reset); 33 34 button = (Button) findViewById(R.id.button1); 35 button.setOnClickListener(this); 36 37 // 獲取第一條進度條的進度----正在播放時的 38 int first = progressBar2.getProgress(); 39 40 // 獲取第二條進度條的進度----緩沖 41 int second = progressBar2.getSecondaryProgress(); 42 // 獲取進度條最大進度 43 int max = progressBar2.getMax(); 44 textView.setText("第一進度百分百:" 45 + (int) (first / (float) max * 100) + "%" 46 + (int) (second / (float) max * 100) + "%"); 47 add.setOnClickListener(this); 48 reduce.setOnClickListener(this); 49 reset.setOnClickListener(this); 50 } 51 52 @Override 53 public void onClick(View v) { 54 // TODO Auto-generated method stub 55 switch (v.getId()) { 56 case R.id.add: { 57 // 增加第一進度第二進度10刻度 58 progressBar2.incrementProgressBy(10); 59 progressBar2.incrementSecondaryProgressBy(10); 60 break; 61 } 62 63 case R.id.reduce: { 64 // 減少第一進度第二進度10刻度 65 progressBar2.incrementProgressBy(-10); 66 progressBar2.incrementSecondaryProgressBy(-10); 67 break; 68 } 69 70 case R.id.reset: { 71 progressBar2.setProgress(50); 72 progressBar2.setSecondaryProgress(80); 73 break; 74 } 75 case R.id.button1: { 76 // 頁面風格: 77 // 1.新建progressDialog對象 78 progressDialog = new ProgressDialog(MainActivity.this); 79 progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL); 80 // 設置標題 81 progressDialog.setTitle("下載;"); 82 // 設置對話框里的文字信息 83 progressDialog.setMessage("歡迎大家下載"); 84 // 設置圖標 85 progressDialog.setIcon(R.drawable.ic_launcher); 86 87 // progreBar的一些屬性 88 progressDialog.setMax(100); 89 // 設置初始化的進度 90 progressDialog.incrementProgressBy(50); 91 // 進度條是明確顯示進度的 92 progressDialog.setIndeterminate(false); 93 // 設置一個確定按鈕 94 progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "確定", 95 new DialogInterface.OnClickListener() { 96 97 @Override 98 public void onClick(DialogInterface dialog, int which) { 99 // TODO Auto-generated method stub 100 Toast.makeText(MainActivity.this, "hahahha,你來了", 1) 101 .show(); 102 } 103 }); 104 // 是否可以通過返回按鈕退出對話框 105 progressDialog.setCancelable(true); 106 // 顯示progressDialog 107 progressDialog.show(); 108 break; 109 } 110 } 111 textView.setText("第一進度百分百:" 112 + (int) (progressBar2.getProgress()/ (float) progressBar2.getMax() * 100) 113 + "%" 114 + (int) (progressBar2.getSecondaryProgress()/ (float) progressBar2.getMax() * 100) + "%"); 115 } 116 117 }
?
?轉載于:https://www.cnblogs.com/my334420/p/6709128.html
總結
以上是生活随笔為你收集整理的Android攻城狮 progressBarprogressDialog的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《华胥引》下载地址
- 下一篇: 基于Android的手机点名签到学生请假