2012-04-02 60 views
6

用戶保存其細節時有第一個活動。點擊保存按鈕後,Alertdialog詢問關於確定或取消。如果用戶點擊確定,則新的活動開始。如何通過選擇確定按鈕在警報對話框中啓動一個新的活動類

protected final Dialog onCreateDialog(final int id) { 
    Dialog dialog = null; 
    switch(id) { 
    case DIALOG_ID: 
     AlertDialog.Builder builder = new AlertDialog.Builder(AppointInformation.this); 
     builder.setMessage("Information saved successfully ! Add Another Info?") 
     .setCancelable(false) 
     .setPositiveButton("No", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int id) { 

startActivity(new Intent(((Dialog)dialog).getContext(),CheckPatient.class));  
      } 
     }) 
     .setNegativeButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.cancel(); 
      } 
     }); 
     AlertDialog alert = builder.create(); 
     dialog = alert; 
     break; 

    default: 

    } 
    return dialog; 
    } 

回答

-1

這應該做的伎倆

Intent intent = new Intent(this, NewActivity.class); 
startActivity(intent); 
+0

從AlertDialog界面中創建意圖時,不能簡單地使用「this」。 – VarnerBeast14 2014-08-12 05:23:16

0

在確定或取消按鈕,點擊就可以寫這篇文章,

Intent intent = new Intent(this, NewActivity.class); 
startActivity(intent); 
0

使用此代碼

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setMessage("Are you sure you want to exit?") 
    .setCancelable(false) 
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      Intent intent = new Intent(PresentActivity.this, NextActivity.class); 
      startActivity(intent); 
     } 
    }) 
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      dialog.cancel(); 
     } 
    }); 
builder.show(); 
+0

http://stackoverflow.com/questions/9974564/how-to-start-a-new-activity-class-in-alertdialog-popup-by-choosing-ok-button不工作,我已經嘗試過。 – 2012-04-02 10:50:21

+0

現在檢查我的答案,我添加了builder.show();在代碼中它現在工作。 – 2012-04-02 10:56:39

+0

http://stackoverflow.com/questions/9974564/how-to-start-a-new-activity-class-in-alertdialog-popup-by-choosing-ok-button這是catlogerror E/AndroidRuntime(29931) :java.lang.RuntimeException:無法啓動活動ComponentInfo {mks.gks.sks/mks.gks.sks.CheckPatient}:java.lang.IllegalArgumentException:索引1處的綁定值爲空 – 2012-04-02 11:08:44

0

做些什麼如下所示。

AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(AndroidAlertDialog.this); myAlertDialog.setTitle("--- Title ---"); 
     myAlertDialog.setMessage("Alert Dialog Message"); 
     myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface arg0, int arg1) { 
     Intent intent = new Intent(this, NextActivity.class); 
     startActivity(intent); 
    }}); 

     myAlertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface arg0, int arg1) { 

     // do something when the Cancel button is clicked 
     }}); 

     myAlertDialog.show(); 
1
在你的代碼更改

startActivity(new Intent(((Dialog)dialog).getContext(),CheckPatient.class)); 

startActivity(new Intent(getBaseContext(),CheckPatient.class)); 

startActivity(new Intent(Activityname.this,CheckPatient.class)); 
+0

http://stackoverflow.com/questions/9974564/how-to-start-a-new-activity顯示AndroidRuntime(19707):java.lang.RuntimeException:無法啓動活動ComponentInfo {mks.gks.sks/mks.gks.sks.CheckPatient}不能啓動活動ComponentInfo {mks.gks.sks/mks.gks.sks.CheckPatient} :java.lang.IllegalArgumentException:索引1處的綁定值爲空 – 2012-04-02 10:53:32

+0

您可以使用CheckPatient代碼編輯帖子。 – 2012-04-02 10:57:22

+0

bez你在checkPatient中有bug不在對話框中活動 – 2012-04-02 10:59:12

0
AlertDialog.Builder alert = new AlertDialog.Builder(getApplicationContext()); 
    alert.setTitle(title); 
    alert.setCancelable(false); 
    alert.setMessage(message); 
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
      startActivity(new Intent(YourActivity.this, NewActivity.class)); 
     } 
    }); 
    alert.show(); 

使用上面的代碼:::

2

我知道一切都太遲了,但我想回答這個幫助別人,這個工作對我來說:

Intent intent = new Intent(getContext(),OtherActivity.class); 
       context.startActivity(intent); 

上下文是當前activiy的情況下。

+0

謝謝@Valentina_Siii它適用於我 – 2018-02-27 21:00:37

相關問題