<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:text="@string/button1"
android:onClick="onClickButton"/>
public void onClickButton(View view){
TextView textview = (TextView) findViewById(R.id.textView1);
textview.setVisibility(View.VISIBLE);
}
That is the code that makes the text appear in the main activity interface when the button is pressed. What is the point of passing in an View object when you don't use it in the "onClickButton" method block? I am asking this because the app crashes if I leave out the parameter even when I am not using the view object in the code block.
Objects aren't passed at all. By default, the argument is evaluated and its value is passed, by value, as the initial value of the parameter of the method you're calling. Now the important point is that the value is a reference for reference types - a way of getting to an object (or null).
Note: When an object reference is passed to a method, the reference itself is passed by use of call-by-value. However, since the value being passed refers to an object, the copy of that value will still refer to the same object that its corresponding argument does.
In the latest version of C#, which is C# 9 at this time of writing, objects are by default passed by ref. So any changes made to the object in the calling function will persist in the object in the called function. Show activity on this post.
Since values of ‘a’ and ‘b’ are not the same for both the references, so if (condition) is false, so else block will execute, and false will be returned. In Java we can pass objects to methods as one can perceive from the below program as follows: One of the most common uses of object parameters involves constructors.
You might have several buttons in your layout, and only one method in your activity's code. In such a situation, it becomes necessary to differentiate between different buttons.
That's where this can be used.
public void onClickButton(View view){ if(view.getId() == R.id.buttonSave){ // Do something } else if(view.getId() == R.id.buttonCancel){ // Do something else } }
Although, you can bind different methods to different views, by having a method for each view type.
Yet another use case could be:
After clicking on the button, you want to modify the button itself, say, hide it, or change the label, then you obviously need a reference to the button.
what is the point of passing in an object view of type View when you don't use it in the "onClickButton" method block?
First of all, the Button Docs tell you to. Secondly, it can get used in the function. Since Button
s aren't the only View
s which are clickable
, having the View
parameter allows you to check that View
type to see what has been pressed and to perform other operations with it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With