android 自定义progressdialog,自定义ProgressDialog

android 自定义progressdialog,自定义ProgressDialog最近工作中需要用到progressDialog,可是系统自带的黑色progressDialog又是其丑无比,无奈只能自己自定义了,在网上查看别人的例子,并自己整理了一份Demo:先上图:MyProgressDialog:packagecom.example.myprogressdialog_zzw;importandroid.app.Dialog;importandroid.content….

大家好,又见面了,我是你们的朋友全栈君。

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

先上图:

f45420443314092f9d05ca6c7c20d98e.png

92f35a45f69bdab3fbc92f1ff328c2e9.png

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/159103.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • uint32 java_关于Java的int和C的uint32之间的转换

    uint32 java_关于Java的int和C的uint32之间的转换最近在做一个项目,是Android程序跟单片机之间通讯的,需求是Android程序给单片机发送一堆数据之后,要对这些数据进行CRC校验,手机端自己算一个校验值,发送给单片机,由单片机跟单片机部分算出的校验值做对比,一致则通过校验,不一致则校验失败。刚开始用Java自带的CRC校验类做校验,用CRC校验工具测试,结果一致,于是满怀信心的开始跟单片机正式测试,结果校验失败。以为是大小端的原因,就改了下…

    2025年9月27日
    2
  • 文件名太长删除不了

    文件名太长删除不了

    2021年10月16日
    45
  • StringTokenizer类的用法「建议收藏」

    StringTokenizer类的用法「建议收藏」StringTokenizer是一个用来分隔String的应用类,相当于VB的split函数。1.构造函数publicStringTokenizer(Stringstr)publicStringTokenizer(Stringstr,Stringdelim)publicStringTokenizer(Stringstr,Stringdelim,boolean

    2022年8月11日
    6
  • win10 1分钟自动重启_windows7一分钟后自动重启

    win10 1分钟自动重启_windows7一分钟后自动重启前言Charles是收费软件,可以免费试用30天。试用期过后,未付费的用户仍然可以继续使用,但是每次使用时间不能超过30分钟,并且启动时将会有10秒种的延时。此时,我们只需网上找一个注册码即可解

    2022年7月31日
    6
  • vue axios跨域请求_vue跨域访问

    vue axios跨域请求_vue跨域访问vue中axios跨域请求1.axios是第三方库使用方法:使用npm:$npminstallaxios使用bower:$bowerinstallaxios使用cdn:<scriptsrc=”https://unpkg.com/axios/dist/axios.min.js”></script>axios…

    2025年11月1日
    2
  • eplan激活码【2021最新】

    (eplan激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月22日
    298

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号