Android 开发:按钮 onClickListeners 帮助 [英] Android Development: Button onClickListeners Help

查看:19
本文介绍了Android 开发:按钮 onClickListeners 帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;

这篇关于Android 开发:按钮 onClickListeners 帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆