首页 > 移动开发 > Android自定义ProgressDialog

Android自定义ProgressDialog

MainActivity如下:

[java][/java] view plaincopy

  1. package cn.testprogressdialog;
  2. import android.app.Activity;
  3. import android.app.Dialog;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.content.DialogInterface.OnCancelListener;
  7. import android.os.Bundle;
  8. import android.view.Window;
  9. /**
  10.  * Demo描述:
  11.  * 自定义ProgressDialog
  12.  *
  13.  * 备注说明:
  14.  * 在此处progressDialog.setOnCancelListener(){}
  15.  * 并没有起到按下back键盘使风火轮消失的作用.
  16.  * 因为在initDialog(Context context)中设置了
  17.  * dialog.setCancelable(false);
  18.  * 若需要此功能,注释掉该句代码即可.
  19.  */
  20. public class MainActivity extends Activity {
  21.     @Override
  22.     protected void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.main);
  25.         Dialog progressDialog=initDialog(MainActivity.this);
  26.         progressDialog.setOnCancelListener(new OnCancelListener() {
  27.             @Override
  28.             public void onCancel(DialogInterface dialog) {
  29.                 dialog.dismiss();
  30.                 //其余业务逻辑
  31.             }
  32.         });
  33.         progressDialog.show();
  34.     }
  35.     public Dialog initDialog(Context context) {
  36.         Dialog dialog = new DialogSubClass(MainActivity.this, R.style.dialog);
  37.         dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  38.         dialog.setCanceledOnTouchOutside(false);
  39.         dialog.setCancelable(false);
  40.         return dialog;
  41.     }
  42. }

DialogSubClass如下:

[java][/java] view plaincopy

  1. package cn.testprogressdialog;
  2. import android.app.Dialog;
  3. import android.content.Context;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. public class DialogSubClass extends Dialog {
  8.     public DialogSubClass(Context context, int theme) {
  9.         super(context, theme);
  10.     }
  11.     protected DialogSubClass(Context context, boolean cancelable,OnCancelListener cancelListener) {
  12.         super(context, cancelable, cancelListener);
  13.     }
  14.     public DialogSubClass(Context context) {
  15.         super(context);
  16.     }
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         View view = LayoutInflater.from(getContext()).inflate(R.layout.dialoglayout, null);
  21.         setContentView(view);
  22.     }
  23.     @Override
  24.     public void show() {
  25.         super.show();
  26.     }
  27.     @Override
  28.     public void dismiss() {
  29.         super.dismiss();
  30.     }
  31.     public void setMsg(String txt) {
  32.     }
  33. }

dialoglayout.xml如下:

[html][/html] view plaincopy

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="wrap_content"
  4.     android:layout_height="wrap_content" >
  5.     <ProgressBar
  6.         android:id="@+id/progressBar"
  7.         android:layout_width="wrap_content"
  8.         android:layout_height="wrap_content"
  9.         android:layout_centerHorizontal="true"
  10.         />
  11.     <TextView
  12.         android:layout_width="wrap_content"
  13.         android:layout_height="wrap_content"
  14.         android:text="loading"
  15.         android:textSize="20sp"
  16.         android:layout_below="@id/progressBar"
  17.         android:layout_marginTop="-5dip"
  18.         android:layout_centerHorizontal="true"
  19.         android:textColor="@android:color/black"
  20.         />
  21. </RelativeLayout>

styles.xml如下:

[html][/html] view plaincopy

  1. <resources>
  2.     <style name="AppBaseTheme" parent="android:Theme.Light"></style>
  3.     <style name="AppTheme" parent="AppBaseTheme"></style>
  4.     <style name="dialog" parent="@android:style/Theme.Dialog">
  5.         <item name="android:windowFrame">@null</item>
  6.         <item name="android:windowIsFloating">true</item>
  7.         <item name="android:windowIsTranslucent">true</item>
  8.         <item name="android:windowNoTitle">true</item>
  9.         <item name="android:windowBackground">@android:color/transparent</item>
  10.         <item name="android:backgroundDimEnabled">true</item>
  11.         <item name="android:windowContentOverlay">@null</item>
  12.     </style>
  13. </resources>

main.xml如下:

[html][/html] view plaincopy

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     >
  6.     <TextView
  7.         android:layout_width="wrap_content"
  8.         android:layout_height="wrap_content"
  9.         android:text="测试自定义ProgressDialog"
  10.         android:layout_centerHorizontal="true"
  11.         android:textSize="24sp"
  12.     />
  13. </RelativeLayout>

本文固定链接: http://www.devba.com/index.php/archives/2228.html | 开发吧

报歉!评论已关闭.