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


相关推荐

  • maven pom.xml具体解释(整理)「建议收藏」

    maven pom.xml具体解释(整理)

    2022年1月31日
    43
  • Android 蓝牙开发(一)蓝牙通信「建议收藏」

    Android 蓝牙开发(一)蓝牙通信「建议收藏」随着可穿戴设备的流行,研究蓝牙是必不可少的一门技术了。总结了下蓝牙开发使用的一些东西分享一下。首先需要AndroidManifest.xml文件中添加操作蓝牙的权限。允许程序连接到已配对的蓝牙设备。

    2022年6月26日
    21
  • 使用griddata进行均匀网格和离散点之间的相互插值

    使用griddata进行均匀网格和离散点之间的相互插值文章目录1griddata函数介绍2离散点插值到均匀网格3均匀网格插值到离散点4获取最近邻的Index插值操作非常常见,数学思想也很好理解。常见的一维插值很容易实现,相对来说,要实现较快的二维插值,比较难以实现。这里就建议直接使用scipy的griddata函数。1griddata函数介绍官网介绍2离散点插值到均匀网格definterp2d_station_to_gri…

    2022年5月20日
    29
  • MAC PHP集成环境安装MAMP

    MAC PHP集成环境安装MAMP我在初学PHP的时候先安装了小皮(PHPstudy),但是这个集成开发软件更适合windows,mac版本的也是在最近才出来的,bug很多。然后去安装了Xmsmpformac,但是也是不是很顺利。最终尝试了一下MAMP。虽然要付费,但是真的香,而且可以有14天的免费试用期。足够初学者进行学习。接下来是一个我安装MAMP的一个过程。1、首先去官网进行安装,左上方点击download,以及接下来跟着走就好了。2、这是安装完以后打开的初始界面。3、我们首先可以配置一下apache和my

    2022年6月28日
    32
  • 计算机二级考试数据结构与算法知识点_算法与数据结构是计算机两大基础

    计算机二级考试数据结构与算法知识点_算法与数据结构是计算机两大基础按照自己的理解写的解题思路,如有错误希望指正。1.算法的复杂度: ①时间复杂度:执行算法所需的计算工作量(又叫:基本运算次数) ②空间复杂度:执行算法所需的内存 它们是没有任何关系的!!!2.求二叉树序列类题目 要点:前序—根左右 中序—左根右 后序—左右根 例1:已知前序ABCDE,中序BCADE,求后序;同类型,已知任意两个求第三个 解题思路: 由前序知道A是根,结合中序,CB是左子树,DE…

    2022年8月18日
    5
  • 电商平台安全_跨境电商有哪些平台

    电商平台安全_跨境电商有哪些平台电商网站安全之威胁一、越权操作凡是仅靠传入参数就进行数据库查询的功能即存在越权。越权类型:1、平行越权(订单,留言,送货地址,修改信息,修改密码…)2、垂直越权(修改信息,修改密码,创建用户..)3、越权查询4、越权修改5、直接越权6、间接越权7、……越权操作的危害:泄漏用户数据,非法篡改他人业务,权限提升。无法通过WAF以及常规手段发现。越权形式影响越权查看订单/保单订单数据…

    2022年10月1日
    1

发表回复

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

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