Android Development: Button onClickListeners Help(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 帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android 开发:按钮 onClickListeners 帮助
基础教程推荐
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01