進(jìn)度條ProgressBar及ProgressDialog(實(shí)例)
廢話不多說,直接上代碼
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是進(jìn)度條組件,通常用于向用戶展示某個(gè)耗時(shí)操作完成的進(jìn)度,而不讓用戶感覺是程序失去了響應(yīng),從而更好地提升用戶界面的友好性
課程目標(biāo):
1、制定ProgressBar顯示風(fēng)格(系統(tǒng)默認(rèn))
2、ProgressBar的分類
水平進(jìn)度條,能精確顯示,圓圈進(jìn)度條,不精確顯示
3、標(biāo)題上ProgressBar的設(shè)置
4、ProgressBar的關(guān)鍵屬性
5、ProgressBar的關(guān)鍵方法
6、ProgressDiglog的基礎(chǔ)使用
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);
// 啟用窗口特征,啟用帶進(jìn)度的進(jìn)度條和不帶進(jìn)度的進(jìn)度條,
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();/*獲取第一進(jìn)度*/
int second=progressBar3.getSecondaryProgress();/*獲取第二進(jìn)度*/
int max=progressBar3.getMax();/*獲取最大進(jìn)度*/
textView.setText("第一進(jìn)度條百分比"+(int)((first/(float)max)*100)+"%"+"第二進(jìn)度條百分比"+(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對(duì)象
progressDialog=new ProgressDialog(MainActivity.this);
// 設(shè)置顯示風(fēng)格
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// 設(shè)置標(biāo)題
progressDialog.setTitle("慕課網(wǎng)");
// 設(shè)置對(duì)話框的內(nèi)容
progressDialog.setMessage("歡迎大家支持慕課網(wǎng)");
// 設(shè)置圖標(biāo)
progressDialog.setIcon(R.mipmap.ic_launcher);
/*設(shè)置關(guān)于進(jìn)度條的一些屬性*/
// 設(shè)置最大進(jìn)度
progressDialog.setMax(100);
// 設(shè)置初始化已經(jīng)增長的進(jìn)度
progressDialog.incrementProgressBy(50);
// 設(shè)置進(jìn)度條明確顯示進(jìn)度
progressDialog.setIndeterminate(false);
/* 設(shè)定一個(gè)確定按鈕*/
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE,"確定", new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"歡迎大家支持慕課網(wǎng)",Toast.LENGTH_SHORT).show();
}
});
// 是否可以通過返回按鈕來取消對(duì)話框
progressDialog.setCancelable(true);
// 顯示ProgressDialog
progressDialog.show();
}
textView.setText("第一進(jìn)度條百分比"+(int)((progressBar3.getProgress()/(float)progressBar3.getMax())*100)+"%"+"第二進(jìn)度條百分比"+(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
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="進(jìn)度"
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/顯示進(jìn)度條"
android:id="@+id/show"
android:layout_below="@+id/progressBar2"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<!-- ProgressBar關(guān)鍵屬性
1.android:max ---最大顯示進(jìn)度
2.android:progress ---第一顯示進(jìn)度
3.android:secondaryProgress---第二顯示進(jìn)度
4.android:isdeterminate ---設(shè)置是否精確顯示(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>
以上這篇進(jìn)度條ProgressBar及ProgressDialog(實(shí)例)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
一文詳解無痕埋點(diǎn)在Android中的實(shí)現(xiàn)
很多時(shí)候因?yàn)楫a(chǎn)品都會(huì)要獲取用戶的行為,需要客戶端進(jìn)行相關(guān)的埋點(diǎn),下面這篇文章主要給大家介紹了關(guān)于無痕埋點(diǎn)在Android中實(shí)現(xiàn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
Android 實(shí)現(xiàn)WebView點(diǎn)擊圖片查看大圖列表及圖片保存功能
這篇文章主要介紹了Android 實(shí)現(xiàn)WebView點(diǎn)擊圖片查看大圖列表及圖片保存功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
實(shí)例講解Android應(yīng)用開發(fā)中Fragment生命周期的控制
這篇文章主要介紹了Android應(yīng)用開發(fā)中Fragment生命周期的控制,Fragment依賴于Activity,所以生命周期方面也受Activity的影響,需要的朋友可以參考下2016-02-02
Android PopupWindow實(shí)現(xiàn)左側(cè)彈窗效果
這篇文章主要為大家詳細(xì)介紹了Android PopupWindow實(shí)現(xiàn)左側(cè)彈窗效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android中new Notification創(chuàng)建實(shí)例的最佳方法
這篇文章主要介紹了Android中new Notification創(chuàng)建實(shí)例的最佳方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
Android應(yīng)用中使用ViewPager實(shí)現(xiàn)類似QQ的界面切換效果
這篇文章主要介紹了Android應(yīng)用中使用ViewPager實(shí)現(xiàn)類似QQ的界面切換效果的示例,文中的例子重寫了PagerAdapter,并且講解了如何解決Android下ViewPager和PagerAdapter中調(diào)用notifyDataSetChanged失效的問題,需要的朋友可以參考下2016-03-03
Android簡單實(shí)現(xiàn)啟動(dòng)畫面的方法
這篇文章主要介紹了Android簡單實(shí)現(xiàn)啟動(dòng)畫面的方法,結(jié)合實(shí)例形式分析了啟動(dòng)畫面核心代碼及相關(guān)函數(shù),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07

