随時随地閱讀更多技術實戰幹貨,擷取項目源碼、學習資料,請關注源代碼社群公衆号(ydmsq666)、QQ技術交流群(183198395)。
在android中進度條和頁籤的使用一文中簡單介紹了進度條的使用,但是該文中隻介紹了普通進度條(包括标題欄進度條和水準進度條等)的使用,那麼在本文中将補充一種對話框式的進度條,它在實際中運用更為廣泛,代碼如下:
Activity:
package com.home;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button showProgressDialogBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showProgressDialogBtn = (Button) findViewById(R.id.main_btn_show);
showProgressDialogBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// 顯示對話框
showDialog(0);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
// 執行個體化進度條對話框
ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("測試進度條對話框");
dialog.setIndeterminate(true);
dialog.setMessage("程式正在加載請稍後。。。");
dialog.setCancelable(true);
return dialog;
}
}
布局XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/main_btn_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顯示進度條對話框" />
</LinearLayout>
附上圖檔效果: