我创建了一个自定义对话框,我想在单击“确定”时开始一个新活动。如何获取上下文以将其设置为 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
}
}
}