OnClickListener 的问题

发布于 2024-12-06 17:14:13 字数 1114 浏览 0 评论 0原文

嘿,我是 Android 新手,我只想处理点击事件,但我遇到了问题...这是我的代码:

package karim.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;


public class TestActivity extends Activity implements android.view.View.OnClickListener {


    private Button b1;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        b1 = (Button) this.findViewById(R.id.button1);
        b1.setOnClickListener(this);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    @Override
    public void onClick(View v) {



    }

我收到了这个错误:

Description Resource    Path    Location    Type
The method onClick(View) of type TestActivity must override a superclass method TestActivity.java   /Test/src/karim/test    line 33 Java Problem

你能告诉我出了什么问题吗???

请具体说明!!

Hey am new to android , i just want to handle a click event , but i got problems ...thi sis my code:

package karim.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;


public class TestActivity extends Activity implements android.view.View.OnClickListener {


    private Button b1;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        b1 = (Button) this.findViewById(R.id.button1);
        b1.setOnClickListener(this);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    @Override
    public void onClick(View v) {



    }

and i got this error :

Description Resource    Path    Location    Type
The method onClick(View) of type TestActivity must override a superclass method TestActivity.java   /Test/src/karim/test    line 33 Java Problem

can you please tell me whats wrong ????

Please Be specific !!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

二智少女 2024-12-13 17:14:13

我的猜测是您正在使用 Java 5 编译器(或 IDE 中的 Java 5 编译器设置)进行编译。在 Java 5 中,@Override 只能用于重写方法的方法,而不能用于接口。在 Java 6 中扩展为接口方法重写。

更改编译器版本,或删除 onClick 方法上的 @Override 注解。

My guess is that you're compiling with a Java 5 compiler (or Java 5 compiler settings in the IDE). In Java 5, @Override was only usable on a method overriding a method of a class, not an interface. It was extended to interface method overriding in Java 6.

Change the compiler version, or remove the @Override annotation on the onClick method.

情深已缘浅 2024-12-13 17:14:13

您不需要让您的 Activity 类实现 OnClickListener。摆脱它。您只需定义侦听器并将其分配给您的按钮即可。查看官方 android 文档:

http://developer.android。 com/guide/topics/ui/ui-events.html

You don't need to have your Activity class implement OnClickListener. Get rid of that. You just need to define the listener and assign it to your button. Take a look at the official android docs:

http://developer.android.com/guide/topics/ui/ui-events.html

妞丶爷亲个 2024-12-13 17:14:13
public class TestActivity extends Activity{


    private Button b1;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        b1 = (Button) this.findViewById(R.id.button1);
        b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

    }

或者在你的情况下

public class TestActivity extends Activity implements android.view.View.OnClickListener {


    private Button b1;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        b1 = (Button) this.findViewById(R.id.button1);
        b1.setOnClickListener(this);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    @Override
    public void onClick(View v) {
    if(v==b1)
    {
       //button logic
    }

    }
public class TestActivity extends Activity{


    private Button b1;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        b1 = (Button) this.findViewById(R.id.button1);
        b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

    }

or in your case

public class TestActivity extends Activity implements android.view.View.OnClickListener {


    private Button b1;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        b1 = (Button) this.findViewById(R.id.button1);
        b1.setOnClickListener(this);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    @Override
    public void onClick(View v) {
    if(v==b1)
    {
       //button logic
    }

    }
晒暮凉 2024-12-13 17:14:13

作为“JB Nizet”答案的扩展:

处理 onClick 事件的最简洁方法是(在我看来)添加 onClick-attribute 到 XML-Layout 定义。

<Button android:text="Click Me!"
  android:onClick="doSomething"
/>

在您的 java 代码中:

public void doSomething(View v){
  // Do stuff here
}

这样,您可以为每个 onClick 事件定义一个方法。从 API 级别 4 开始可用,对应 Android 1.6

As an extension to "JB Nizet" answer:

The neatest way to handle onClick-events is (in my opinion) to add the onClick-attribute to the XML-Layout definition.

<Button android:text="Click Me!"
  android:onClick="doSomething"
/>

In your java-code:

public void doSomething(View v){
  // Do stuff here
}

This way, you can define a single method for every single onClick-event. This is available since API-Level 4 which corresponds to Android 1.6

酒解孤独 2024-12-13 17:14:13

看起来您正在 eclipse 中的项目中使用低于 java 6 的编译器。右键单击您的项目,转到属性--> Java 编译器。确保您在编译器合规性字段中选择了 1.6。

Looks like you are using compiler below java 6 for your project in eclipse. Right click on your project, go to properties-->Java compiler. Make sure you have 1.6 selected in compiler compliance field.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文