简体   繁体   English

android方法/ java中未使用的参数

[英]Unused parameters in android method/java

In java, we use method parameters for a number of reasons but I've never used parameters in my methods unless I needed them 在Java中,出于多种原因,我们使用方法参数,但是除非有必要,否则我不会在方法中使用参数

When defining a method in android for a button that I want to click, I had my method defined in XML under the Button element 在android中为我要单击的按钮定义方法时,我在Button元素下以XML定义了我的方法

android:onClick="showText" 

then, in my ProjectName.java file I defined the method 然后,在我的ProjectName.java文件中定义了方法

public void showText(View view){  

} 

The View parameter is never used by me.. I have no need for it. 我从未使用过View参数。我不需要它。 By asking questions on Stackoverflow I was told that the View in the parameter is the view that was clicked. 通过在Stackoverflow上提问,我被告知参数中的View是被单击的视图。 Button is a subclass of View as is TextView and since that is the case, that answer sort of makes sense. Button是Text的View的子类,既然是这种情况,那么这种回答就很有意义。 But the question I have is.... is this the rule for just the onClick method? 但是我的问题是……这是仅onClick方法的规则吗? Also, why can I not pass the Button as a parameter? 另外,为什么不能将Button作为参数传递? Button is the actual "View" being clicked, so why not Button? Button是要单击的实际“视图”,那么为什么不选择Button?

Furthermore, are there other methods similar to this and if so, do they all follow the rule just like the View parameter, where let's say if I had a class called "A" and it had a subclass "B" and that class had a subclass "C", and if I wanted to use the onClick method, I can only pass the Highest Class as the parameter? 此外,还有其他与此类似的方法吗?如果是,它们是否都遵循规则,就像View参数一样,假设我有一个名为“ A”的类,并且有一个子类“ B”,而该类有一个子类“ C”,如果我想使用onClick方法,我只能传递最高类作为参数吗?

To simplify that, are there ever cases where I would pass anything else OTHER THAN "View" as the parameter to these methods? 为简化起见,在任何情况下,我都将“ View”以外的其他参数作为参数传递给这些方法吗?

Thank you. 谢谢。

It depends on the method we're talking about. 这取决于我们正在谈论的方法。 As the onClick event is generated in the View level it can only pass you a View . 由于onClick事件是在View级别生成的,因此只能为您传递View However you can easyly cast it to a Button , you just have to check if it is one. 但是,您可以轻松地将其转换为Button ,只需检查它是否为一个。

if (view instanceof Button) Button btn = (Button) view;

In other cases you may have other parameters such as a TextWatcher when you add aa text changed listener to an EditText . 在其他情况下,将文本更改的侦听器添加到EditText时,您可能具有其他参数,例如TextWatcher

The reason it uses View for the parameter type is because the method setOnClickListener is not actually a member of the Button class, but rather a member of the View class (from which button inherits). 它使用View作为参数类型的原因是因为setOnClickListener方法实际上不是Button类的成员,而是View类(从中继承按钮的类)的成员。

Here it is: http://developer.android.com/reference/android/view/View.html#setOnClickListener(android.view.View.OnClickListener) 它在这里: http : //developer.android.com/reference/android/view/View.html#setOnClickListener(android.view.View.OnClickListener)

As such, the parameter type has to be a View, because it is defined at that level. 因此,参数类型必须是View,因为它是在该级别定义的。

This is also useful because you might use the same onClick handler on several different Views. 这也很有用,因为您可能在多个不同的视图上使用相同的onClick处理程序。 Any View can have an onClick event. 任何视图都可以具有onClick事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM