2013-03-02 93 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(); 
      } 
      })