一、基礎知識:
1.ProgressBar在介面檔案XML中的布局:
[html]
<progressBar android:id="@+id/progressbar_updown"
android:layout_width="200dp"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:layout_gravity="center_vertical"
android:max="100"
android:progress="50"
android:secondaryProgress="70" >
<progressBar android:id="@+id/progressbar_updown"
android:layout_width="200dp"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:layout_gravity="center_vertical"
android:max="100"
android:progress="50"
android:secondaryProgress="70" >
[plain]
style="?android:attr/progressBarStyleHorizontal" 設定風格為長形
android:max="100" 最大進度值為100
android:progress="50" 初始化的進度值
android:secondaryProgress="70" 初始化的底層第二個進度值
android:layout_gravity="center_vertical" 垂直置中
style="?android:attr/progressBarStyleHorizontal" 設定風格為長形
android:max="100" 最大進度值為100
android:progress="50" 初始化的進度值
android:secondaryProgress="70" 初始化的底層第二個進度值
android:layout_gravity="center_vertical" 垂直置中
2.ProgressBar在代碼檔案(.java)中的控制使用:
[java]
private ProgressBar myProgressBar;
//定義ProgressBar
myProgressBar = (ProgressBar) findViewById(R.id.progressbar_updown);
//ProgressBar通過ID來從XML中擷取
myProgressBar.incrementProgressBy(5);
//ProgressBar進度值增加5
myProgressBar.incrementProgressBy(-5);
//ProgressBar進度值減少5
myProgressBar.incrementSecondaryProgressBy(5);
//ProgressBar背後的第二個進度條 進度值增加5
myProgressBar.incrementSecondaryProgressBy(-5);
//ProgressBar背後的第二個進度條 進度值減少5
private ProgressBar myProgressBar;
//定義ProgressBar
myProgressBar = (ProgressBar) findViewById(R.id.progressbar_updown);
//ProgressBar通過ID來從XML中擷取
myProgressBar.incrementProgressBy(5);
//ProgressBar進度值增加5
myProgressBar.incrementProgressBy(-5);
//ProgressBar進度值減少5
myProgressBar.incrementSecondaryProgressBy(5);
//ProgressBar背後的第二個進度條 進度值增加5
myProgressBar.incrementSecondaryProgressBy(-5);
//ProgressBar背後的第二個進度條 進度值減少5
3.XML重要屬性
android:progressBarStyle:預設進度條樣式
android:progressBarStyleHorizontal:水平樣式
4.重要方法
[plain]
getMax():返回這個進度條的範圍的上限
getProgress():返回進度
getSecondaryProgress():返回次要進度
incrementProgressBy(int diff):指定增加的進度
isIndeterminate():指示進度條是否在不確定模式下
setIndeterminate(boolean indeterminate):設定不確定模式下
setVisibility(int v):設定該進度條是否可視
getMax():返回這個進度條的範圍的上限
getProgress():返回進度
getSecondaryProgress():返回次要進度
incrementProgressBy(int diff):指定增加的進度
isIndeterminate():指示進度條是否在不確定模式下
setIndeterminate(boolean indeterminate):設定不確定模式下
setVisibility(int v):設定該進度條是否可視
二、代碼展示:
1."Activity_09\src\yan\activity_09\MainActivity.java"
[java]
package yan.activity_09;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.app.Activity;
public class MainActivity extends Activity {
// 聲明變數
private ProgressBar firstBar = null;
private ProgressBar secondBar = null;
private Button myButton = null;
private int progress_vol = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//映射控制項ID到變數
firstBar = (ProgressBar)findViewById(R.id.firstBar);
secondBar = (ProgressBar)findViewById(R.id.secondBar);
myButton = (Button)findViewById(R.id.myButton);
myButton.setOnClickListener(new ButtonListenr());
}
class ButtonListenr implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(0 == progress_vol)
{
// 設定進度條的最大值
firstBar.setMax(200);
// 設定進度條為可見的狀態
firstBar.setVisibility(View.VISIBLE);
secondBar.setVisibility(View.VISIBLE);
}else if(progress_vol < firstBar.getMax()){
// 設定主進度條的當前值
firstBar.setProgress(progress_vol);
// 設定第二進度條的當前值
firstBar.setSecondaryProgress(progress_vol+10);
// 預設的進度條是無法顯示進行的狀態的
//secondBar.setProgress(progress_vol);
}else{
// 設定進度條為不可見的狀態
firstBar.setVisibility(View.GONE);
secondBar.setVisibility(View.GONE);
}
progress_vol +=10;
}
}
}
package yan.activity_09;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.app.Activity;
public class MainActivity extends Activity {
// 聲明變數
private ProgressBar firstBar = null;
private ProgressBar secondBar = null;
private Button myButton = null;
private int progress_vol = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//映射控制項ID到變數
firstBar = (ProgressBar)findViewById(R.id.firstBar);
secondBar = (ProgressBar)findViewById(R.id.secondBar);
myButton = (Button)findViewById(R.id.myButton);
myButton.setOnClickListener(new ButtonListenr());
}
class ButtonListenr implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(0 == progress_vol)
{
// 設定進度條的最大值
firstBar.setMax(200);
// 設定進度條為可見的狀態
firstBar.setVisibility(View.VISIBLE);
secondBar.setVisibility(View.VISIBLE);
}else if(progress_vol < firstBar.getMax()){
// 設定主進度條的當前值
firstBar.setProgress(progress_vol);
// 設定第二進度條的當前值
firstBar.setSecondaryProgress(progress_vol+10);
// 預設的進度條是無法顯示進行的狀態的
//secondBar.setProgress(progress_vol);
}else{
// 設定進度條為不可見的狀態
firstBar.setVisibility(View.GONE);
secondBar.setVisibility(View.GONE);
}
progress_vol +=10;
}
}
}
2."Activity_09\res\layout\main.xml"
[html]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00aaaa"
>
<TextView
android:id="@+id/firstText"
android:text="@string/hello_world"
android:gravity="center_vertical"
android:textSize="15pt"
android:background="#aa0000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"/>
<ProgressBar
android:id="@+id/firstBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<ProgressBar
android:id="@+id/secondBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="begin"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00aaaa"
>
<TextView
android:id="@+id/firstText"
android:text="@string/hello_world"
android:gravity="center_vertical"
android:textSize="15pt"
android:background="#aa0000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"/>
<ProgressBar
android:id="@+id/firstBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<ProgressBar
android:id="@+id/secondBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="begin"
/>
</LinearLayout>
3."Activity_09\res\values\strings.xml"
[html]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Activity_09</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Activity_09</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
</resources>
三、效果展示: