2016-05-01 91 views
0

我正在學習android,而且我正在做一個計算器。 我已經做了XML的一部分,在那裏我把按鈕 我試圖完成的Java文件,它是構成計算器工作無法找到OnClickListener

但Java文件中我得到這個代碼錯誤: 錯誤:(22,64)錯誤:無法找到符號類OnClickListener

,我不知道該怎麼做:C我還在學習 我已經看到了,我應該把

public class MainActivity extends ActionBarActivity implements View.OnClickListener { 

但我不明白那是什麼,我不知道這是否會影響計算器。

,當你說你要添加

public class MainActivity extends ActionBarActivity implements View.OnClickListener {

的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"); 
     }; 

    } 
} 
+2

你缺少了'進口android.view.View.OnClickListener'? –

+0

爲什麼你在OnClick中初始化textView?將此移至onCreate並將pantalla作爲全局變量 –

+0

用View.OnClickListener替換您的實現OnClickListener –

回答

0

所以你的問題是在這裏: public class MainActivity extends ActionBarActivity implements OnClickListener

把上面一行 public class MainActivity extends ActionBarActivity implements View.OnClickListener

你缺少查看 .OnClickListener和使用只是實現OnClickListener 轉變,將解決你的問題;)

0

你的錯誤是,你實現DialogInterface.onClickListener,它不是一個簡單的點擊監聽工作,但在你的情況,你必須實現View.onClickListener

您DialogInterface.onClickListener只在對話工作,它不是一個查看 和你正在研究視圖對象這就是爲什麼。

0

實施View.onClickListener而不是OnClickListener