13

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

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))) {
                }
4

4 回答 4

33

使用它可能更容易

ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "", 
                        "Loading. Please wait...", true);

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

取消将是

    dialog.dismiss();

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

于 2012-06-11T09:15:58.680 回答
11

由于ProgressDialog该类已被弃用,这里有一个简单的显示ProgressBar方式AlertDialog

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

    AlertDialog.Builder builder;
    AlertDialog progressDialog;
    
  2. 在 Activity 中添加 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;
    }
    
  3. 初始化progressDialog

    progressDialog = getDialogProgressBar().create();
    
  4. 每当您想使用实用方法时显示/隐藏 AlertDialog:

    progressDialog.show()progressDialog.dismiss()

于 2019-08-26T07:31:04.400 回答
4

如果您希望显示进度条,请尝试以下步骤,您也可以将整个代码复制并粘贴到代码的相关部分,它应该可以工作。

//the first thing you need to to is to initialize the progressDialog Class like this

final ProgressDialog progressBarDialog= new ProgressDialog(this);

//set the icon, title and progress style..

progressBarDialog.setIcon(R.drawable.ic_launcher);

progressBarDialog.setTitle("Showing progress...");

progressBarDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);


    //setting the OK Button
    progressBarDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog,
                int whichButton){
            Toast.makeText(getBaseContext(),
                    "OK clicked!", Toast.LENGTH_SHORT).show();
        }
    });

    //set the Cancel button
    progressBarDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int whichButton){
            Toast.makeText(getApplicationContext(), "Cancel clicked", Toast.LENGTH_SHORT).show();
        }
    });
//initialize the dialog..
progressBarDialog.setProgress(0);

//setup a thread for long running processes
new Thread(new Runnable(){
    public void run(){
        for (int i=0; i<=15; i++){
            try{
                Thread.sleep(1000);
                progressBarDialog.incrementProgressBy((int)(5));
            }
            catch(InterruptedException e){
                e.printStackTrace();
            }
        }
        //dismiss the dialog
        progressBarDialog.dismiss();
     }
   });

//show the dialog
progressBarDialog.show();

取消按钮应关闭对话框。

于 2014-12-05T16:20:15.430 回答
0

试试下面的代码

  private class DownloadingProgressTask extends
        AsyncTask<String, Void, Boolean> {

    private ProgressDialog dialog = new ProgressDialog(ShowDescription.this);

    /** progress dialog to show user that the backup is processing. */

    /** application context. */

    protected void onPreExecute() {
         this.dialog.setMessage("Please wait");
         this.dialog.show();
    }

    protected Boolean doInBackground(final String... args) {
        try {
            // write your request code here


            **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))) {**

              return true;
        } catch (Exception e) {
            Log.e("tag", "error", e);
            return false;
        }
    }

    @Override
    protected void onPostExecute(final Boolean success) {

        if (dialog.isShowing()) {
            dialog.dismiss();
        }

        if (success) {
            Toast.makeText(ShowDescription.this,
                    "File successfully downloaded", Toast.LENGTH_LONG)
                    .show();
            imgDownload.setVisibility(8);
        } else {
            Toast.makeText(ShowDescription.this, "Error", Toast.LENGTH_LONG)
                    .show();
        }
    }

}

并在 onclick 事件中调用它

new DownloadingProgressTask().execute();
于 2012-06-11T10:33:07.927 回答