package cn.testprogressdialog;import android.app.Activity;import android.app.Dialog;import android.content.Context;import android.content.DialogInterface;import android.content.DialogInterface.OnCancelListener;import android.os.Bundle;import android.view.Window;/** * Demo描述: * 自訂ProgressDialog * * 備忘說明: * 在此處progressDialog.setOnCancelListener(){} * 並沒有起到按下back鍵盤使風火輪消失的作用. * 因為在initDialog(Context context)中設定了 * dialog.setCancelable(false); * 若需要此功能,注釋掉該句代碼即可. */public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Dialog progressDialog=initDialog(MainActivity.this);progressDialog.setOnCancelListener(new OnCancelListener() {@Overridepublic void onCancel(DialogInterface dialog) {dialog.dismiss();//其餘商務邏輯}});progressDialog.show();}public Dialog initDialog(Context context) {Dialog dialog = new DialogSubClass(MainActivity.this, R.style.dialog);dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);dialog.setCanceledOnTouchOutside(false);dialog.setCancelable(false);return dialog;}}
package cn.testprogressdialog;import android.app.Dialog;import android.content.Context;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;public class DialogSubClass extends Dialog {public DialogSubClass(Context context, int theme) {super(context, theme);}protected DialogSubClass(Context context, boolean cancelable,OnCancelListener cancelListener) {super(context, cancelable, cancelListener);}public DialogSubClass(Context context) {super(context);}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);View view = LayoutInflater.from(getContext()).inflate(R.layout.dialoglayout, null);setContentView(view);}@Overridepublic void show() {super.show();}@Overridepublic void dismiss() {super.dismiss();}public void setMsg(String txt) {}}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ProgressBar android:id="@+id/progressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="loading" android:textSize="20sp" android:layout_below="@id/progressBar" android:layout_marginTop="-5dip" android:layout_centerHorizontal="true" android:textColor="@android:color/black" /></RelativeLayout>
<resources> <style name="AppBaseTheme" parent="android:Theme.Light"></style> <style name="AppTheme" parent="AppBaseTheme"></style> <style name="dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:backgroundDimEnabled">true</item> <item name="android:windowContentOverlay">@null</item> </style></resources>
<RelativeLayout 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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="測試自訂ProgressDialog" android:layout_centerHorizontal="true" android:textSize="24sp" /></RelativeLayout>