如何在Android中从对话框启动活动

13
我创建了一个自定义对话框,我想在单击"确认"按钮时启动新的活动。如何获取上下文并将其设置为Intent构造函数的第一个参数?
我可以使用getContext()创建意图,但我无法调用startActivity。我应该将调用对话框的活动传递给对话框的构造函数吗?这是通过单击对话框启动活动的常规方式吗?
public class CustomDialog extends Dialog implements OnClickListener {
    Button okButton, cancelButton;

    public CustomDialog(Context context) {      
        super(context);     
        setContentView(R.layout.custom_dialog);
        okButton = (Button) findViewById(R.id.button_ok);
        okButton.setOnClickListener(this);
        cancelButton = (Button) findViewById(R.id.button_cancel);
        cancelButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {       
        if (v == cancelButton)
            dismiss();
        else {
            Intent i = new Intent(getContext(), ItemSelection.class);
            startActivity(i); //The method startActivity(Intent) is undefined for the type CustomDialog
        }
    }
}
7个回答

19
public class CustomDialog extends Dialog implements OnClickListener {
  Button okButton, cancelButton;
  Activity mActivity;

  public CustomDialog(Activity activity) {      
    super(activity);
    mActivity = activity;
    setContentView(R.layout.custom_dialog);
    okButton = (Button) findViewById(R.id.button_ok);
    okButton.setOnClickListener(this);
    cancelButton = (Button) findViewById(R.id.button_cancel);
    cancelButton.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {       
    if (v == cancelButton)
        dismiss();
    else {
        Intent i = new Intent(mActivity, ItemSelection.class);
        mActivity.startActivity(i);
    }
  }
}

似乎我无法调用startActivity:“CustomDialog类型未定义startActivity(Intent)方法” - jul
我已经进行了更正。你可以选择这种方式,或者在你的活动中声明Dialog类,这将直接访问“Activity”的方法。 - EricLarch

5
Intent i = new Intent(getBaseContext(), ItemSelection.class);

这对我很有帮助,尽管结构不同,对话框没有类。

4

@dhaag23,你甚至不需要做那么多工作!

调用getContext()

这将返回传递给对话框构造函数的Context


Cheezmeister,你是对的。其他人可以看到Dialog.java的源代码。 - Yura Shinkarev

2

就像Cheezmeister所写的一样,获取Activity并不是必要的。你可以像这样简单地使用上下文:

Intent i = new Intent(getContext(), ItemSelection.class);
getContext().startActivity(i);

2

简单,只需在本地变量中保存传递给CustomDialog构造函数的上下文。


1
我建议您使用这个。它让事情变得非常简单:

AlertDialog.Builder dialog = new AlertDialog.Builder(RegistrationActivity.this);
dialog.setCancelable(false);
dialog.setTitle("Error Alert");
dialog.setMessage(info[1]);
dialog.setPositiveButton("ok", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
        Intent intent = new Intent(RegistrationActivity.this, RegistrationActivity.class);

        startActivity(intent);
    }
})
.setNegativeButton("", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
});

final AlertDialog alert = dialog.create();
alert.show();

info[1] 是我的数据,将被显示。您可以用自己的信息替换它。


0

如果你正在开发一个DialogFragment,你可以这样做:

public class MyDialogFragment : DialogFragment
    {
        private Context _Context;
        ...

         public override void OnActivityCreated(Bundle savedInstanceState)
        {
            _Context = Context;

            ...


            alertDialog.SetButton("Start Activity Button", delegate
            {
                var uri = Android.Net.Uri.Parse("https://.....");
                var intent = new Intent(Intent.ActionView, uri);
                _Context.StartActivity(intent);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接