OnClick() 事件和 OnClickListener 之间的区别?

发布于 2024-12-04 23:11:10 字数 130 浏览 0 评论 0原文

我在大多数项目中总是使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

平生欢 2024-12-11 23:11:10

我不确定这个问题是否清楚。 View.OnClickListener 是一个接口,它定义了 onClick(View) 方法。如果您有一个打算侦听点击的类,则您应该实现该接口(如果尚未扩展具有此功能的类),并也实现此方法。你必须同时使用两者;它们在某种程度上并不是替代品。

I'm not sure the question is clear. View.OnClickListener is an interface, which defines the onClick(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.

妥活 2024-12-11 23:11:10

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.

剪不断理还乱 2024-12-11 23:11:10

我假设您使用的 onClick 是您在 XML 布局中定义的。这两个是具有相同功能但实现方式不同的替代方案。

  1. XML 布局中带有函数绑定的 onClick 是 onClick 和它将调用的函数之间的绑定。该函数必须有一个参数(View)才能使 onClick 发挥作用。

  2. OnClickListener 是任何类都可以实现的接口。由于它是任何类都可以实现的接口,因此它具有更大的灵活性和更复杂的形式。 OnClickListener

    提供的灵活性很少

    • 如果需要,您可以轻松地将一个侦听器实现替换为另一个侦听器实现。
    • OnClickListener 使您能够将点击事件的操作/行为与触发该事件的 View 分开。虽然对于简单的情况来说这并不是什么大问题,但对于复杂的事件处理来说,这可能意味着代码具有更好的可读性和可维护性
    • 由于 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.

  1. 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 (the View) in order for onClick to function.

  2. 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 with OnClickListener

    • You could easily swap one listener implementation with another if you need to.
    • An OnClickListener enable you to separate the action/behavior of the click event from the View 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 code
    • Since OnClickListener 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.
绿光 2024-12-11 23:11:10

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

つ可否回来 2024-12-11 23:11:10
Button button = (Button)findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Do stuff
    }
});

OnClickListener是一个接口,onClickOnClickListener的方法。

Button button = (Button)findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Do stuff
    }
});

OnClickListener is an interface and onClick is method of OnClickListener.

糖粟与秋泊 2024-12-11 23:11:10

您可能希望以编程方式设置 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.

谁的新欢旧爱 2024-12-11 23:11:10

我们用来

    public void button_onClick_name(View v)
{
-------
}

在类之外定义一个方法。
但是要在类中定义组件 Click 事件,我们使用 onclick 侦听器。

We use

    public void button_onClick_name(View v)
{
-------
}

to define a method out of the class.
But To define a component Click event within a class, we use onclick listener.

娇柔作态 2024-12-11 23:11:10

您可能希望以编程方式设置 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 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 for Fragments, 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.

暮倦 2024-12-11 23:11:10

我们在 xml 中使用 OnClick ,在 java 代码中使用 OnClickListner 。两者都用于执行功能。

We use OnClick in xml and OnClickListner in java code . Both are use to perform a function.

疑心病 2024-12-11 23:11:10

将“OnClickListener”视为等待用户单击应用程序按钮的人。
然后你的人将执行你的方法OnClick()

您必须在 xml 文件中为按钮添加一个 id,然后在 MainActivity.java 文件中为其指定一个名称。然后为你的家伙设置一个点击监听器。并添加您的 onClick 方法。
这就是为什么 onClick 绑定到接口 View.OnClickListener :
https://developer.android.com/reference/android/view/ View.OnClickListener.html

示例:

Button myButton = (Button)findViewById(R.id.myButton);
myButton.setOnClickListener(new View.OnClickListener(){
    @override
    public void onClick(View v) {
        // your method...
    }
}

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 :

Button myButton = (Button)findViewById(R.id.myButton);
myButton.setOnClickListener(new View.OnClickListener(){
    @override
    public void onClick(View v) {
        // your method...
    }
}
凤舞天涯 2024-12-11 23:11:10

您可以在 XML 中添加 android:onClick="your_method" 属性。

示例:

 <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Click"
  android:onClick="your_method"/>

You can add android:onClick="your_method" attribute in your XML.

Example:

 <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Click"
  android:onClick="your_method"/>
作死小能手 2024-12-11 23:11:10

当我们想在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 useandroid:onClick="your_method"
If you use XML variant, you must implement your_method in your app class.

最冷一天 2024-12-11 23:11:10

每个人都提到过经常使用的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 for OnClickListener.

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 method findViewById for Button in that case android:onClick becomed useful just put that method in activity!!

傲鸠 2024-12-11 23:11:10

这是简单的术语
如果你在家并且想打电话给某人..你可以直接打电话,他们可以听你的。 (使用 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.

萌酱 2024-12-11 23:11:10

您可能希望以编程方式设置 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 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 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 use OnClickListeners to control their buttons, since they're not Activities, and won't be searched for listeners defined in onClick.

话少情深 2024-12-11 23:11:10

onclick()setOnClicklisner() 的主要区别如下:

  1. onclick()
    是xml中的一个属性。单击按钮时调用 onclick 方法假设布局中有三个按钮,您只能添加一个 onclick() 函数,并且当单击任何一个按钮时 onclick() 将被调用

  2. setOnClicklistner()
    假设您的布局中有三个按钮,您想要对它们执行不同的操作。然后你应该在每个按钮上使用 setonClicklistner() 方法为它们提供不同的方法

The main diffrence between onclick() and setOnClicklisner() is discribed as follow:

  1. 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 of onclick() and when any of one button will clicked onclick() will called

  2. setOnClicklistner()
    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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文