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


相关推荐

  • 前端组件库_前端组件库有什么好处

    前端组件库_前端组件库有什么好处0.前端自动化(Workflow)前端构建工具Yeoman–asetoftoolsforautomatingdevelopmentworkflowgulp–Thestr

    2022年8月3日
  • 宽字节注入详解_pba商业分析师含金量

    宽字节注入详解_pba商业分析师含金量尽管现在呼吁所有的程序都使用unicode编码,所有的网站都使用utf-8编码,来一个统一的国际规范。但仍然有很多,包括国内及国外(特别是非英语国家)的一些cms,仍然使用着自己国家的一套编码,比如gbk,作为自己默认的编码类型。也有一些cms为了考虑老用户,所以出了gbk和utf-8两个版本。我们就以gbk字符编码为示范,拉开帷幕。gbk是一种多字符编码,具体定义自行百度。但有一个地方尤其要…

    2022年10月15日
  • Visual Studio 2017安装教程

    Visual Studio 2017安装教程下载VS2017组件选择及安装设置激活其它下载VS2017下载VS2017的安装器:http://msdn.itellyou.cn/我的安装版本为:VisualStudio2017(version15.7)VisualStudioProfessional2017(version15.7)(x86andx64)-(MultipleLanguages)下载此安…

    2022年5月26日
  • linux启动时开启screen

    linux启动时开启screen

    2022年3月13日
  • 领悟@Page指令中的AutoEventWireup

    领悟@Page指令中的AutoEventWireupAsp.NET中可以修改AutoEventWireup=”true”,使页面与某些特殊的事件方法绑定,自动识别这些具有特定名称的事件,而不需要进行委托。这些特定名称包括:Page_Init,Page_Load,Page_DataBind,Page_PreRender和Page_Unload等。.aspx设置AutoEventWireup=false情况下,Pag…

    2022年5月27日
  • git配置用户名和邮箱密钥_git配置全局用户名和密码

    git配置用户名和邮箱密钥_git配置全局用户名和密码一般刚安装Git都要配置用户名和密码,因为你提交代码到本地仓库(上传代码到远程仓库)时会用到,如果没有没有配置,在你提交时它会提醒你的。那么接下来我们讲一下怎么配置。

    2022年9月7日

发表回复

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

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