在警报对话框中显示进度条

9 android

我的应用程序中有一个用于登录身份验证的警告对话框.在发送请求时,我想显示一个进度条,如果响应是成功则想要解雇.如果有人知道,请帮助我.我使用下面的代码:

final AlertDialog.Builder alert = new AlertDialog.Builder(this);
LinearLayout login = new LinearLayout(this);
TextView tvUserName = new TextView(this);
TextView tvPassword = new TextView(this);
TextView tvURL = new TextView(this);
final EditText etUserName = new EditText(this);
final EditText etPassword = new EditText(this);
final EditText etURL = new EditText(this);
login.setOrientation(1); // 1 is for vertical orientation
tvUserName.setText(getResources().getString(R.string.username));
tvPassword.setText(getResources().getString(R.string.password));
tvURL.setText("SiteURL");
login.addView(tvURL);
login.addView(etURL);
login.addView(tvUserName);
login.addView(etUserName);
login.addView(tvPassword);
etPassword.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_PASSWORD);
login.addView(etPassword);
alert.setView(login);
alert.setTitle(getResources().getString(R.string.login));
alert.setCancelable(true);
alert.setPositiveButton(getResources().getString(R.string.login),
new DialogInterface.OnClickListener() {
    public void onClick(final DialogInterface dialog,
    int whichButton) {
        strhwdXml = etURL.getText().toString();
        strUserName = etUserName.getText().toString();
        XmlUtil.username = strUserName;
        strPassword = etPassword.getText().toString();
        if ((strUserName.length() == 0)
        && (strPassword.length() == 0)
        && (strhwdXml.length() == 0)) {
            Toast.makeText(
            getBaseContext(),
            getResources().getString(
            R.string.userPassword),
            Toast.LENGTH_SHORT).show();
            onStart();
            } else {
            final SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());
            SharedPreferences.Editor prefsEditor = prefs
            .edit();
            try {
                StringBuffer inStreamBuf = new StringBuffer();
                inStreamBuf = XmlUtil
                .getLoginAuthResponse(strUserName,
                strPassword, strhwdXml);
                strXmlResponse = inStreamBuf.toString();
                Log.e("Response:", strXmlResponse);
                String parsedXML = ParseResponse(strXmlResponse);
                if (parsedXML
                .equalsIgnoreCase(getResources()
                .getString(R.string.success))) {
                }
Run Code Online (Sandbox Code Playgroud)

jid*_*vah 30

使用它可能更容易

ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "", 
                        "Loading. Please wait...", true);
Run Code Online (Sandbox Code Playgroud)

您可以在此处阅读有关进度对话框的更多信息

取消将是

    dialog.dismiss();
Run Code Online (Sandbox Code Playgroud)

此类在API级别26中已弃用.ProgressDialog是一个模式对话框,可防止用户与应用程序进行交互.您应该使用ProgressBar等进度指示器,而不是使用此类,可以将其嵌入到应用程序的UI中.或者,您可以使用通知来通知用户任务的进度.有关详细信息,请单击此处

  • ProgressDialog已弃用,不适用于appcompat库. (5认同)

San*_*ans 10

由于ProgressDialog此类已废弃,这里是显示一个简单的方法ProgressBarAlertDialog

  1. 在您的活动中添加字段:

    AlertDialog.Builder builder;
    AlertDialog progressDialog;
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在您的活动中添加 getDialogProgressBar() 方法:

    public AlertDialog.Builder getDialogProgressBar() {
    
      if (builder == null) {
        builder = new AlertDialog.Builder(this);
    
        builder.setTitle("Loading...");
    
        final ProgressBar progressBar = new ProgressBar(this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
        progressBar.setLayoutParams(lp);
        builder.setView(progressBar);
      }
      return builder;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 初始化progressDialog

    progressDialog = getDialogProgressBar().create();
    
    Run Code Online (Sandbox Code Playgroud)
  4. 随时使用实用方法显示/隐藏 AlertDialog:

    progressDialog.show()progressDialog.dismiss()