progressdialog进度框_进度对话框 ProgressDialog 用法总结[通俗易懂]

progressdialog进度框_进度对话框 ProgressDialog 用法总结[通俗易懂]ProgressDialog继承自AlertDialog,AlertDialog继承自DialogpublicclassProgressDialogextendsAlertDialogProgressDialog的创建方式有两种,一种是newProgressDialog,一种是调用ProgressDialog的静态方法show()创建并显示,这种进度条只能是圆形条。imageProgr…

大家好,又见面了,我是你们的朋友全栈君。

ProgressDialog 继承自AlertDialog,AlertDialog继承自Dialog

public class ProgressDialog extends AlertDialog

ProgressDialog的创建方式有两种,一种是new ProgressDialog,一种是调用ProgressDialog的静态方法show()创建并显示,这种进度条只能是圆形条。

a9855cdc4712

image

ProgressDialog dialog = ProgressDialog.show(this, “提示”, “正在登陆中…”, true, false, null);

常用方法

setProgressStyle:设置进度条风格,风格为圆形,旋转的。

setTitlt:设置标题

setMessage:设置提示信息;

setIcon:设置标题图标;

setIndeterminate:设置ProgressDialog 的进度条是否不明确;这个属性对于ProgressDailog默认的转轮模式没有实际意义,默认下设置为true,它仅仅对带有ProgressBar的Dialog有作用。修改这个属性为false后可以实时更新进度条的进度。

setCancelable:设置ProgressDialog 是否可以按返回键取消;

cancelListner:当前Dialog强制取消之后将会被执行,通常用来清理未完成的任务。

setButton:设置ProgressDialog 的一个Button(需要监听Button事件);

show:显示ProgressDialog。

cancel:删除progressdialog

dismiss: 删除progressdialog 作用和cancel相同

setMax(int)、getMax:设置最大进度条的值

setProgress(int)、getProgress:更新进度条,当然一般都需要Handler的结合来更新进度条

incrementProgressBy(int)增加进度条

setProgressDrawable:设置progress发生变化时的进度指示条的背景图

Mainactivity

public class MainActivity extends ListActivity {

private ProgressDialog dialog;

private AlertDialog alertDialog;

private ProgressDialogFragment dialogFragment;

private Handler mHandler = new Handler() {

@Override

public void handleMessage(android.os.Message msg) {

switch (msg.what) {

case 0:

dialog.cancel();// cancel和dismiss唯一的区别是,调用cancel方法会【回调】OnCancelListener

break;

case 1:

int progress = (Integer) msg.obj;

if (progress < 20) {

dialog.incrementProgressBy(5);// 增加进度条的进度

mHandler.sendMessageDelayed(Message.obtain(mHandler, 1, progress + 1), 150);

} else dialog.dismiss();

break;

case 2:

alertDialog.dismiss();

break;

case 3:

getFragmentManager().beginTransaction().remove(dialogFragment).commit();

break;

}

};

};

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

String[] array = { “ProgressDialog.STYLE_SPINNER:不确定的圆形滚动条”,//

“ProgressDialog.STYLE_HORIZONTAL:确定的水平滚动条”, //

“通过AlertDialog实现不确定圆形滚动条效果,其View包含一个ProgressBar”,//

“通过DialogFragment实现不确定圆形滚动条效果,其View包含一个ProgressBar”, };

setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, new ArrayList(Arrays.asList(array))));

Fragment fragment = getFragmentManager().findFragmentByTag(“ProgressDialogFragment”);

//旋转屏幕后dialogFragment为空,但是findFragmentByTag的结果不为空,所以dialogFragment将永远不会消失

if (fragment != null) {

if (dialogFragment == null) Toast.makeText(MainActivity.this, “dialogFragment为空,findFragmentByTag不为空”, Toast.LENGTH_SHORT).show();

getFragmentManager().beginTransaction().remove(fragment).commit();

}

}

@Override

protected void onDestroy() {

super.onDestroy();

if (mHandler != null) mHandler.removeCallbacksAndMessages(null);

}

@Override

protected void onListItemClick(ListView l, View v, int position, long id) {

switch (position) {

case 0://旋转屏幕后直接消失

showPD();

mHandler.sendEmptyMessageDelayed(0, 3000);

break;

case 1://旋转屏幕后直接消失

showHorizontalPD();

mHandler.sendMessageDelayed(Message.obtain(mHandler, 1, 0), 150);

break;

case 2://旋转屏幕后直接消失

showAlertDialog();

mHandler.sendEmptyMessageDelayed(2, 3000);

break;

case 3://旋转屏幕后会重新创建

showDialogFragment();

mHandler.sendEmptyMessageDelayed(3, 3000);

break;

}

}

* //跟AlertDailog的样式一摸一样,还有一个圆形进度条!!!!!!!

private void showPD() {

dialog = new ProgressDialog(this);

dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//转盘

dialog.setCancelable(false);

dialog.setCanceledOnTouchOutside(false);

dialog.setTitle(“提示”);

dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

@Override

public void onDismiss(DialogInterface dialog) {

Toast.makeText(MainActivity.this, “消失了”, Toast.LENGTH_SHORT).show();

}

});

dialog.setMessage(“正在加载,请稍后……”);

dialog.show();

}

private void showHorizontalPD() {

dialog = new ProgressDialog(this);

dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

dialog.setCancelable(true);

dialog.setCanceledOnTouchOutside(false);

dialog.setIcon(R.drawable.ic_launcher);//这里指的是标题左侧的图标。注意:如果没有设置title只设置Icon的话,是不会显示图标的

dialog.setTitle(“提示”);

dialog.setMax(100);

dialog.setButton(DialogInterface.BUTTON_POSITIVE, “确定…”, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

Toast.makeText(MainActivity.this, “确定”, Toast.LENGTH_SHORT).show();

}

});

dialog.setButton(DialogInterface.BUTTON_NEGATIVE, “取消…”, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

Toast.makeText(MainActivity.this, “取消”, Toast.LENGTH_SHORT).show();

}

});

dialog.setMessage(“正在加载,请稍后……”);

dialog.show();

}

private void showAlertDialog() {

alertDialog = new AlertDialog.Builder(this).setView(R.layout.layout).create();

alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);//设置Dialog背景透明效果

alertDialog.show();

}

private void showDialogFragment() {

dialogFragment = ProgressDialogFragment.newInstance(“加载中…”);

dialogFragment.show(getFragmentManager(), “ProgressDialogFragment”);

}

}

ProgressDialogFragment

public class ProgressDialogFragment extends DialogFragment {

/**构造时把传入的参数带进来,注意一定要通过Bundle及setArguments传递数据*/

public static ProgressDialogFragment newInstance(String message) {

Bundle bundle = new Bundle();//把所有需要传递的数据都放在Bundle中

bundle.putString(“message”, message);

ProgressDialogFragment dialog = new ProgressDialogFragment();

dialog.setArguments(bundle);//通过setArguments把Bundle传递过去

return dialog;

}

@SuppressLint(“InflateParams”)

@Override

public Dialog onCreateDialog(Bundle savedInstanceState) {

View contentView = getActivity().getLayoutInflater().inflate(R.layout.layout, null);

TextView tv_messag = (TextView) contentView.findViewById(R.id.tv_messag);

if (getArguments() != null && !TextUtils.isEmpty(getArguments().getString(“message”))) {

tv_messag.setVisibility(View.VISIBLE);

tv_messag.setText(getArguments().getString(“message”));

} else tv_messag.setVisibility(View.GONE);

Dialog dialog = new Dialog(getActivity());

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//设置Dialog没有标题。需在setContentView之前设置,在之后设置会报错

dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);//设置Dialog背景透明效果

dialog.setCancelable(false);

dialog.setContentView(contentView);

return dialog;

}

}

@希望能帮助到大家!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/159233.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • BoundsChecker安装下载及使用教程攻略

    BoundsChecker安装下载地址:参见文章结尾附件1前言我在本文中详细介绍了测试工具NuMegaDevpartner(以下简称NuMega)的使用方法。NuMega是一个动态测试工具,主要应用于白盒测试。该工具的特点是学习简单、使用方便、功能有效。NuMega共有三个独立的子功能——BoundsChecker、TrueCoverage、TrueTime。BoundsChecker

    2022年4月6日
    47
  • android错误之解析包时出现问题(一)

    类越来越多,有点乱,强迫症,多弄了几个包,整理的井井有条,心里那个舒服,结果问题就来了无法安装了,总是显示“解析包时出现问题”,最烦的就是这种问题,一点技术含量都没有的小问题却要浪费大量时间去找原因。百度了很多,这个算是原因整理的最全的,可是一一对应着找过去,还是没发现问题http://blog.sina.com.cn/s/blog_6040e83d0100slph.html幸好我

    2022年3月10日
    38
  • 树:普通树(非二叉树)的遍历

    树:普通树(非二叉树)的遍历

    2022年2月7日
    56
  • 带宽计算_家庭宽带100兆够用吗

    带宽计算_家庭宽带100兆够用吗许多人对Kbps、KB、Mbps等速度单位有所误解,以下简单解释一下所谓的1.5M、3M、6M如何计算。所谓1.5M宽带,其实是指1.5Mbps(bitspersecond),亦

    2022年8月3日
    3
  • addslashes和stripslashes函数

    addslashes和stripslashes函数addslashes()函数返回在预定义字符之前添加反斜杠的字符串。预定义字符是:单引号(’)双引号(”)反斜杠(\)NULLecho”Who’sBillGates?<br>”;echoaddslashes(“Who’sBillGates?”)结果:Who’sBillGates?Who\’sBill…

    2022年10月21日
    0
  • 如何分析heapdump文件_heapdump怎么看

    如何分析heapdump文件_heapdump怎么看jhat是Java堆分析工具(JavaheapAnalyzesTool).在JDK6u7之后成为标配.使用该命令需要有一定的Java开发经验,官方不对此工具提供技术支持和客户服务。用法:jhat[options]heap-dump-file参数:options可选命令行参数,请参考下面的Optionsheap-dump-file要查看的二进制Java堆转储文件(Java…

    2022年10月3日
    0

发表回复

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

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