getActivity() 在哪里定义的? [英] getActivity() where it is defined?
问题描述
我对 android 很陌生,我正在关注这个示例.>
代码说我们需要做这些步骤才能得到一个对话框:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());//2. 将各种 setter 方法链接在一起以设置对话框特征builder.setMessage(R.string.dialog_message).setTitle(R.string.dialog_title);//3. 从 create() 中获取 AlertDialogAlertDialog 对话框 = builder.create();
但是getActivity()
方法是在哪里定义的?
我找不到那个方法.
getActivity()
方法给出了 Activity 的上下文.您可以使用 YourActivityName.this
代替它.
方法 getActivity()
通常用于片段中,以获取插入或膨胀它们的活动的上下文.
例如
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivityName.this);
或者如果你在你的活动中编写代码,甚至
AlertDialog.Builder builder = new AlertDialog.Builder(this);
会锻炼.如有任何疑问,请随时提出
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();
But where does the getActivity()
method is defined?
I can't find that method.
The getActivity()
method gives the context of the Activity. You can use YourActivityName.this
instead of it.
The method getActivity()
is normally used in fragments to get the context of the activity in which they are inserted or inflated.
eg
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屋!