2011-05-02 40 views
0

我是Android新手。 我面臨一些麻煩 我的情景是 - 當我點擊按鈕,所以打開alertDailog和alertdailog有兩個按鈕像發送和取消,當我點擊發送按鈕,我想打開ProgressBar因爲發送按鈕有havy的內容,所以採取更多時間。如何打開alertDailog裏面的ProgressBar?

即時通訊使用的處理器,但使用此代碼

btn.setOnClickListener(新View.OnClickListener(){ 公共無效的onClick(視圖v){

   handler.sendEmptyMessage(0); 

     } 

    }); 
    alert = new Dialog(ProgramDetailActivity.this); 

     alert.setContentView(R.layout.dialog_email); 
     alert.setTitle("     Enter mail info"); 
     alert.setCancelable(true); 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, 
        int id) { 
       finish(); 
      } 
     }; 
      btnSend = (Button) alert.findViewById(R.id.btnsend); 
     btnBack = (Button) alert.findViewById(R.id.btncancel); 

     alert.show(); 
沒有發現任何精確解

IM

btnSend.setOnClickListener(新View.OnClickListener(){

  @SuppressWarnings("null") 
         public void onClick(View v) { 
         handler2.sendEmptyMessage(0); 

//執行長時間運行 progressDialog.dismiss();

      toast=Toast.makeText(ProgramDetailActivity.this,"Mail has been sent Sucessfully!", 

Toast.LENGTH_LONG); toast.show();

} }

私人處理程序handler2 =新的處理程序(){ 公共無效的handleMessage(消息MSG){ alert.hide(); alert.cancel(); alert.dismiss();

progressDialog = ProgressDialog.show(v.getContext(),        "Email Sending", "Please wait..."); 
    } 
}; 
+0

你嘗試過什麼? – Venky 2011-05-02 10:52:29

+1

顯示代碼.... – 2011-05-02 11:03:26

+0

點擊按鈕時,我將請求傳遞給handler.then處理程序,打開alert dailog。 – 2011-05-02 11:05:55

回答

1

我也用Progressdialog在Alertdialog.The代碼的click事件低於:

AlertDialog.Builder submitalert = new AlertDialog.Builder(Summary.this); 
submitalert.setTitle(getResources().getString(R.string.app_name)) 
.setMessage("Submit the data to database?") 
.setPositiveButton("Yes", new DialogInterface.OnClickListener(){ 

       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
        update.setTitle(getResources().getString(R.string.app_name)); 
        update.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
        update.setCancelable(true); 
        update.setMax(100); 
        update.show(); 


        Thread background = new Thread (new Runnable() { 
          public void run() { 
           try { 
            // enter the code to be run while displaying the progressbar. 
            // 
            // This example is just going to increment the progress bar: 
            // So keep running until the progress value reaches maximum value 
            while (update.getProgress()<= update.getMax()) { 
             // wait 500ms between each update 
             Thread.sleep(500); 

             // active the update handler 
             progressHandler.sendMessage(progressHandler.obtainMessage()); 
            } 

           } catch (java.lang.InterruptedException e) { 
            // if something fails do something smart 
           } 
          } 
         }); 

         // start the background thread 
         background.start(); 
         if(update.getProgress()== 100) 
          { 
           update.dismiss(); 
          } 
        } 




      }) 
      .setNegativeButton("No", new DialogInterface.OnClickListener(){ 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 

       } 
      }) 
      .show(); 
+0

謝謝,你的線程概念是真正解決我所有的問題。 – 2011-05-04 07:40:33