android 自定义progressdialog,自定义ProgressDialog
最近工作中需要用到progressDialog,可是系统自带的黑色progressDialog又是其丑无比,无奈只能自己自定义了,在网上查看别人的例子,并自己整理了一份Demo:
先上图:
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
android 自定义progressdialog,自定义ProgressDialog相关推荐
- Android开发之自定义的ProgressDialog
package com.example.dialog;import android.app.ProgressDialog; import android.content.Context; /*** 自 ...
- Android学习笔记之ProgressDialog
mian.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...
- android 自定义dialog 定时关闭,Android编程实现自定义Dialog的大小自动控制方法示例...
本文实例讲述了Android编程实现自定义Dialog的大小自动控制方法.分享给大家供大家参考,具体如下: Android应用开发中,无论是出于功能还是增加用户体验,弹出对话框(Dialog)进行一些 ...
- 如何在android中创建自定义对话框?
本文翻译自:How to create a Custom Dialog box in android? I want to create a custom dialog box like below ...
- 针对Android Tv的自定义RecyclerView2 0横竖向连动
版权声明:本文为博主原创文章,转载请注明出处. 推荐: 欢迎关注我创建的Android TV 简书专题,会定期给大家分享一些AndroidTv相关的内容: www.jianshu.com/c/37ef ...
- android 自定义actionbar前面有一块空白,解决Android V7后自定义Toolbar、ActionBar左侧有空白问题...
如图所示: 1.查看Wiget.AppCompat.Toolbar的parent主题,如下所示: @style/TextAppearance.Widget.AppCompat.Toolbar.Titl ...
- android标尺自定义view,android尺子的自定义view——RulerView详解
项目中用到自定义尺子的样式: 原效果为 因为跟自己要使用的view稍有不同 所以做了一些修改,修改的注释都放在代码中了,特此记录一下. 首先是一个自定义View: public class RuleV ...
- android 继承dialog自定义对话框
android 继承dialog自定义对话框 样式如下: 代码实现: 主要Activity: public class MainActivity extends Activity { Button ...
- android如何让自定义控件居中,Android自定义控件之自定义TextView,实现drawableLeft可以和文字一起居中...
如何实现使用TextView的DrawableLeft使图片和文字居中显示呢??? 代码如下: 1.首先自定义一个类,继承TextViewpackage com.test.signcalendar.w ...
- android 设置点击ProgressDialog外的区域不消失
今天,简单讲讲android如何设置点击ProgressDialog外的区域不消失. 昨天,自己做一个加载框ProgressDialog时,完成功能后,发现ProgressDialog点击区域外 ...
最新文章
- php如何获取当前时间 格式化,PHP获取当前日期和时间格式化步骤
- Java洛谷P1149 火柴棒等式
- 备忘:phalcon的坑
- Linux C高级编程——网络编程基础(1)
- C 语言实例 -求分数数列1/2+2/3+3/5+5/8+...的前n项和
- 趣头条宣布6月30日停止自媒体创作平台服务和维护
- JavaScript学习(五十六)—寄生式继承(临时构造器的使用)
- UVA - 815 Flooded!
- 知识、经验的漏洞还有很多很多
- WCDMA中的基本概念
- vs 2019怎么运行单个的cpp文件以及报错main已存在解决方法
- 大写金额转换(报销大写金额转换)
- Python_bug之Numpy问题
- 深入理解Linux 条件变量2:使用条件变量实现[生产-消费]框架
- Session.AUTO_ACKNOWLEDGE
- ur机器人计算机模拟仿真,ur机器人编程-设置工具
- 服务器dns显示fec,服务器dns地址fec0
- STM32配置DAC输出固定电压和方波
- 麦克风阵列之一阶差分麦克风阵列
- oracle数据库or exists,Oracle Not Exists运算符