0

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

3 回答 3

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);
            }
          })
于 2013-03-02T07:34:21.530 回答
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/

于 2013-03-02T07:29:17.143 回答
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();
            }
          })
于 2013-03-02T07:43:44.580 回答