简体   繁体   English

Android:如何从警报对话框启动活动

[英]Android: How to start an Activity from an alert dialog

I need to start an activity when the user chooses an item in an alert dialog. 当用户在警告对话框中选择一个项目时,我需要启动一个活动。 How do I get the context to pass to the intent constructor in the following code... 如何在上面的代码中将上下文传递给intent构造函数...

builder.setItems(items, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int item) {

                Intent i = new Intent(<WHAT DO I PUT HERE?>, <new activity>.class);    
                startActivity(i);

      }

});

Is it the use of the inner class?? 它是内部类的使用吗? Any thoughts? 有什么想法吗?

You can retrieve the context you have passed to AlertDialog.Builder with getBaseContext() . 您可以使用getBaseContext()检索已传递给AlertDialog.Builder的上下文。 See the doc here . 请参阅此处的文档。

So this should work: 所以这应该工作:

Intent i = new Intent(getBaseContext(), <new activity>.class);    
startActivity(i);
Intent i = new Intent("some.thing.activity"); 
startActivity(i);

//in the brackets you can add the activity path, ie: the package name //eg: za.ac.cut.Activity //在括号中可以添加活动路径,即:包名称//例如:za.ac.cut.Activity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM