Button Views in android

2019-08-06 13:36发布

I have a doubt when i am specifying a onClick function on button why View is used as a parameter ? and What happens when we don't specify that view

Example:

public void ara(View button){
}

here onClick function of a button : ara

Thanks

4条回答
戒情不戒烟
2楼-- · 2019-08-06 14:08

View is being used as a parameter in the onClick() method because it's the View that the OnClickListener is attached to.

This is very useful in multiple ways, such as if the View is a TextView and you want to update it's text when the view is clicked.

Other common uses might be:

  • Setting View visibility

  • Passing the View to an ObjectAnimator for animations when clicking

  • Fetching the Context through view.getContext()

Overall, it's just a necessary parameter for you to either perform modifications on the View that the OnClickListener belongs to, or for you to use the View for other purposes or calculations.

Additionally, if you don't include the View in the onClick() method, then your code will fail to compile. This is because OnClickListener is an interface, and by default, methods inside an interface don't have a method body.

Instead, it's up to you to override the method and give it a proper method body. However, since you're overriding the method, this also means you must include the same method name, same method modifiers, and same method parameters to properly reflect the method you are overriding.

Failure to do so would mean that the required interface method isn't being overridden, so your code will fail to compile.

查看更多
老娘就宠你
3楼-- · 2019-08-06 14:14

As you can see at the documentation : Responding to Click Events.

Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Do something in response to button click
    }
}); 

The method you declare in the android : onClick attribute must have a signature exactly as shown above. Specifically, the method must:

Be public

Return void

Define a View as its only parameter (this will be the View that was clicked).

查看更多
何必那么认真
4楼-- · 2019-08-06 14:18

Because the same method could be used for several Views, then you need to know which one is emitting the event.

public void ara(View button) {
    switch (button.getId() {
        ...
    }
}
查看更多
淡お忘
5楼-- · 2019-08-06 14:26

View is a parameter I believe because it is your way of saying which view initiated the callback.

Interface definition for a callback to be invoked when a view is clicked. ViewOnClickListener

And you set it up like this:

final Button button = findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // View v is the button in this case.
                 // Here you add what to do when this callback is initiated.
             }
         });
     }

In this piece of code make note that:

  1. You should first understand that a button is just another view.
  2. Now you are declaring your Button to match the Button you have described in your layouts. This is done by final Button button = findViewById(R.id.button_id); take note of the findViewById.
  3. The whole point of a button is that when you press it, it should do something. That something this is what is known as a callback. This is why you are doing button.setOnClickListener (which is from View.setOnClickListener). You are basically saying that the Button is the view that is talking right now and that the View.OnClickListener should listen to what this view has to say.
  4. Now we come to the final point of why there is a View v in onClick(View v). The view that is speaking will be the View v. Why is this useful? this is so you can reference the same listener (callback) multiple times. In this case we made a callback on the fly when we did new View.OnClickListener(). But what if we didn't? What if multiple buttons referenced the same listener? This is when View v becomes important. It is so we can do something like v.getId() so we know which view was speaking (initiated the callback). This function will return the id of R.id of whichever view called it. In this case we declared a button with R.id.button_id so this is how we would know that the button called it.
查看更多
登录 后发表回答