The realization source of Calculator

Source: Internet
Author: User

Package com.example.cal;


Import Android. R.integer;

Import Android.os.Bundle;

Import android.app.Activity;

Import Android.view.Menu;

Import Android.view.View;

Import Android.widget.Button;

Import Android.widget.EditText;

Import Android.widget.ImageButton;


public class Mainactivity extends Activity {

Private ImageButton One, three, Four,five,six,seven,eight,nine,zero,add,

Dividion,equal,minus,product;

EditText et;

String Num,backnum,allnum;

int I,backi,alli;

int logo;

@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_main);

et= (EditText) Findviewbyid (r.id.et_num);

zero= (ImageButton) Findviewbyid (R.ID.IB_0);

One= (ImageButton) Findviewbyid (r.id.ib_1);

two= (ImageButton) Findviewbyid (r.id.ib_2);

Three= (ImageButton) Findviewbyid (r.id.ib_3);

Four= (ImageButton) Findviewbyid (r.id.ib_4);

Five= (ImageButton) Findviewbyid (r.id.ib_5);

six= (ImageButton) Findviewbyid (r.id.ib_6);

Seven= (ImageButton) Findviewbyid (r.id.ib_7);

eight= (ImageButton) Findviewbyid (r.id.ib_8);

Nine= (ImageButton) Findviewbyid (r.id.ib_9);

Add= (ImageButton) Findviewbyid (R.id.loga_add);

dividion= (ImageButton) Findviewbyid (r.id.dividion);

Minus= (ImageButton) Findviewbyid (R.id.minus);

product= (ImageButton) Findviewbyid (R.id.pro);

Equal= (ImageButton) Findviewbyid (r.id.equal);

}

public void init () {

et= (EditText) Findviewbyid (r.id.et_num);

zero= (Button) Findviewbyid (R.ID.IB_0);

one= (Button) Findviewbyid (r.id.ib_1);

two= (Button) Findviewbyid (r.id.ib_2);

three= (Button) Findviewbyid (r.id.ib_3);

four= (Button) Findviewbyid (r.id.ib_4);

five= (Button) Findviewbyid (r.id.ib_5);

six= (Button) Findviewbyid (r.id.ib_6);

seven= (Button) Findviewbyid (r.id.ib_7);

eight= (Button) Findviewbyid (r.id.ib_8);

nine= (Button) Findviewbyid (r.id.ib_9);

add= (Button) Findviewbyid (R.id.loga_add);

dividion= (Button) Findviewbyid (r.id.dividion);

minus= (Button) Findviewbyid (R.id.minus);

product= (Button) Findviewbyid (R.id.pro);

equal= (Button) Findviewbyid (r.id.equal);

//}

public void One (View v) {

Et.append ("1");

}

public void (View v) {

Et.append ("2");

}

public void Three (View v) {

Et.append ("3");

}

public void Four (View v) {

Et.append ("4");

}

public void Five (View v) {

Et.append ("5");

}

public void Six (View v) {

Et.append ("6");

}

public void Seven (View v) {

Et.append ("7");

}

public void Eight (View v) {

Et.append ("8");

}

public void Nine (View v) {

Et.append ("9");

}

public void Zero (View v) {

Et.append ("0");

}

When you click the plus sign

public void Add (View v) {

Num=et.gettext (). toString ();

if (Num.equals ("")) {

Et.settext ("");

}else{

i = Integer.parseint (num);

Logo=1;


Et.settext ("");}

}

When you click the minus sign

public void minus (View v) {

Num=et.gettext (). toString ();

if (Num.equals ("")) {

Et.settext ("");

}else{

I=integer.parseint (num);

logo=2;

Et.settext ("");}

}

When you click Multiplication sign

public void product (View v) {

Num=et.gettext (). toString ();

if (Num.equals ("")) {

Et.settext ("");

}else{

I=integer.parseint (num);

logo=3;

Et.settext ("");}

}

When you click Division Sign

public void Dividion (View v) {

Num=et.gettext (). toString ();

if (Num.equals ("")) {

Et.settext ("");

}else{

I=integer.parseint (num);

logo=4;

Et.settext ("");}

}

public void Back (View v) {

Num=et.gettext (). toString ();

if (Num.equals ("")) {

Et.settext ("");

}else{

String mnum=num.substring (0, Num.length ()-1);

Et.settext (mnum);}

}

When you click the equals sign

public void equal (View v) {

Backnum=et.gettext (). toString ();

if (Backnum.equals ("")) {

Et.settext ("");

}else{

Backi=integer.parseint (Backnum);

if (logo==1) {

Alli=i+backi;

Backnum=integer.tostring (Alli);

Et.settext (Backnum);

}else if (logo==2) {

Alli=i-backi;

Backnum=integer.tostring (Alli);

Et.settext (Backnum);

}else if (logo==3) {

Alli=i*backi;

Backnum=integer.tostring (Alli);

Et.settext (Backnum);

}else{

Alli=i/backi;

Backnum=double.tostring (Alli);

Et.settext (Backnum);

}}

}

@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;

}


}


The realization source of Calculator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.