OnClick() 事件和 OnClickListener 之间的区别?
我在大多数项目中总是使用 onclick()
事件。但是,我读到了有关 OnClickListener()
的内容。谁能说出这两者之间有什么区别?哪一个最适合在 Android 应用程序中使用?
I'm always using onclick()
event in most of my projects. But, I read about OnClickListener()
. Can anyone tell what's the difference between these two? And which one is best to use in Android application?.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
我不确定这个问题是否清楚。
View.OnClickListener
是一个接口,它定义了onClick(View)
方法。如果您有一个打算侦听点击的类,则您应该实现该接口(如果尚未扩展具有此功能的类),并也实现此方法。你必须同时使用两者;它们在某种程度上并不是替代品。I'm not sure the question is clear.
View.OnClickListener
is an interface, which defines theonClick(View)
method. If you have a class which intends to listen for clicks, you should both implement the interface (if not already extending a class that does), and implement this method too. You have to use both; they're not somehow alternatives.OnClickListener
是您需要实现的接口,可以在java代码中设置为视图。最近,android 在视图中添加了一个名为
android:onclick
的 xml 属性,该属性可用于直接在视图的 Activity 中处理点击,而无需实现任何接口。两者的功能相同,只是一个通过 java 代码设置,另一个通过 xml 代码设置。
OnClickListener
is the interface you need to implement and can be set to a view in java code.Lately android added a xml attribute to views called
android:onclick
, that can be used to handle clicks directly in the view's activity without need to implement any interface.Both function the same way, just that one gets set through java code and the other through xml code.
我假设您使用的
onClick
是您在 XML 布局中定义的。这两个是具有相同功能但实现方式不同的替代方案。XML 布局中带有函数绑定的
onClick
是 onClick 和它将调用的函数之间的绑定。该函数必须有一个参数(View
)才能使 onClick 发挥作用。OnClickListener
是任何类都可以实现的接口。由于它是任何类都可以实现的接口,因此它具有更大的灵活性和更复杂的形式。OnClickListener
提供的灵活性很少
OnClickListener
使您能够将点击事件的操作/行为与触发该事件的View
分开。虽然对于简单的情况来说这并不是什么大问题,但对于复杂的事件处理来说,这可能意味着代码具有更好的可读性和可维护性I am assuming by
onClick
that you use is the one that you defines in XML Layout. These two are alternative that serve same function but implemented differently.The
onClick
with function binding in XML Layout is a binding between onClick and the function that it will call. The function have to have one argument (theView
) in order for onClick to function.An
OnClickListener
is an interface that any class could implement. Since it is an interface that any class could implement, this has more flexibility and more complex in its form. Few flexibilities that you could have withOnClickListener
OnClickListener
enable you to separate the action/behavior of the click event from theView
that triggers the event. While for simple cases this is not such a big deal, for complex event handling, this could mean better readability and maintainability of the codeOnClickListener
is an interface, the class that implements it has flexibilities in determining the instance variables and methods that it needs in order to handle the event. Again, this is not a big deal in simple cases, but for complex cases, we don't want to necessary mix up the variables/methods that related to event handling with the code of the View that triggers the event.OnClickListener 是等待某人实际点击的东西,onclick 确定当有人点击时会发生什么
监听器是一个类,onclick 是一个方法,这种区别在简单的情况下不是很有用,但是如果你想要更复杂,它就变得更必要的
OnClickListener is what waits for someone to actually click, onclick determines what happens when someone clicks
the listener is a class, the onclick is a method, this distinction is not very useful in simple cases, but if you want to be more complicated it becomes more necessary
OnClickListener是一个接口,onClick是OnClickListener的方法。
OnClickListener is an interface and onClick is method of OnClickListener.
您可能希望以编程方式设置 OnClickListener 有几个原因。第一个是您是否想在应用程序运行时更改按钮的行为。您可以将按钮完全指向另一个方法,或者通过设置不执行任何操作的 OnClickListener 来禁用该按钮。
当您使用 onClick 属性定义侦听器时,视图仅在其宿主活动中查找具有该名称的方法。以编程方式设置 OnClickListener 允许您从其宿主活动以外的其他位置控制按钮的行为。当我们了解片段时,这将变得非常相关,片段基本上是小型活动,允许您构建具有自己的生命周期的可重用视图集合,然后可以将其组装成活动。片段始终需要使用 OnClickListener 来控制其按钮,因为它们不是活动,并且不会搜索 onClick 中定义的侦听器。
There are a couple reasons why you might want to programmatically set an OnClickListener. The first is if you ever want to change the behavior of your button while your app is running. You can point your button at another method entirely, or just disable the button by setting an OnClickListener that doesn't do anything.
When you define a listener using the onClick attribute, the view looks for a method with that name only in its host activity. Programmatically setting an OnClickListener allows you to control a button's behavior from somewhere other than its host activity. This will become very relevant when we learn about Fragments, which are basically mini activities, allowing you to build reusable collections of views with their own lifecycle, which can then be assembled into activities. Fragments always need to use OnClickListeners to control their buttons, since they're not Activities, and won't be searched for listeners defined in onClick.
我们用来
在类之外定义一个方法。
但是要在类中定义组件 Click 事件,我们使用 onclick 侦听器。
We use
to define a method out of the class.
But To define a component Click event within a class, we use onclick listener.
您可能希望以编程方式设置
OnClickListener
的原因有几个。第一个是您是否想在应用程序运行时更改按钮的行为。您可以将按钮完全指向另一个方法,或者通过设置不执行任何操作的OnClickListener
来禁用该按钮。当您使用
onClick
属性定义侦听器时,视图仅在其宿主活动中查找具有该名称的方法。通过编程方式设置OnClickListener
允许您从其宿主活动以外的其他位置控制按钮的行为。这将与Fragments
非常相关,Fragments
总是需要使用OnClickListeners
来控制它们的按钮,因为它们不是活动,并且不会'不会搜索onClick
中定义的侦听器。There are a couple reasons why you might want to programmatically set an
OnClickListener
. The first is if you ever want to change the behavior of your button while your app is running. You can point your button at another method entirely, or just disable the button by setting anOnClickListener
that doesn't do anything.When you define a listener using the
onClick
attribute, the view looks for a method with that name only in its host activity. Programmatically setting anOnClickListener
allows you to control a button's behavior from somewhere other than its host activity. This will become very relevant forFragments
,Fragments
always need to useOnClickListeners
to control their buttons, since they're not Activities, and won't be searched for listeners defined inonClick
.我们在 xml 中使用
OnClick
,在java
代码中使用OnClickListner
。两者都用于执行功能。We use
OnClick
in xml andOnClickListner
injava
code . Both are use to perform a function.将“OnClickListener”视为等待用户单击应用程序按钮的人。
然后你的人将执行你的方法OnClick()。
您必须在 xml 文件中为按钮添加一个 id,然后在 MainActivity.java 文件中为其指定一个名称。然后为你的家伙设置一个点击监听器。并添加您的 onClick 方法。
这就是为什么 onClick 绑定到接口 View.OnClickListener :
https://developer.android.com/reference/android/view/ View.OnClickListener.html
示例:
Consider "OnClickListener" as a guy who is waiting your user to click the button of your app.
Then your guy will execute your method OnClick().
You have to put an id to your button in your xml file, then give it a name in your MainActivity.java file. Then set a click listener to your guy. And add your onClick method.
That's why onClick is bound to the interface View.OnClickListener :
https://developer.android.com/reference/android/view/View.OnClickListener.html
Example :
您可以在 XML 中添加
android:onClick="your_method"
属性。示例:
You can add
android:onClick="your_method"
attribute in your XML.Example:
当我们想在Java代码中为按钮添加点击监听器时,我们使用
OnClickListener
。当我们想在布局文件中为按钮添加点击监听器时,我们使用
android:onClick="your_method"
如果您使用 XML 变体,则必须在应用类中实现
your_method
。When we want to add click listener to button in Java code, we use
OnClickListener
.When we want to add click listener to button in the layout file, we use
android:onClick="your_method"
If you use XML variant, you must implement
your_method
in your app class.每个人都提到过经常使用的
OnClickListener
监听器。我想再添加一点
android:onClick
作为方法工作,它不需要引用,因此当您必须在执行某些任务后添加按钮时它非常有用,因此您无法引用它OnClickListener
。例如,当我们创建仅包含布局(无片段)的 viewpager 时,如果您将按钮放入任何布局中,则仅当布局可见时它才会初始化,因此您无法使用方法
findViewById
对于 Button 在这种情况下android:onClick
变得有用,只需将该方法放入活动中!everyone has mentioned about
OnClickListener
listner which one always used.i want to add one more point
android:onClick
works as method and it's doesn't need to be reference so it's useful when you have to add button after some task executed so you cant't referenced it forOnClickListener
.For an
example
when we create viewpager with only layout (no fragments) if you put an button in any layout it insialized only when layout visible so you can't use methodfindViewById
for Button in that caseandroid:onClick
becomed useful just put that method in activity!!这是简单的术语
如果你在家并且想打电话给某人..你可以直接打电话,他们可以听你的。 (使用 onclick)。但是,如果您在外面并且想给家里的人打电话,您需要使用电话或互联网。(需要使用 onclicklistener)。
在 Android 中,一切都从 home 开始,即 main_activity
这就是 Android 简化您工作的方式;当您有一项活动时,您不必附加侦听器、创建对象并定义它。只需使用 onClick 即可。
onclicklistener一般用在Fragments中。
所以继续编码。
Here is the simple terminology
If u are at home and U want to call someone..u can call directly and they can listen u. (use onclick). But if u are outside and u want to Call someone at home u need to use either phone or Internet.(need to use onclicklistener).
In Android everything starts from home, I.e. main_activity
This is the way android eases yr work ; when u have one activity u don't have to attach a listener, create object, and define it. Just use onClick.
Onclicklistener are generally used in Fragments.
So Keep Coding.
您可能希望以编程方式设置
OnClickListener
的原因有几个。第一个是您是否想在应用程序运行时更改按钮的行为。您可以将按钮完全指向另一个方法,或者通过设置不执行任何操作的OnClickListener
来禁用该按钮。当您使用
onClick
属性定义侦听器时,视图仅在其宿主活动中查找具有该名称的方法。通过编程方式设置OnClickListener
允许您从其宿主活动以外的其他位置控制按钮的行为。当我们谈论片段时,这将变得非常相关,片段基本上是迷你活动,允许您构建具有自己的生命周期的可重用视图集合,然后可以将其组装到活动中。片段始终需要使用OnClickListeners
来控制其按钮,因为它们不是活动,并且不会搜索onClick
中定义的侦听器。There are a couple reasons why you might want to programmatically set an
OnClickListener
. The first is if you ever want to change the behavior of your button while your app is running. You can point your button at another method entirely, or just disable the button by setting anOnClickListener
that doesn't do anything.When you define a listener using the
onClick
attribute, the view looks for a method with that name only in its host activity. Programmatically setting anOnClickListener
allows you to control a button's behavior from somewhere other than its host activity. This will become very relevant when we talk about Fragments, which are basically mini activities, allowing you to build reusable collections of views with their own lifecycle, which can then be assembled into activities. Fragments always need to useOnClickListeners
to control their buttons, since they're not Activities, and won't be searched for listeners defined inonClick
.onclick()
和setOnClicklisner()
的主要区别如下:onclick()
是xml中的一个属性。单击按钮时调用
onclick
方法假设布局中有三个按钮,您只能添加一个onclick()
函数,并且当单击任何一个按钮时onclick()
将被调用setOnClicklistner()
假设您的布局中有三个按钮,您想要对它们执行不同的操作。然后你应该在每个按钮上使用
setonClicklistner()
方法为它们提供不同的方法The main diffrence between
onclick()
andsetOnClicklisner()
is discribed as follow:onclick()
Is a attribute in xml. when a button is clicked
onclick
method is called suppose you have three button in layout you can add only one function ofonclick()
and when any of one button will clickedonclick()
will calledsetOnClicklistner()
Suppose you have three button in layout you want to perform different action from them. Then you should use
setonClicklistner()
method on each of button to give different method for them