【问题标题】:How do I start an xml activity from a dialog?如何从对话框启动 xml 活动?
【发布时间】:2013-03-02 07:26:05
【问题描述】:

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

【问题讨论】:

  • 你知道意图吗?它将您从一个页面带到另一个页面。
  • 请参考android意图以及如何在按钮单击时触发它们,您应该在发布问题之前进行研究
  • 没有“XML 活动”之类的东西。我认为需要一些 Android 教程。

标签: java android eclipse android-activity adt


【解决方案1】:

您必须使用意图来打开所需的活动。代码将如下所示:

//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】:

    在您的 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();
                }
              })
    

    【讨论】:

      【解决方案3】:
      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/

      【讨论】:

      • 这是我用来构建项目的页面 - 但现在我需要知道当用户单击上面代码中的“链接”时如何移动到另一个页面(可能使用意图?)。
      • 太棒了!我明白了...唯一的问题是现在我收到了无法实例化类型 Intent 错误 - 有什么建议吗?
      • 啊啊啊啊!在删除错误并重新保存并确认它们没有回来并且整个应用程序强制使用上面的脚本关闭之后,我尝试通过模拟器运行代码。 :(
      • 无法理解你,所以现在你面临的问题或错误是什么??
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 2011-12-31
      相关资源
      最近更新 更多