获取上下文中的onClick(DialogInterface V,诠释buttonId)?文中、上下、onClick、DialogInterface

2023-09-03 22:10:54 作者:伤心总是难免的i

获取上下文中的onClick(视图中查看),回调的按钮的OnClickListener很简单:

Getting context inside onClick(View view), the callback for a button's OnClickListener is easy:

  view.getContext()

但我想不出如何获取上下文中的onClick(DialogInterface V,诠释buttonId),回调的对话框的OnClickListener:

But I can't figure out how to get context inside onClick(DialogInterface v, int buttonId), the callback for a dialog's OnClickListener:

public class MainActivity extends Activity implements android.content.DialogInterface.OnClickListener

是否有可能呢?

Is it possible at all?

推荐答案

您可以参考的外部环境,当你定义你的 DialogInterface.OnClickListener 作为一个匿名类。如果你在一个活动中,你可以使用 MyActivity.this 作为背景。

You can reference an outer context when you define your DialogInterface.OnClickListener as an anonymous class. If you're in an activity you can use MyActivity.this as the context.

修改 - 因为你的活动是实施 DialogInterface.OnClickListener ,你应该能够只使用这个作为背景。

Edit - since your Activity is implementing DialogInterface.OnClickListener, you should be able to just use this as the context.