Android入門之ProgressBar的使用教程
介紹
Android里的ProgressBar默認(rèn)為一個不斷轉(zhuǎn)圈的圓,它也可以自定義,但是如我在上一篇里所述我們不追求專業(yè)的beautiful。我們求的是Android這本知識我們可以完整的體系化的學(xué)會。因此在本篇里我們就使用ProgressBar的默認(rèn)樣式了。
ProgressBar有一個屬性:
style="?android:attr/progressBarStyleHorizontal"
這個屬性會讓原來默認(rèn)的ProgressBar變成橫向的進(jìn)度條。
課程目標(biāo)

- 課程內(nèi)設(shè)置了兩個進(jìn)度條。一個默認(rèn)的轉(zhuǎn)圈類ProgressBar,一個橫向的ProgressBar。
- 點“顯示/隱藏效果演示“,可以隱藏或者顯示ProgressBar,此處的隱藏我們用的是事件是“GONE“,GONE的意思是不顯示同時這個組件在原界面也不占位。
- 點擊橫向的ProgressBar會顯示當(dāng)前進(jìn)度值,每點一下橫向的ProgressBar當(dāng)前的值會+5并顯示在下部的TextView內(nèi)。
我們先來看我們的主界面UI
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/displayBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="顯示/隱藏效果演示"
android:textSize="20sp" />
<ProgressBar
android:id="@+id/pgBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<ProgressBar
android:id="@+id/pgBarHorizontal"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:max="100" />
<TextView
android:id="@+id/pgValueTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
</LinearLayout>它有4個組件,從上到下依次排列。
核心代碼導(dǎo)讀:
- style="?android:attr/progressBarStyleHorizontal"代表這個ProgressBar為橫向進(jìn)度條狀;
- android:max="100",這邊設(shè)置了進(jìn)度條漲滿的值為100;
我們再來看我們的JAVA端和界面交互代碼
MainActivity.java
package org.mk.android.demo.demoprogressbar;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ProgressBar btnProgressBar;
private ProgressBar pgStyleHorizontal;
private TextView txtPgValueShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtPgValueShow = (TextView) findViewById(R.id.pgValueTextView);
//橫向進(jìn)度條,點一下加5
pgStyleHorizontal = (ProgressBar) findViewById(R.id.pgBarHorizontal);
pgStyleHorizontal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.pgBarHorizontal:
//每點擊一次按鈕就可以獲取進(jìn)度條當(dāng)前的進(jìn)度
int progress = pgStyleHorizontal.getProgress();
progress = progress + 5;
pgStyleHorizontal.setProgress(progress);
StringBuffer currentValue = new StringBuffer();
currentValue.append("當(dāng)前進(jìn)度為: ").append(String.valueOf(progress));
txtPgValueShow.setText(currentValue.toString());
break;
default:
break;
}
}
});
//點一下顯示點一下隱藏圓形進(jìn)度條
Button button = (Button) findViewById(R.id.displayBtn);
btnProgressBar = (ProgressBar) findViewById(R.id.pgBar);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.displayBtn:
if (btnProgressBar.getVisibility() == View.GONE) {
btnProgressBar.setVisibility(View.VISIBLE);
} else {
btnProgressBar.setVisibility(View.GONE);
}
break;
default:
break;
}
}
});
}
}請各位自己動手去實現(xiàn)和運行一下,體驗一下這個效果吧。
到此這篇關(guān)于Android入門之ProgressBar的使用教程的文章就介紹到這了,更多相關(guān)Android ProgressBar內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android的ListView點擊item使item展開的做法的實現(xiàn)代碼
這篇文章主要介紹了android的ListView點擊item使item展開的做法的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
Android中的Dalvik和ART詳解及區(qū)別分析
小編通過這篇文章給大家整理了什么是Dalvik和ART,并進(jìn)行了區(qū)別的分析,下面一起來看看。2016-07-07
Android中自定義view實現(xiàn)側(cè)滑效果
這篇文章主要介紹了Android中自定義view實現(xiàn)側(cè)滑效果的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-11-11

