java 将 View 类对象作为参数传递给按钮调用的方法(View 视图)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13971026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing the View class object as a parameter to the method invoked by a button (View view)
提问by willsantoso
I'm trying to create an app for Android, and I follow this tutorial http://developer.android.com/training/basics/firstapp/starting-activity.html
我正在尝试为 Android 创建一个应用程序,我按照本教程http://developer.android.com/training/basics/firstapp/starting-activity.html
there is a part
有一部分
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
then I followed this tutorial and everything worked, untill I remove parameter View view
然后我按照本教程进行操作,一切正常,直到我删除参数 View view
my question is just why everytime I remove it, so the function just be:
我的问题是为什么每次我删除它,所以功能只是:
/** Called when the user clicks the Send button */
public void sendMessage() {
// Do something in response to button
}
and I run the app, it forced close.
我运行该应用程序,它强制关闭。
could anyone enlighten me? thank you
有人可以启发我吗?谢谢
采纳答案by David M
the method (typically called onClick(View view)
) method takes a parameter (the View it is associated with). see http://developer.android.com/reference/android/view/View.OnClickListener.htmlfor more information on the function. if you leave off the parameter you wouldn't expect it to work, would you?
方法(通常称为onClick(View view)
) 方法接受一个参数(与其关联的视图)。有关该功能的更多信息,请参阅http://developer.android.com/reference/android/view/View.OnClickListener.html。如果您不使用该参数,您就不会期望它起作用,对吗?
回答by Aleks G
If you look carefully in the XML, you'll see the following attribute on the button:
如果您仔细查看 XML,您将在按钮上看到以下属性:
android:onClick="sendMessage"
This attribute means that when the button is clicked, message
该属性表示当按钮被点击时,消息
public void sendMessage(View view)
is invoked. This is due to the fact that onClick
method in the OnClickListener
interface requires a parameter of type View
. When you remove the parameter, android still attempts to call method sendMessage(View view)
but that method does not exist any more, therefore you get a force-close.
被调用。这是因为接口中的onClick
方法OnClickListener
需要一个类型为 的参数View
。当您删除参数时,android 仍会尝试调用方法,sendMessage(View view)
但该方法不再存在,因此您会强制关闭。
Parameter view
is the actual view (button in your case) that was clicked. With this, you can assign multiple buttons to invoke the same method and inside the method check which button was clicked.
参数view
是被点击的实际视图(在你的例子中是按钮)。有了这个,您可以分配多个按钮来调用相同的方法,并在方法内部检查单击了哪个按钮。
If you want to have the method without the parameters, then you should assign it in the code instead of the XML. Change your XML to be
如果您想要不带参数的方法,那么您应该在代码中而不是在 XML 中分配它。将您的 XML 更改为
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:id="@+id/send_button" />
(Note that android:onClick
attribute is now removed and android:id
is added.) Then in your activity in onCreate
method you would add the following line:
(请注意,android:onClick
现在已删除并android:id
添加属性。)然后在onCreate
方法中的活动中添加以下行:
this.findViewById(R.id.send_button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendMessage();
}
}
As you can see, this is quite a bit more code to write, but it does provide you with more flexibility should you need it.
如您所见,要编写的代码要多得多,但如果您需要,它确实为您提供了更大的灵活性。
回答by ben75
When you put this kind of thing in your xml :
当你把这种东西放在你的 xml 中时:
android:onClick="sendMessage"
The android framework will add an OnClickListener on your button. This "automatically" generated OnclickListener will try to call a method named "sendMessage" with one single argument of type View.
android 框架将在您的按钮上添加一个 OnClickListener。这个“自动”生成的 OnclickListener 将尝试使用一个 View 类型的参数调用名为“sendMessage”的方法。
If this method doesn't exists it simply crash.
如果此方法不存在,它只会崩溃。