为什么我们必须在onClick方法中添加“视图"作为参数,它的作用是什么? [英] Why do we have to add 'View' as parameter in onClick method and what it does?

查看:170
本文介绍了为什么我们必须在onClick方法中添加“视图"作为参数,它的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Android Studio中的Button上设置一个事件侦听器,以更改TextView中的文本,至此,我们定义了OnClick方法.在其参数列表中,它要求View对象.谁能解释它的作用?我不明白View类的对象将在哪里使用.

I'm setting up a Event Listener on a Button in Android Studio for changing the text inside the TextView and came to this point were we define OnClickmethod. In its parameter list it ask for View Object. Can anyone explain what it does? I can't understand where the object of View class is going to get used.

推荐答案

可以在其中重用事件处理程序,例如OnClick方法,在您的情况下,View参数是触发该方法的Button实例-多个Button可以具有相同的OnClick处理程序,在该方法内部,您可以检查触发了哪个Button(如果有多个)并做出相应的反应.

It is there to let you reuse event handlers like the OnClick method, the View parameter is in your case the Button instance that has fired the method - multiple Buttons can have the same OnClick handler, inside the method you can check which of the Buttons has fired (if there are more than one) and react accordingly.

实际上,这不仅对于Android上的事件驱动编程而言非常典型,而且对于所有现代UI编程(iOS,Windows,OS/X等)而言都是非常典型的.

Actually it is very typical for event-driven programming not only on Android but all contemporary UI programming - iOS, Windows, OS/X, etc.

还有一点点:参数的类型是View而不是Button,因为不仅按钮对OnClick做出反应,而且UI对象对OnClick做出反应的常见类型是View.

There is a little bit more to that: the parameter's type is View and not Button because not only Buttons react to OnClick and the common type of UI objects reacting to OnClick is View.

现在,您不仅可以以不同的方式处理事件,还可以直接访问事件的来源,以便可以进行处理-在Button的示例中,您可以更改其标题,并且可以确定您知道正在更改触发事件的UI对象的标题.

Now not only you can handle the event differently, you have also direct access to the source of the event so that you can work on it - in the example of the Button you can change its caption and you know for sure that you are changing the caption of the very UI object that fired the event.

因此以这种方式创建事件驱动的API具有很多意义.

So it has a lot of sense to do event-driven APIs in this manner.

更新

注册事件处理程序(或Android-world中的ClickListener)的方法之一是直接在布局中将方法名称分配给onClick属性-当然,前提是该方法具有正确的签名,即期望有一个参数类型为View且返回类型为void-您可以在另一个答案中查看示例.

One of the methods to register an event handler (or ClickListener in Android-world) is assigning the method name to the onClick Attribute directly in the layout - provided of course that the method has the proper signature, that is expect one Parameter of type View and has void return type - you can see an example in the other answer.

另一种方法是分配一个匿名内部类作为侦听器,如下所示:

The other is to assign an anonymous inner class as a Listener like this:

findViewById(R.id.someButton).setOnClickListener(
    new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        doSomething();
      }

    });

您还可以拥有一个实现View.OnClickListener接口的类的实例,即使'Activity'本身也可以做到这一点,然后将其注册为侦听器.

You can also have an instance of a class implementing the View.OnClickListener interface, even the `Activity' itself could do it, and then register it as a listener.

事实上,它们所有的工作原理都是一样的-在按钮实例中注册了一个实现View.OnClickListener接口的类的实例,按钮(或相应视图)识别其边界内的单击并调用侦听器将自身(this)传递给侦听器.

In fact all of them work the same - there is an instance of a class implementing View.OnClickListener interface registered in the button instance, the button (or a view for that matter) recognizes a click inside its boundaries and calls a listener passing itself (this) to the listener.

我个人认为,第三种方法是最糟糕的-您只能在类中使用一种名称方法,而在布局中则使用许多按钮,因此所有这些按钮都需要使用条件代码进行处理.

Im my personal opinion the third way is the worst - you can only have one method of a name in the class but many buttons in a layout, so all of them would need to be handled using conditional code.

第一个很好,因为您可以在视觉上对其进行设置,但是鉴于它全部位于XML文件中,并且您可以为一个活动设置多种布局,因此在较大的项目中对其进行控制可能很麻烦.

The first is nice because you can set it visually, yet given that it is all in XML files and you could have multiple layouts for an activity it can be quite a mess to keep it under control in a larger project.

因此,我的最爱是第二个-它使您可以将所有逻辑保留在代码中的某个位置,例如,如果您在活动的onCreate方法中注册了所有处理程序.

My favorite is thus the second one - it allows you to keep all the logic on one place in the code, for example if you register all you handlers in the onCreate method of an activity.

这篇关于为什么我们必须在onClick方法中添加“视图"作为参数,它的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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