简体   繁体   English

什么参数应该传递给.show()?

[英]What arguments should be passed to .show()?

So I'm pretty new to Android development, and Java programming in general (well, okay, so programming period...), so I came across something that I found the answer to online, but I want to know WHY it works. 因此,我对Android开发和Java编程(例如,好的,所以编程期...)很陌生,所以我遇到了一些可以在网上找到答案的东西,但是我想知道为什么它可以工作。

I created a class that was concerned with creating an AlertDialog, based off of the code on that Android Tutorial website. 我基于该Android教程网站上的代码创建了一个与创建AlertDialog有关的类。 Then, in a different activity, I created a method that instantiated an object of this AlertDialog class. 然后,在另一个活动中,我创建了一个实例化AlertDialog类的对象的方法。 Then, I tried to use .show() to actually call up the AlertDialog. 然后,我尝试使用.show()实际调用AlertDialog。

It wouldn't work without any arguments, so the two arguments I had to pass to it were getFragmentManager(), and any kind text, as long as it was in quotations, like this: 没有任何参数它是行不通的,因此,我必须传递给它的两个参数是getFragmentManager()和任何类型的文本,只要它用引号引起来就可以像这样:

alertDialogObject.show(getFragmentManager(), "Hi"); alertDialogObject.show(getFragmentManager(),“ Hi”);

Finally after writing it like this the errors went away, and the AlertDialog box popped uo in my app just fine. 最终,在像这样编写之后,错误消失了,AlertDialog框在我的应用程序中弹出uo。 Can someone explain to me the basics of what I passed to show(), and what kind of arguments show() wants? 有人可以向我解释我传递给show()的基本知识,以及show()需要什么样的参数吗?

Thanks for the help!!! 谢谢您的帮助!!!

Without a link to the tutorial you're referring to, I'm going to assume you're talking about the implementation of a DialogFragment , based on the code snippet: 如果没有指向您要参考的教程的链接,我将假设您正在基于代码片段谈论DialogFragment的实现:

alertDialogObject.show(getFragmentManager(), "Hi");

For the best answer to your question as to "what I passed to show() " and "what kind of arguments show() wants " , you should probably refer to the documentation on the DialogFragment class . 为了获得最佳的回答你的问题,以“我传给show()“什么样的参数show() wants ”,你或许应该参考的文档DialogFragment

To answer the second question more specifically, have a look at the two available show(...) methods. 为了更具体地回答第二个问题,请看一下两个可用的show(...)方法。 These are called 'overloads' in Java terminology by the way: methods that have the same name, but accept different parameters. 这些在Java术语中被称为“重载”:具有相同名称但接受不同参数的方法。 In the documentation, every method usually has short description explaining what it does, what its return value is and what parameters are expected. 在文档中,每个方法通常都有简短的说明,以解释其作用,返回值是什么以及期望使用什么参数。 No exception for these two: 这两个都不例外:

public int show(FragmentTransaction transaction, String tag)

Display the dialog, adding the fragment using an existing transaction and then committing the transaction. 显示对话框,使用现有事务添加片段,然后提交事务。

public void show(FragmentManager manager, String tag)

Display the dialog, adding the fragment to the given FragmentManager. 显示对话框,将片段添加到给定的FragmentManager中。 This is a convenience for explicitly creating a transaction, adding the fragment to it with the given tag, and committing it. 这对于显式创建事务,使用给定标签将片段添加到其中并提交的方式非常方便。 This does not add the transaction to the back stack. 这不会将事务添加到后台堆栈。 When the fragment is dismissed, a new transaction will be executed to remove it from the activity. 取消片段后,将执行新事务以将其从活动中删除。

So you have the option of calling .show(...) either with a FragmentTransaction or FragmentManager , followed by a tag. 因此,您可以选择通过FragmentTransactionFragmentManager调用.show(...) ,后跟标签。 There is a plethora of resources on the way how to work with transactions and/or the manager out there. 关于如何处理事务和/或那里的经理的方式,有很多资源。 If you like to know more about that, I suggest you start with any tutorial or Android book that explains the concepts of (and differences between) activities and fragments. 如果您想进一步了解这一点,建议您从任何教程或Android书籍开始,这些书籍或教程解释活动和片段(及其之间的区别)的概念。 If it's examples you're after, definitely go through the API demos that come with the Android SDK. 如果您要查找的是示例,请务必通过Android SDK随附的API演示进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 应该将什么作为源传递给构造函数? - What should be passed to the constructor as the source? 没有传递任何参数时,我应该在构造函数中使用关键字“ this”吗? - Should I be using keyword “this” in a constructor when no arguments are being passed? 什么上下文名称应该传递给Glide方法? - What context name should be passed to Glide method? sdkmanager 返回相同的 output,无论传递给它的是什么 arguments - sdkmanager returning the same output, regardless of what arguments are passed to it Runtime.exec() 中可以传递的最大参数数量是多少? - What is the maximum number of arguments that can be passed in a Runtime.exec()? 如果传递了错误的对象类型,我应该抛出什么类型的异常? - What type of Exception should I throw if the wrong type of object is passed? 在pickTrees中返回treeHelper方法时应该传递什么作为“树”? - What should be passed as "tree" when returning the treeHelper method in pickTrees? 计算通过的学生人数-我应该在if语句中输入什么? - Calculate the number of students who passed - what should I put in the if statement? 计算传递给方法的参数 - Count passed arguments to a method 错误的参数传递给.ksh - wrong arguments are passed to .ksh
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM