2013-03-02 96 views
0

我試圖加載一個XML活動,當用戶點擊下面的鏈接按鈕時。如何從對話框中啓動xml活動?

有人可以協助嗎?我不知道怎麼做(和它的駕駛我堅果!)

所有我想要做的是,當用戶點擊「鏈接」讓他們派人過來AppActivity2.java/main2.xml

package com.mkyong.android; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class AppActivity extends Activity { 

final Context context = this; 
private Button button; 

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    button = (Button) findViewById(R.id.button1); 

    // add button listener 
    button.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 

     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
      context); 

     // set title 
     alertDialogBuilder.setTitle("Settings Menu"); 

     // set dialog message 
     alertDialogBuilder 
      .setMessage("Link or Delete?") 
      .setCancelable(false) 
      .setPositiveButton("Link",new  DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id) { 
        // if this button is clicked, close 
        // current activity 
        AppActivity.this.finish(); 
       } 
       }) 
      .setNegativeButton("Delete",new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id) { 
        // if this button is clicked, just close 
        // the dialog box and do nothing 
        dialog.cancel(); 
       } 
      }); 

      // create alert dialog 
      AlertDialog alertDialog = alertDialogBuilder.create(); 

      // show it 
      alertDialog.show(); 
     } 
    }); 
} 
} 
+1

你是知道的意圖?它從一個頁面轉到另一個頁面。 – Shachi 2013-03-02 07:29:45

+0

請參考Android意圖以及如何在按鈕點擊時觸發它們,您應該在發佈您的問題之前進行調查 – 2013-03-02 07:32:00

+0

我不是,但我只是通過http://developer.android.com/reference閱讀了一下/android/content/Intent.html 但是我仍然不確定我可以添加到這個切換到另一個頁面的代碼行。 – 2013-03-02 07:35:03

回答

0
final Dialog dialog = new Dialog(context); 
     dialog.setContentView(R.layout.custom); 
     dialog.setTitle("Title..."); 

     // set the custom dialog components - text, image and button 
     TextView text = (TextView) dialog.findViewById(R.id.text); 
     text.setText("Android custom dialog example!"); 
     ImageView image = (ImageView) dialog.findViewById(R.id.image); 
     image.setImageResource(R.drawable.ic_launcher); 

     Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK); 
     // if button is clicked, close the custom dialog 
     dialogButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 


        Intent it = new Intent(AppActivity.this, urActivity.class); 
        startActivity(it); 
        dialog.dismiss(); 
      } 
     }); 

     dialog.show(); 

也請查看下面的鏈接瞭解更多詳情。

http://www.mkyong.com/android/android-custom-dialog-example/

+0

這是我用來構建項目的頁面 - 但現在,當用戶單擊上面代碼中的「鏈接」時,現在我需要知道如何移至其他頁面(可能使用意圖?)。 – 2013-03-02 07:37:31

+0

你有使用意向 – duggu 2013-03-02 07:41:11

+0

檢查更新回答 – duggu 2013-03-02 07:43:21

0

你必須使用一個意圖打開所需的活動。該代碼將是這樣的:

//create the intent (must be final in order to be able to acces it from the inner class) 
final Intent nextActivityIntent = new Intent(this, AppActivity2.class); 
alertDialogBuilder 
     .setMessage("Link or Delete?") 
     .setCancelable(false) 
     .setPositiveButton("Link",new  DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog,int id) { 
       // if this button is clicked, close 
       // current activity 
       AppActivity.this.finish(); 
       //start the activity using the intent 
       startActivity(intent); 
      } 
      }) 
0

在你setPositiveButton方法塊,寫代碼,即可開始活動

 .setPositiveButton("Link",new  DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog,int id) { 
       //start new activity 

       Intent intentAppActivity2 = new Intent(AppActivity.this, AppActivity2.class); 
       startActivity(intentAppActivity2); 

       // if this button is clicked, close 
       // current activity 
       AppActivity.this.finish(); 
      } 
      })