2012-04-02 11 views
6

Có hoạt động đầu tiên mà người dùng lưu chi tiết của họ. sau khi nhấp vào nút lưu một Alertdialog hỏi về ok hoặc hủy bỏ. nếu người dùng nhấp vào ok thì một hoạt động mới sẽ bắt đầu.Cách bắt đầu một lớp hoạt động mới trong cửa sổ bật lên AlertDialog bằng cách chọn nút ok

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; 
    } 

Trả lời

-1

này nên làm các trick

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

Bạn không thể đơn giản sử dụng 'điều này' khi tạo ý định từ bên trong giao diện AlertDialog. – VarnerBeast14

0

Trong ok hoặc Hủy bỏ nút bấm bạn có thể viết những dòng này,

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

sử dụng mã này

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 Không làm việc tôi đã thử điều này. –

+0

kiểm tra câu trả lời của tôi ngay bây giờ, tôi đã được thêm builder.show(); trong mã nó hoạt động ngay bây giờ. –

+0

http://stackoverflow.com/questions/9974564/how-to-start-a-new-activity-class-in-alertdialog-popup-by-choosing-ok-button đây là catlogerror E/AndroidRuntime (29931) : java.lang.RuntimeException: Không thể bắt đầu hoạt động ComponentInfo {mks.gks.sks/mks.gks.sks.CheckPatient}: java.lang.IllegalArgumentException: giá trị liên kết tại chỉ mục 1 là null –

0

Do something như dưới đây cho điều này.

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

trong sự thay đổi mã của bạn:

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

để

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

hoặc

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

http://stackoverflow.com/questions/9974564/how-to-start-a-new-activity -class-in-alertdialog-popup-by-select-ok-button không hoạt động hiển thị AndroidRuntime (19707): java.lang.RuntimeException: Không thể bắt đầu hoạt động ComponentInfo {mks.gks.sks/mks.gks.sks.CheckPatient} : java.lang.IllegalArgumentException: giá trị liên kết tại chỉ mục 1 là null –

+0

bạn có thể chỉnh sửa bài đăng bằng mã CheckPatient không. –

+0

bez bạn có lỗi trong hoạt động CheckPatient không có trong hộp thoại –

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(); 

Sử dụng mã ở trên :::

2

Tôi biết đó là quá muộn nhưng tôi muốn trả lời này vì đã giúp người khác, điều này làm việc cho tôi:

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

bối cảnh là bối cảnh của activiy hiện hành.

+0

Cảm ơn @Valentina_Siii Nó phù hợp với tôi –