等待对话框,这里有两种方式来实现:
ProgressDialog方式
progressDialog初始化
private void initProgressDialog() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setIndeterminate(false);//循环滚动
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage("loading...");
progressDialog.setCancelable(false);//false不能取消显示,true可以取消显示
}
显示对话框
progressDialog.show();
隐藏对话框
progressDialog.dismiss();
自定义Dialog
继承Dialog
public class WaitDialog extends Dialog {
private TextView waitText;
public WaitDialog(Context context) {
super(context, R.style.WaitDialogStyle);//设置样式
setCanceledOnTouchOutside(false);//按对话框以外的地方不起作用,按返回键可以取消对话框
getWindow().setGravity(Gravity.CENTER);
setContentView(R.layout.dialog_wait_layout);
waitText = (TextView) findViewById(R.id.tv_wait_dialog_text);
}
/**
* 设置显示文字
*
* @param waitMsg
*/
public void setText(CharSequence waitMsg) {
waitText.setText(waitMsg);
}
/**
* 设置文字
*
* @param resId
*/
public void setText(int resId) {
waitText.setText(resId);
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="60dp"
android:background="@color/white"
android:gravity="center"
android:orientation="horizontal">
<ProgressBar
android:layout_width="30dp"
android:layout_height="30dp"
android:indeterminateBehavior="repeat"
android:indeterminateOnly="true" />
<TextView
android:id="@+id/tv_wait_dialog_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:gravity="center"
android:textSize="25sp" />
</LinearLayout>
显示
waitDialog.show();
隐藏
waitDialog.dismiss();