如何从对话框中启动xml活动?

时间:2013-03-02 07:26:06

标签: java android eclipse android-activity adt

我正在尝试加载用户单击下面的链接按钮时的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();
        }
    });
}
}

3 个答案:

答案 0 :(得分: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/

答案 1 :(得分: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);
            }
          })

答案 2 :(得分: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();
            }
          })
相关问题