Android开发:按钮onClickListeners帮助

时间:2010-08-21 19:44:40

标签: php android onclick

基本上,到目前为止,我只有一个添加和减号按钮,它将改变TextView的值。

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

上“事件监听器”下的第二个示例进行操作

问题是我编译时遇到4个错误,我看不出我做错了什么?

请有人指出是否有任何错误,或者请告诉我一个更简单的方法。

感谢您的时间。

这是错误:

/home/alex/NetBeansProjects/GolfScoreCard/src/com/alex/golfscorecard/MainActivity.java:19: com.alex.golfscorecard.MainActivity is not abstract and does not override abstract method onClick(android.content.DialogInterface,int) in android.content.DialogInterface.OnClickListener
public class MainActivity extends Activity implements OnClickListener{
/home/alex/NetBeansProjects/GolfScoreCard/src/com/alex/golfscorecard/MainActivity.java:49: setOnClickListener(android.view.View.OnClickListener) in android.view.View cannot be applied to (com.alex.golfscorecard.MainActivity)
        buttonMinus.setOnClickListener(this);
/home/alex/NetBeansProjects/GolfScoreCard/src/com/alex/golfscorecard/MainActivity.java:50: setOnClickListener(android.view.View.OnClickListener) in android.view.View cannot be applied to (com.alex.golfscorecard.MainActivity)
        buttonPlus.setOnClickListener(this);
/home/alex/NetBeansProjects/GolfScoreCard/src/com/alex/golfscorecard/MainActivity.java:51: setOnClickListener(android.view.View.OnClickListener) in android.view.View cannot be applied to (com.alex.golfscorecard.MainActivity)
        buttonOK.setOnClickListener(this);

这是我的代码:

package com.alex.golfscorecard;

import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 *
 * @author alex
 */
public class MainActivity extends Activity implements OnClickListener{
  Button buttonMinus, buttonPlus, buttonOK;
  TextView golfScore;

    // Implement the OnClickListener callback
    // Incomplete!
    public void onClick(View v) {
      switch(v.getId()){
          case R.id.buttonMinus:
          score--;
          break;
          case R.id.buttonPlus:
          score++;
          break;
      }
    }

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

        //Set the UI elements and find their view in main.xml
        buttonMinus = (Button) findViewById(R.id.buttonMinus);
        buttonPlus = (Button) findViewById(R.id.buttonPlus);
        buttonOK = (Button) findViewById(R.id.buttonOK);
        golfScore = (TextView) findViewById(R.id.golfScore);

        //Set the UI elements' on click listeners
        buttonMinus.setOnClickListener(this);
        buttonPlus.setOnClickListener(this);
        buttonOK.setOnClickListener(this);

    }

}

2 个答案:

答案 0 :(得分:2)

您导入了错误的OnClickListener。改变

import android.content.DialogInterface.OnClickListener;

import android.view.View.OnClickListener;

答案 1 :(得分:1)

我还建议不要像你那样实现onClickListener。这是一个非常糟糕的设计。相反,为每个按钮设置一个匿名的onclicklistener。



buttonMinus.setOnClickListener(new onClickListner(){

@Override
public void onClick(){

//do stuff

});