android等待對話框
阿新 • • 發佈:2019-04-23
ble 不起作用 ESS name pos void lin dia read
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();
源碼下載
android等待對話框