大家好,又见面了,我是你们的朋友全栈君。
ProgressDialog 继承自AlertDialog,AlertDialog继承自Dialog
public class ProgressDialog extends AlertDialog
ProgressDialog的创建方式有两种,一种是new ProgressDialog,一种是调用ProgressDialog的静态方法show()创建并显示,这种进度条只能是圆形条。
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;
}
}
@希望能帮助到大家!
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/159233.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...