在android上实现OnClickListener时遇到问题
我想为我的主视图上的按钮实现一个单击侦听器。我的代码如下所示
protected void onCreate(Bundle savedValues) {
...
// Capture our button from layout
Button button = (Button)findViewById(R.id.btnFinish);
// Register the onClick listener with the implementation above
button.setOnClickListener(mFinishListener);
...
}
private OnClickListener mFinishListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
,但显示错误如下,
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (DialogInterface.OnClickListener) MobileTrackerActivity.java /MobileTracker/src/com/example/mobiletracker line 37 Java Problem
我不知道该怎么做。请帮忙。
I want to implement a click listener for a button on my main view. My code is something like below
protected void onCreate(Bundle savedValues) {
...
// Capture our button from layout
Button button = (Button)findViewById(R.id.btnFinish);
// Register the onClick listener with the implementation above
button.setOnClickListener(mFinishListener);
...
}
private OnClickListener mFinishListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
But shows me error as follows
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (DialogInterface.OnClickListener) MobileTrackerActivity.java /MobileTracker/src/com/example/mobiletracker line 37 Java Problem
I have no idea what to do. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您没有使用正确的接口来实例化
mFinishLinstener
变量...您可能有一个指定
DialogInterface
的导入,这会混淆视图。尝试显式指定
View.OnClickListener
。You are not using the correct interface to instantiate the
mFinishLinstener
variable...It is possible you have an import specifying
DialogInterface
and that is confusing the view.Try specifying
View.OnClickListener
explicitly.根据我的意见,实现按钮单击事件的最佳方法。
您可以使用 android:onClick 属性为 XML 布局中的按钮分配一个方法,而不是将 OnClickListener 应用于活动中的按钮。例如:
现在,当用户单击按钮时,Android 系统会调用 Activity 的 selfDestruct(View) 方法。为了使其工作,该方法必须是公共的并接受视图作为其唯一参数。例如:
注意:上面的代码在 Android SDK - Button 中给出。
As per my opinion Best way to implement On click event for the Button.
Instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick attribute. For example:
Now, when a user clicks the button, the Android system calls the activity's selfDestruct(View) method. In order for this to work, the method must be public and accept a View as its only parameter. For example:
Note: The above code is given in Android SDK - Button.
试试这个代码::
try this code :::
只需尝试一下这个:
Simply try this one as:
你也可以使用像下面的代码..
you can also use like below code..
您还可以在 xml 中声明 onclick。
在您的代码中,您可以将函数定义为:
You can also declare the onclick in the xml.
And in your code you would define the function as: