在5000ms后显示第二个警报对话框

时间:2011-06-24 05:39:32

标签: android dialog

这里我想在第一个对话框消失后显示第二个对话框。我正在显示5000ms的第一个对话框,我们怎么能这样做?

提前致谢......!

 Button signin = (Button) findViewById(R.id.regsubmitbtn);

signin.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        showDialog(0);
        t = new Thread() {
            public void run() {
                register();
                try {
                    Thread.sleep(5000);
                    removeDialog(0);

            e.printStackTrace();
        }
            }
        };
        t.start();
    }
});
   @Override
  protected Dialog onCreateDialog(int id) {
   switch (id) {
    case 0: {
    dialog = new ProgressDialog(this);
    dialog.setMessage("Registering...");
    dialog.setIndeterminate(true);
    dialog.setCancelable(true);
    return dialog;
}
}
return null;
} 

1 个答案:

答案 0 :(得分:4)

创建一个计数器,计算对话框创建的次数,然后创建一个处理程序,通过该处理程序发送消息是否显示对话框或dismis。

请参阅以下代码。它完全符合您的要求

实施例

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity{

    Button btn = null;
    ProgressDialog dialog = null;
    int counter = 0;
    Thread t =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myxml);

    btn = (Button) findViewById(R.id.btn001);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
//          tv.setHint(tv.getHint());
//          while(counter<2){

            t = new Thread() {
                public void run() {
//                  register();
                    try {
                        while(counter<2){
                        updateGallery(0);
                        Thread.sleep(5000);

                        updateGallery(1);
                        Thread.sleep(1000);

                        }

            }catch(Exception ex){
                ex.printStackTrace();
            }
                }
            };
            t.start();

            }
//          tv.setTextColor(getResources().getColor(R.color.DarkBlue));
//      }
    });



//  tv.setFreezesText(true);
}




@Override
protected Dialog onCreateDialog(int id) {
 switch (id) {
  case 0: {

      ++counter;
  dialog = new ProgressDialog(this);
  if(counter==1){
  dialog.setMessage("Registering...");
  }else{
      dialog.setMessage("Registered successfully");
  }
  dialog.setIndeterminate(true);
  dialog.setCancelable(true);

}

}
 return dialog;
}

public void updateGallery(int actionsToBePerformedOnScreen) {
    Message msg = new Message();
    msg.what = actionsToBePerformedOnScreen;
    galleryListHandler.sendMessage(msg);

}

public Handler galleryListHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case 0:

            showDialog(0);

            break;

        case 1:
            // clear all images in the list
            removeDialog(0);
            break;


        }

    };
};
}