java - 接口(interface) OnClickListener 无法使用我的 onClick 方法?

标签 java android eclipse android-layout

这是我的大问题:

http://www.bilder-space.de/show_img.php?img=7371f3-1333218887.jpg&size=original

和代码:

package org.madmax.sudoku;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

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

        //Set up Click Listener for all Buttons
        View continueButton = findViewById(R.id.continue_button);
        continueButton.setOnClickListener(this);
        View newButton = findViewById(R.id.new_button);
        newButton.setOnClickListener(this);

        public void onClick(View v) {
            switch(v.getId()) {
            case R.id.about_button:
                Intent i = new Intent(this, About.class);
                startActivity(i);
                break;
            }
        }

    }
}

错误是:

-void is an invalid type for the variable onCLick()

为什么 Eclipse 会给我这个错误?

最佳答案

您可以像这样在 onCreate 方法中使用 onclick

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    Button newButton = findViewById(R.id.new_button);
    newButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // your code here

        }
    });
}

关于java - 接口(interface) OnClickListener 无法使用我的 onClick 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9958604/

相关文章:

java - StringUtils 类型的方法 isNullOrEmpty(String) 未定义

android - 如何获取所有wifi接入点的BSSID?

android - 如何使用 Xamarin android 打开另一个带有 onclick 监听器的已安装应用程序

java - 当我启动类(class)时,值没有被传递

java - 哪个 Eclipse API 用于执行 "Override/implement Methods"菜单?

java - Retrofit/OkHttp 删除 Transfer-Encoding header

java - Android 发布到 Twitter

java - BufferedImage 用透明像素填充矩形

java - 任务 ':app:dexDebug' android错误执行失败

java - eclipse 中行号左侧的那些黄色水平箭头有什么作用?