自定义ProgressDialog(无遮罩效果)

1.自定义progressDialog的theme:

    
    

2.自定义的CustomProgressDialog extends Dialog:

package com.ylj.progressdialogdemo;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
  
public class CustomProgressDialog extends Dialog {  
	private static CustomProgressDialog dialog;

	public CustomProgressDialog(Context context) {  
        super(context);  
    }  
  
    public CustomProgressDialog(Context context, int theme) {  
        super(context, theme);  
    }      
  
    /** 
     * 弹出自定义ProgressDialog 
     *  
     * @param context 
     *            上下文 
     * @param message 
     *            提示 
     * @param cancelable 
     *            是否可取消 
     * @param cancelListener 
     *            按下返回键监听 
     * @return 
     */  
    public static CustomProgressDialog show(Context context, CharSequence message, boolean cancelable, OnCancelListener cancelListener) { 
    	if(dialog == null){
    		dialog = new CustomProgressDialog(context, R.style.Custom_Progress);  
    	}
        dialog.setTitle("");  
        dialog.setContentView(R.layout.layout_progress_dialog);  
        
        if (message == null || message.length() == 0) {  
            dialog.findViewById(R.id.tv_message).setVisibility(View.GONE);  
        } else {  
            TextView tv_message = (TextView) dialog.findViewById(R.id.tv_message);  
            tv_message.setText(message);  
        }  
        // 按返回键是否取消  
        dialog.setCancelable(cancelable);  
        // 监听返回键处理  
        dialog.setOnCancelListener(cancelListener);  
        // 设置居中  
        dialog.getWindow().getAttributes().gravity = Gravity.CENTER;  
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();  
        // 设置背景层透明度  
        lp.dimAmount = 0.1f;  
        dialog.getWindow().setAttributes(lp);  
        dialog.show();  
        
        return dialog;  
    } 
    
    /**
     * 关闭dialog
     */
    public static void dimiss() {  
        if(dialog != null && dialog.isShowing()){
        	dialog.dismiss();
        }
    }
} 

3.layout_progress_dialog.xml 布局:




    

    


4.MainActivity:

package com.ylj.progressdialogdemo;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener {

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

		setContentView(R.layout.activity_main);
		findViewById(R.id.btn_save).setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		new LoginTask().execute();
	}

	private class LoginTask extends AsyncTask {

		@Override
		protected void onPreExecute() {
			CustomProgressDialog.show(MainActivity.this, "登录中...", false, null);
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... params) {
			try {
				new Thread().sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			CustomProgressDialog.dimiss();
		}
	}
}

5.activity_main.xml布局:



    

效果图如下:

自定义ProgressDialog(无遮罩效果)_第1张图片

你可能感兴趣的:(Android)