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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 微信小程序,循环下拉列表,点击时只展开其中一个「建议收藏」

    微信小程序,循环下拉列表,点击时只展开其中一个这是效果1.wxml<!–bigwarp–><viewclass=”dewarp”><viewclass=’com-selectBox’wx:for=”{{detil}}”wx:for-item=”item”wx:key=””><viewclass=’co…

    2022年4月10日
    301
  • UFT12.02安装

    UFT12.02安装1.打开安装程序 2.选择语言3.选择插件4.UFT配置5.安装6.安装完成7.自述文件8.启动UFT9.进入UFT后的界面

    2022年5月8日
    36
  • mybatis-plus id主键生成的坑

    mybatis-plus id主键生成的坑mybatis-plusid主键生成的坑简要说明错误解决方案一1.修改id字段类型2.调整数据库id字段类型解决方案二添加注解其他`type`类型介绍简要说明由于mybatis-plus会自动插入一个id到实体对象,不管你封装与否,所以有时候导致一些意外的情况发生默认是生成一个长数字字符串(编码不同可能结尾带有字母)错误estedexceptionisorg.apac…

    2022年6月17日
    84
  • <input type=”file” />浏览时只显示指定文件类型

    <input type=”file” />浏览时只显示指定文件类型浏览时只显示指定文件类型inputtype=”file”accept=”application/msword”>br>br>accept属性列表br>1.accept=”application/msexcel”2.accept=”application/msword”3.accept=”application/pdf”4.accept=”application/pos

    2022年7月16日
    17
  • DHCP配置命令(DHCP配置命令)

    #DHCP动态主机配置协议,用来分配IP地址等网络参数。用户上网需要条件:IP地址,网关,DNS…注意:除非有特殊需求会采用静态配置(企业员工比较多的企业)路由器、核心交换机、Linux、服务器上面都可以配置DHCPDHCP配置实验:[Huawei]dhcpenable:开启DHCP服务[Huawei]ippoolaa:在路由器上创建IP地址池[Huawei-ip-pool-aa]network192.168.1.0mask24:给IP地址池添加IP地址网段[Huawei-i

    2022年4月18日
    54
  • laravel怎么获取到public路径

    laravel怎么获取到public路径

    2021年10月22日
    40

发表回复

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

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