Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> android用戶界面-組件Widget-進度條ProgressBar

android用戶界面-組件Widget-進度條ProgressBar

編輯:Android開發實例

android的進度條有對話框進度條、標題進度條和水平進度條

一、對話框進度條

創建步驟

1、覆蓋Activity的onCreateDialog()方法,並在其中創建對話框。

2、調用Activity的showDialog()方法,顯示進度條對話框。

 

/Chapter04_UI_ProgressBar01/src/com/amaker/test/MainActivity.java

 
package com.amaker.test;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
private Button myBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myBtn = (Button)findViewById(R.id.Button01);
myBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(0);
}
});
}

@Override
protected Dialog onCreateDialog(int id) {
ProgressDialog dialog = new ProgressDialog(this);
// 可以不顯示標題
dialog.setTitle("測試對話框");
dialog.setIndeterminate(true);
dialog.setMessage("程序正在加載請稍後!");
dialog.setCancelable(true);

dialog.setButton(Dialog.BUTTON_POSITIVE, "確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}
);

return dialog;
}
}

 

二、標題欄進度條

1、調用Activity的requestWindowFeatures()方法,獲得進度條。

2、調用Activity的setProgressBarIndeterminateVisibility()方法,顯示進度條對話框。

 

/Chapter04_UI_ProgressBar02/src/com/amaker/test/MainActivity.java

 
package com.amaker.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
/** Called when the activity is first created. */

private Button b1,b2;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
b1 = (Button)findViewById(R.id.Button01);
b2 = (Button)findViewById(R.id.Button02);

b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setProgressBarIndeterminateVisibility(true);
}
});

b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setProgressBarIndeterminateVisibility(false);
}
});
}
}

 

三、水平進度條

創建步驟

1、在布局文件中聲明ProgressBar。

2、在Activity中獲得ProgressBar實例。

3、調用ProgressBar的incrementProgressBy()方法增加和減少進度。

 

/Chapter04_UI_ProgressBar03/src/com/amaker/test/MainActivity.java

 
package com.amaker.test;

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 MainActivity extends Activity {
private Button b1,b2;
ProgressBar progressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button)findViewById(R.id.Button01);
b2 = (Button)findViewById(R.id.Button02);
progressBar = (ProgressBar)findViewById(R.id.ProgressBar01);

b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
progressBar.incrementProgressBy(1);
}
});

b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
progressBar.incrementProgressBy(-1);
}
});

}
}

 /Chapter04_UI_ProgressBar03/res/layout/main.xml

 

 
<?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"
>
<ProgressBar
android:id="@+id/ProgressBar01"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
></ProgressBar>

<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加"></Button>

<Button
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="減少"></Button>

</LinearLayout>
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved