Android 开发:按钮 onClickListeners 帮助

2022-01-09 00:00:00 onclick php android

Basically, so far I've just got an add and minus button which will change the value of a TextView.

I followed the second example under "Event Listeners" on http://developer.android.com/guide/topics/ui/ui-events.html

The problem is I get 4 errors when I go to compile, I don't see what I'm doing wrong?

Please can someone point out if there is anything wrong, or show me a simpler way please.

Thank you for your time.

Here's the errors:

/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);

Here is my code:

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);

    }

}

解决方案

You have imported wrong OnClickListener. Change

import android.content.DialogInterface.OnClickListener;

to

import android.view.View.OnClickListener;

相关文章