android使用者介面-組件Widget-進度條ProgressBar

來源:互聯網
上載者:User

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>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.