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)
上一篇 2022年7月14日 下午12:36
下一篇 2022年7月14日 下午12:36


相关推荐

  • 如何将int类型转换为char_int类型转换成char类型

    如何将int类型转换为char_int类型转换成char类型近来面试遇到一个问题,通过控制台输入一个12位的数字,然后进行一些计算,然后被困在如何把char类型的数字转换成int类型。通过搜索,找到两个解决办法。1、把char型转换成int类型      for(inti=0;i<str.length();i++) {     chartemp_char=str.charAt(i); //把字符转换…

    2026年4月13日
    4
  • 1191 家谱树(拓扑排序)

    1191 家谱树(拓扑排序)1 问题描述 有个人的家族很大 辈分关系很混乱 请你帮整理一下这种关系 给出每个人的孩子的信息 输出一个序列 使得每个人的孩子都比那个人后列出 输入格式第 1 行一个整数 n 表示家族的人数 接下来 n 行 第 i 行描述第 i 个人的孩子 每行最后是 0 表示描述完毕 每个人的编号从 1 到 n 输出格式输出一个序列 使得每个人的孩子都比那个人后列出 数据保证一定有解 如果有多解输出任意一解 数据范围 1 n 100 输入样例

    2025年9月7日
    7
  • NetApp存储术语介绍

    NetApp存储术语介绍存储存储即为在不同场场景内采用合理 安全 有效的方式将数据保存在某个介质中 同时也能保证数据被有效的访问和使用 简单说就是两个方面 1 安全有效的长期或者临时保存在某个媒介中 2 保证数据完整有效的存放并通过简单有效的手段访问常见的存储方式 DAS Directattach 直接附属存储 顾名思义为直连存储 简单架构为将存储直接通过点到点的方式直接连接在服务器上 NAS Networkattac 网络附属存储 顾名思义为通过网络 TCP IP 形式与存储连

    2026年3月20日
    2
  • GloVe模型_nerlove模型

    GloVe模型_nerlove模型系列目录(系列更新中)第二讲cs224n系列之word2vec&词向量word2vec进阶之skim-gram和CBOW模型(HierarchicalSoftmax、NegativeSampling)第三讲cs224n系列之skip-pram优化&GlobalVectorbyManning&

    2025年11月24日
    4
  • iOS 判断字符串中含有某个字符串 rangeOfString

    iOS 判断字符串中含有某个字符串 rangeOfString在iOS8以后,还可以用下面的方法来判断是否包含某字符串:NSString*women=@"Heyyouarebitch?";if([womencontain

    2022年7月1日
    23
  • n8n入门教程:用零代码AI工作流自动化你的工作

    n8n入门教程:用零代码AI工作流自动化你的工作

    2026年3月15日
    2

发表回复

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

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