简体   繁体   English

找不到OnClickListener

[英]OnClickListener can't be found

I'm learning android and I'm currently doing a calculator. 我正在学习android,目前正在做计算器。 I've already done the xml part, where I put the buttons And I'm trying to finish the Java file, which is the code that makes the calculator work 我已经完成了xml部分,在其中放置了按钮,然后尝试完成Java文件,这是使计算器工作的代码

BUT in the Java file I've got this error: Error:(22, 64) error: cannot find symbol class OnClickListener 但是在Java文件中,我遇到此错误:错误:(22,64)错误:找不到符号类OnClickListener

and I don't know what to do :c I'm still learning I've seen that I should put 而且我不知道该怎么办:c我仍在学习,我知道我应该把

    public class MainActivity extends ActionBarActivity implements View.OnClickListener {

But I don't understand what's that and I don't know if that will bug the calculator. 但是我不知道那是什么,也不知道这是否会影响计算器。

That's the Java file (MainActivity) 那就是Java文件(MainActivity)

package com.example.glow.pruebas;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button n0 = (Button) findViewById(R.id.B0);
        n0.setOnClickListener(this);
        Button n1 = (Button) findViewById(R.id.B1);
        n1.setOnClickListener(this);
        Button n2 = (Button) findViewById(R.id.B2);
        n2.setOnClickListener(this);
        Button n3 = (Button) findViewById(R.id.B3);
        n3.setOnClickListener(this);
        Button n4 = (Button) findViewById(R.id.B4);
        n4.setOnClickListener(this);
        Button n5 = (Button) findViewById(R.id.B5);
        n5.setOnClickListener(this);
        Button n6 = (Button) findViewById(R.id.B6);
        n6.setOnClickListener(this);
        Button n7 = (Button) findViewById(R.id.B7);
        n7.setOnClickListener(this);
        Button n8 = (Button) findViewById(R.id.B8);
        n8.setOnClickListener(this);
        Button n9 = (Button) findViewById(R.id.B9);
        n9.setOnClickListener(this);

        Button coma = (Button) findViewById(R.id.Bcoma);
        coma.setOnClickListener(this);
        Button igual = (Button) findViewById(R.id.Bigual);
        igual.setOnClickListener(this);

        Button suma = (Button) findViewById(R.id.B6sumar);
        suma.setOnClickListener(this);
        Button resta = (Button) findViewById(R.id.B5restar);
        resta.setOnClickListener(this);
        Button mul = (Button) findViewById(R.id.Bmult);
        mul.setOnClickListener(this);
        Button division = (Button) findViewById(R.id.Bdividir);
        division.setOnClickListener(this);
        Button raiz = (Button) findViewById(R.id.raiz);
        raiz.setOnClickListener(this);
        Button elevado = (Button) findViewById(R.id.BElevado);
        elevado.setOnClickListener(this);

        Button DEL = (Button) findViewById(R.id.BDEL);
        DEL.setOnClickListener(this);
        Button AC = (Button) findViewById(R.id.BAC);
        AC.setOnClickListener(this);

        Button sin = (Button) findViewById(R.id.Bsin);
        sin.setOnClickListener(this);
        Button cos = (Button) findViewById(R.id.Bcos);
        cos.setOnClickListener(this);
        Button tan = (Button) findViewById(R.id.Btan);
        tan.setOnClickListener(this);

        Button secreto = (Button) findViewById(R.id.Bsecreto);
        secreto.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        TextView pantalla = (TextView) findViewById(R.id.texto);
        int seleccion = v.getId();

        try {
            switch (seleccion) {
                case R.id.B0:
                    pantalla.setText("0");
                    break;
                case R.id.B1:
                    pantalla.setText("1");
                    break;
                case R.id.B2:
                    pantalla.setText("2");
                    break;
                case R.id.B3:
                    pantalla.setText("3");
                    break;
                case R.id.B4:
                    pantalla.setText("4");
                    break;
                case R.id.B5:
                    pantalla.setText("5");
                    break;
                case R.id.B6:
                    pantalla.setText("6");
                    break;
                case R.id.B7:
                    pantalla.setText("7");
                    break;
                case R.id.B8:
                    pantalla.setText("8");
                    break;
                case R.id.B9:
                    pantalla.setText("9");
                    break;

                case R.id.Bcoma:
                    pantalla.setText(",");
                    break;

                case R.id.Bmult:
                    break;
                case R.id.B5restar:
                    break;
                case R.id.B6sumar:
                    break;
                case R.id.Bdividir:
                    break;
                case R.id.BAC:
                    break;

                case R.id.Bsin:
                    break;
                case R.id.Bcos:
                    break;
                case R.id.Btan:
                    break;
            }

        }catch(Exception e){
            pantalla.setText("error");
        };

    }
}

A silly mistake. 一个愚蠢的错误。

Change your implement from OnClickListener to View.OnClickListener . 将您的工具从OnClickListener更改为View.OnClickListener

Since you said you don't know what it does: 由于您说过,您不知道它的作用:

http://developer.android.com/reference/android/view/View.OnClickListener.html http://developer.android.com/reference/android/view/View.OnClickListener.html

You are basically trying to implement an Interface , so that you can access its method onClick(View v) where you are passing the View in the argument. 您基本上是在尝试实现一个Interface ,以便您可以在其中将View传递给参数的位置访问其onClick(View v)方法。

As you said you have to add 如您所说,您必须添加

public class MainActivity extends ActionBarActivity implements View.OnClickListener {

So your problem is here: public class MainActivity extends ActionBarActivity implements OnClickListener . 因此,您的问题在这里: public class MainActivity extends ActionBarActivity implements OnClickListener

Change that line to public class MainActivity extends ActionBarActivity implements View.OnClickListener 将该行更改为public class MainActivity extends ActionBarActivity implements View.OnClickListener

You are missing the View .OnClickListener and using just implements OnClickListener Changing that will solve your issue ;) 您缺少View .OnClickListener,仅使用实现OnClickListener的更改即可解决您的问题;)

your mistake is that you implemented DialogInterface.onClickListener , it not work as a simple click listener, but in your case you have to implement View.onClickListener 您的错误是您实现了DialogInterface.onClickListener ,它不能用作简单的单击侦听器,但是在您的情况下,您必须实现View.onClickListener

your DialogInterface.onClickListener only work on dialog, and its not a VIEW and you are working on view object thats why. 您的DialogInterface.onClickListener仅适用于对话框,而不适用于VIEW,而您正在使用视图对象,这就是原因。

实现View.onClickListener而不是OnClickListener

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM