getActivity()在哪里定义的? [英] getActivity() where it is defined?

查看:81
本文介绍了getActivity()在哪里定义的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android的新手,并且正在关注这个示例.

I'm very new to android and I'm following this example.

代码说我们需要执行以下步骤来获得一个对话框:

The code says we need to do these steps to get an dialog box:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

// 2. Chain together various setter methods to set the dialog characteristics
builder.setMessage(R.string.dialog_message)
       .setTitle(R.string.dialog_title);

// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();

但是getActivity()方法在哪里定义?

我找不到那个方法.

推荐答案

getActivity()方法提供了Activity的上下文.您可以使用YourActivityName.this代替它.

The getActivity() method gives the context of the Activity. You can use YourActivityName.this instead of it.

getActivity()方法通常用于片段中,以获取插入或夸大它们的活动的上下文.

The method getActivity() is normally used in fragments to get the context of the activity in which they are inserted or inflated.

例如

AlertDialog.Builder builder = new AlertDialog.Builder(YourActivityName.this);

或者如果您是在活动本身中编写代码

or if you are writing the code in your activity itself even

AlertDialog.Builder builder = new AlertDialog.Builder(this);

将锻炼.请随时提出任何疑问

will workout. Please feel free to ask any doubts

这篇关于getActivity()在哪里定义的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆