You are on page 1of 7

package com.

appcalculadora;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.*;

import android.view.View.OnClickListener;

import android.widget.*;

public class MainActivity extends Activity implements OnClickListener{

private char op;//+, -, *, /

private double memoria;

private Button one;

private Button two;

private Button three;

private Button four;

private Button five;

private Button six;

private Button seven;

private Button eight;

private Button nine;

private Button zero;

private Button borrar;


private Button delete;

private Button sumar;

private TextView txtpantalla;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

one= (Button)findViewById(R.id.One);

two= (Button)findViewById(R.id.Two);

three= (Button)findViewById(R.id.Three);

four= (Button)findViewById(R.id.Four);

five= (Button)findViewById(R.id.Five);

six= (Button)findViewById(R.id.Six);

seven= (Button)findViewById(R.id.Seven);

eight= (Button)findViewById(R.id.Eight);

nine= (Button)findViewById(R.id.Nine);

zero= (Button)findViewById(R.id.Zero);

borrar=(Button) findViewById(R.id.btnborra);

delete=(Button) findViewById(R.id.btnDel);

sumar=(Button) findViewById(R.id.Suma);
txtpantalla=(TextView) findViewById(R.id.TxtPantalla);

one.setOnClickListener(this);

two.setOnClickListener(this);

three.setOnClickListener(this);

four.setOnClickListener(this);

five.setOnClickListener(this);

six.setOnClickListener(this);

seven.setOnClickListener(this);

eight.setOnClickListener(this);

nine.setOnClickListener(this);

zero.setOnClickListener(this);

borrar.setOnClickListener(this);

delete.setOnClickListener(this);

sumar.setOnClickListener(this);

public void PresionarNumero(String number){

String strNumber=txtpantalla.getText().toString();

if(bandOp==true){

strNumber=number;

bandOp=false;

}
else{

if(strNumber=="0")

strNumber=number;

else

strNumber=strNumber+number;

txtpantalla.setText(strNumber);

public void borrarNumeroAnterior(){

txtpantalla.setText("0");

public void borrarUnNumero(){

String strNumber=txtpantalla.getText().toString();

if(strNumber!="0"){

if(strNumber.length()>1)

strNumber=strNumber.substring(0,strNumber.length()-1);

else

strNumber="0";

txtpantalla.setText(strNumber);
}

private void realizaroperacion(char op){

if(this.op==' '){

this.op=op;

this.memoria=Double.parseDouble(this.txtpantalla.getText().toString());

else{

Double pantalla=Double.parseDouble(this.txtpantalla.getText().toString());

switch(this.op){

case '+':

break;

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}
@Override

public void onClick(View v){

switch(v.getId()){

case R.id.One:

case R.id.Two:

case R.id.Three:

case R.id.Four:

case R.id.Five:

case R.id.Six:

case R.id.Seven:

case R.id.Eight:

case R.id.Nine:

case R.id.Zero:

Button btn= (Button) findViewById(v.getId());

String number= btn.getText().toString();

PresionarNumero(number);

break;

case R.id.btnborra:

borrarNumeroAnterior();

break;

R.id.btnDel:

borrarUnNumero();

break;

case R.id.Suma:

btn=(Button) findViewById(v.getId());

String operacion=btn.getText().toString();
realizarOperacion('+');

break;

case R.id.btnIgual:

break;