在 onClick(DialogInterface v, int buttonId) 中获取上下文? [英] Get context inside onClick(DialogInterface v, int buttonId)?
问题描述
在 onClick(View view)
中获取上下文,按钮的 onClickListener()
回调很容易:
Getting the context inside onClick(View view)
, the callback for a button's onClickListener()
, is easy:
view.getContext()
但我不知道如何在 onClick(DialogInterface v, int buttonId)
中获取上下文,这是 dialog 的 onClickListener 的回调
But I can't figure out how to get the context inside onClick(DialogInterface v, int buttonId)
, the callback for a dialog's onClickListener
public class MainActivity extends Activity implements android.content.DialogInterface.OnClickListener
这可能吗?
推荐答案
当您将 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.
编辑 - 由于您的 Activity 正在实现 DialogInterface.OnClickListener
,您应该能够仅使用 this
作为上下文.
Edit - since your Activity is implementing DialogInterface.OnClickListener
, you should be able to just use this
as the context.
这篇关于在 onClick(DialogInterface v, int buttonId) 中获取上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!