Android中的对话框中的进度条

时间:2014-09-18 23:02:48

标签: android android-layout android-activity progressdialog android-progressbar

我正在尝试在我的Android应用程序中使用Dialogue中的进度条,以便当用户按下按钮时,会出现带有“请稍候”和移动进度条等文本的对话框。我不知道该怎么做。

以下是代码:

package com.example.progressdialog;

import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {


    ProgressDialog pd = new ProgressDialog(this);

    /** Called when the activity is first created. */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pleasewait();
    }



    public void pleasewait(View v){

   pd.setTitle(“Hello”);


           pd.show();
    }

} 

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Please wait…"
        android:onClick=”pleasewait” />

</LinearLayout>

请在我的代码中进行更正,或者,如果可以,请提出一些更好的方法。

当我尝试建议时

enter image description here

2 个答案:

答案 0 :(得分:1)

你可以试试这个:

public class MainActivity extends Activity {
 Button button;
 ProgressDialog progressDoalog;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  button = (Button) findViewById(R.id.button1);
  button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
      progressDoalog = new ProgressDialog(MainActivity.this);
      progressDoalog.setMax(100);
      progressDoalog.setMessage("Its loading....");
      progressDoalog.setTitle("ProgressDialog bar example");
      progressDoalog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
      progressDoalog.show();
      new Thread(new Runnable() {
        @Override
        public void run() {
                        try {
     while (progressDoalog.getProgress() <= progressDoalog
    .getMax()) {
       Thread.sleep(200);
       handle.sendMessage(handle.obtainMessage());
       if (progressDoalog.getProgress() == progressDoalog
    .getMax()) {
         progressDoalog.dismiss();
       }
     }
          } catch (Exception e) {
     e.printStackTrace();
   }
        }
      }).start();
    }

    Handler handle = new Handler() {
    @Override
      public void handleMessage(Message msg) {
        super.handleMessage(msg);
        progressDoalog.incrementProgressBy(1);
      }
    };
  });
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items 
                //to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}

EXAMPLE

简而言之:

Example:

import android.app.ProgressDialog;
//in the class...
private ProgressDialog progressBar;

//when you want the dialog to show the first time.
progressBar = ProgressDialog.show(Main.this, "Disconnecting", "Please wait for few secs...");

//when you want the progressbar to disappear
if (progressBar.isShowing()) {
progressBar.dismiss();
}

答案 1 :(得分:1)

您需要的是声明一个进度对话框

ProgressDialog progress;

用这里的数据显示它

progress = ProgressDialog.show(this, "dialog title","dialog message", true);

而不是在必要时将其解雇

progress.dismiss();