Android最简单的LoadingDialog

Activity的基类

public class BaseAcitivity extends Activity {

  private AlertDialog alertDialog;

  public void showLoadingDialog() {
      alertDialog = new AlertDialog.Builder(this).create();
      alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable());
      alertDialog.setCancelable(false);
      alertDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
          @Override
          public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
              if (keyCode == KeyEvent.KEYCODE_SEARCH || keyCode == KeyEvent.KEYCODE_BACK)
                  return true;
              return false;
          }
      });
      alertDialog.show();
      alertDialog.setContentView(R.layout.loading_alert);
      alertDialog.setCanceledOnTouchOutside(false);
  }

  public void dismissLoadingDialog() {
      if (null != alertDialog && alertDialog.isShowing()) {
          alertDialog.dismiss();
      }
  }
}

Fragment的基类


public class BaseFragment extends Fragment {
    private AlertDialog alertDialog;
    public void showLoadingDialog() {
        alertDialog = new AlertDialog.Builder(getActivity()).create();
        alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable());
        alertDialog.setCancelable(false);
        alertDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_SEARCH || keyCode == KeyEvent.KEYCODE_BACK)
                    return true;
                return false;
            }
        });
        alertDialog.show();
        alertDialog.setContentView(R.layout.loading_alert);
        alertDialog.setCanceledOnTouchOutside(false);
    }

    public void dismissLoadingDialog() {
        if (null != alertDialog && alertDialog.isShowing()) {
            alertDialog.dismiss();
        }
    }
}

XML:loading_alert



    


在styles.xml中添加


代码已经全部贴完,只需要复制Activity,Fragment的基类。然后自己的去继承这些。
用showLoadingDialog()方法开启LoadingDialog
用dismissLoadingDialog()方法关闭LoadingDialog
可以在原有的基础上修改xml布局实现更漂亮的Loading

你可能感兴趣的:(Android最简单的LoadingDialog)