Android進(jìn)度條控件progressbar使用方法詳解
一、簡介
二、方法
1)進(jìn)度條ProgressBar使用方法
1、在layout布局文件中創(chuàng)建ProgressBar控件
<ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:progress="30" />
2、用ProgressBar對象指向ProgressBar控件
private ProgressBar pb_progressBar1; pb_progressBar1=(ProgressBar) findViewById(R.id.pb_progressBar1);
3、通過ProgressBar對象的getProgress()和setProgress()方法對進(jìn)度進(jìn)行修改
if(progress<=100){ progress=pb_progressBar1.getProgress(); progress+=(int)(100*0.2); pb_progressBar1.setProgress(progress); }else progress=100;
三、代碼實例
效果圖:
點(diǎn)擊增加進(jìn)度按鈕:
點(diǎn)擊減少進(jìn)度按鈕:
代碼:
fry.Activity01
package fry; import com.example.Ex26ProgressBar.R; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; public class Activity01 extends Activity implements OnClickListener{ /* * 進(jìn)度條ProgressBar使用方法 * 1、在layout布局文件中創(chuàng)建ProgressBar控件 * 2、用ProgressBar對象指向ProgressBar控件 * 3、通過ProgressBar對象的getProgress()和setProgress()方法對進(jìn)度進(jìn)行修改 * */ private Button btn_addProgress; private Button btn_minusProgress; private ProgressBar pb_progressBar1; private int progress; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity01); btn_addProgress=(Button) findViewById(R.id.btn_addProgress); btn_minusProgress=(Button) findViewById(R.id.btn_minusProgress); pb_progressBar1=(ProgressBar) findViewById(R.id.pb_progressBar1); btn_addProgress.setOnClickListener(this); btn_minusProgress.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn_addProgress: if(progress<=100){ progress=pb_progressBar1.getProgress(); progress+=(int)(100*0.2); pb_progressBar1.setProgress(progress); }else progress=100; break; case R.id.btn_minusProgress: if(progress>=0){ progress=pb_progressBar1.getProgress(); progress-=(int)(100*0.2); pb_progressBar1.setProgress(progress); }else progress=0; break; default: break; } } }
/Ex26ProgressBar/res/layout/activity01.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" > <!-- style設(shè)置控件樣式 --> <!-- 用?來引用東西 --> <ProgressBar style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar style="?android:attr/progressBarStyleLargeInverse" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:progress="30" /> <ProgressBar android:id="@+id/pb_progressBar1" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:progress="50" android:secondaryProgress="80" android:layout_marginTop="30dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btn_addProgress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="增加進(jìn)度" android:layout_weight="1" /> <Button android:id="@+id/btn_minusProgress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="減少進(jìn)度" android:layout_weight="1" /> </LinearLayout> </LinearLayout>
四、注意點(diǎn)
1、通過ProgressBar對象的getProgress()和setProgress()方法對進(jìn)度進(jìn)行修改
progress=pb_progressBar1.getProgress(); progress+=(int)(100*0.2); pb_progressBar1.setProgress(progress);
2、遇到不知道的控件和屬性,可以通過set和get方法來看看怎么使用
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義view實現(xiàn)進(jìn)度條指示效果
這篇文章主要為大家詳細(xì)介紹了Android自定義view實現(xiàn)進(jìn)度條指示效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01Android實戰(zhàn)RecyclerView頭部尾部添加方法示例
本篇文章主要介紹了Android實戰(zhàn)RecyclerView頭部尾部添加方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11Android中通過MediaStore獲取音樂文件信息方法
這篇文章主要介紹了Android中通過MediaStore獲取音樂文件信息方法,本文講解了獲取歌曲的名稱、歌曲的專輯名、歌曲的歌手名、歌曲文件的全路徑、歌曲文件的名稱、歌曲文件的發(fā)行日期等音樂文件信息的方法,需要的朋友可以參考下2015-04-04Android應(yīng)用實踐之?dāng)?shù)獨(dú)游戲開發(fā)
這篇文章主要為大家詳細(xì)介紹了Android應(yīng)用實踐之?dāng)?shù)獨(dú)游戲開發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12