资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

自定义ProgressDialog

最近工作中需要用到progressDialog,可是系统自带的黑色progressDialog又是其丑无比,无奈只能自己自定义了,在网上查看别人的例子,并自己整理了一份Demo:

10余年的麦积网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站的优势是能够根据用户设备显示端的尺寸不同,自动调整麦积建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“麦积网站设计”,“麦积网站推广”以来,每个客户项目都认真落实执行。

先上图:

自定义ProgressDialog

自定义ProgressDialog

MyProgressDialog:

package com.example.myprogressdialog_zzw;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author 鹭岛猥琐男
 *
 */
public class MyProgressDialog extends Dialog
{
    private Context context;
    private static MyProgressDialog myProgressDialog = null;

    public MyProgressDialog(Context context)
    {
        super(context);
        this.context = context;
    }

    public MyProgressDialog(Context context, int theme)
    {
        super(context, theme);
    }

    public static MyProgressDialog createDialog(Context context)
    {
        myProgressDialog = new MyProgressDialog(context,
                R.style.myprogressDialog);
        myProgressDialog.setContentView(R.layout.dialog_layout);
        myProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        return myProgressDialog;

    }

    /*
     * (non-Javadoc)
     *
     * @see android.app.Dialog#onWindowFocusChanged(boolean) 设置动画
     */
    @Override
    public void onWindowFocusChanged(boolean hasFocus)
    {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
        ImageView p_w_picpath_loadingp_w_picpath = (ImageView) myProgressDialog
                .findViewById(R.id.p_w_picpath_loadingp_w_picpath);
        AnimationDrawable animation = (AnimationDrawable) p_w_picpath_loadingp_w_picpath
                .getBackground();
        animation.start();
    }

    public MyProgressDialog setTitle(String strTitle)
    {
        return myProgressDialog;
    }

    /**
     * @param strMessage
     * @return 设置progressDialog的消息内容
     */
    public MyProgressDialog setMessage(String strMessage)
    {
        TextView tv_loadingmsg = (TextView) myProgressDialog
                .findViewById(R.id.tv_loadingmsg);
        if (tv_loadingmsg != null)
        {
            tv_loadingmsg.setText(strMessage);
        }
        return myProgressDialog;
    }
}

在MainActivity中对MyProgressDialog进行调用,为了模仿网络访问结束后,关闭ProgressDialog的过程,采用了线程的sleep,运行5秒后关闭ProgressDialog,上代码:

package com.example.myprogressdialog_zzw;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

/**
 * @author 鹭岛猥琐男
 * 采用线程的的sleep模拟下载结束后关闭progressDialog
 *
 */
public class MainActivity extends Activity
{
    MyProgressDialog myProgressDialog = null;
    Handler handler = new Handler()
    {
        public void handleMessage(Message msg)
        {
            if (msg.what == 1)
            {
                Log.e("接收到消息", "" + msg.what);
                if (myProgressDialog != null)
                {
                    myProgressDialog.dismiss();
                    myProgressDialog = null;
                }
                Toast.makeText(MainActivity.this, "加载完成!", Toast.LENGTH_SHORT)
                        .show();
            }
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn_go = (Button) findViewById(R.id.button1);
        btn_go.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                if (myProgressDialog == null)
                {
                    myProgressDialog = MyProgressDialog
                            .createDialog(MainActivity.this);
                    myProgressDialog.setMessage("努力加载中...");

                }
                myProgressDialog.show();
                new Thread()
                {
                    @Override
                    public void run()
                    {
                        Log.e("线程", "进入线程!");
                        try
                        {
                            Thread.sleep(5000);
                            Message msg = new Message();
                            msg.what = 1;
                            handler.sendMessage(msg);
                        }
                        catch (InterruptedException e)
                        {
                            Log.e("异常", "失败!异常");
                        }

                    }
                }.start();

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

下载地址:http://download.csdn.net/detail/zzw0221/7609051


文章名称:自定义ProgressDialog
分享地址:http://cdkjz.cn/article/gseege.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220