Android simple calculator

Source: Internet
Author: User

Note: When the input is empty, a dialog box prompt is displayed, indicating that no number is entered. When the divisor is 0, a prompt is displayed, indicating that the divisor cannot be blank. after entering a number, click the plus, subtraction, multiplication, division, and operation button to perform the operation. After you click the menu, the "about" and "exit" button appear at the bottom of the screen. Click "About". The "about" dialog box appears. Click "exit" to exit the program.

1. layout, activity_main.xml code:

<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: gravity = "center_vertical" Android: orientation = "vertical"> <relativelayout Android: Id = "@ + ID/relative1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> <! -- The first number involved in the calculation --> <edittext Android: Id = "@ + ID/num1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_alignparentleft = "true" Android: layout_margintop = "10dp" Android: EMS = "4" Android: inputtype = "Number"> <requestfocus/> </edittext> <! -- Operator number --> <textview Android: Id = "@ + ID/fuhao" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_margintop = "15dp" Android: layout_torightof = "@ ID/num1" Android: padding = "@ dimen/padding_medium" Android: text = "@ string/fuhao"/> <! -- The second number involved in the calculation --> <edittext Android: Id = "@ + ID/num2" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_margintop = "10dp" Android: layout_torightof = "@ ID/fuhao" Android: EMS = "4" Android: inputtype = "Number"/> <! -- Equal sign --> <textview Android: Id = "@ + ID/denghao" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_margintop = "15dp" Android: layout_torightof = "@ ID/num2" Android: padding = "@ dimen/padding_medium" Android: text = "@ string/denghao"/> <! -- Result --> <textview Android: Id = "@ + ID/result" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_margintop = "15dp" Android: layout_torightof = "@ ID/denghao" Android: padding = "@ dimen/padding_medium" Android: text = "@ string/result"/> </relativelayout> <relativelayout Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> <! -- Plus sign --> <button Android: Id = "@ + ID/Add" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_alignparentleft = "true" Android: layout_centerhorizontal = "true" Android: EMS = "4" Android: text = "@ string/Add"/> <! -- Minus sign --> <button Android: Id = "@ + ID/Sub" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_centerhorizontal = "true" Android: layout_torightof = "@ ID/Add" Android: EMS = "4" Android: text = "@ string/Sub"/> <! -- Multiplication --> <button Android: Id = "@ + ID/mult" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_centerhorizontal = "true" Android: layout_torightof = "@ ID/Sub" Android: EMS = "4" Android: text = "@ string/mult"/> <! -- Divisor --> <button Android: Id = "@ + ID/divid" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_torightof = "@ ID/mult" Android: EMS = "4" Android: text = "@ string/divid"/> </relativelayout> <relativelayout Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> <! -- About buttons --> <button Android: Id = "@ + ID/about" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "@ string/about"/> <! -- Exit button --> <button Android: Id = "@ + ID/quit" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "@ string/quit"/> </relativelayout> </linearlayout>

2. mainactivity. Java code:

Package CN. BZU. case1; import Java. text. decimalformat; import CN. BZU. model. calculatorbiz; import android. OS. bundle; import android. app. activity; import android. app. alertdialog; import android. view. menu; import android. view. menuitem; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. edittext; import android. widget. textview; import android. widge T. toast; import android. widget. remoteviews. actionexception; import android. support. v4.app. navutils; public class mainactivity extends activity {private double number1, number2; private edittext num1, num2; private textview result, fuhao; private button add, sub, mult, divid, quit, about; calculatorbiz calculator; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate ); Setcontentview (R. layout. activity_main); num1 = (edittext) This. findviewbyid (R. id. num1); num2 = (edittext) This. findviewbyid (R. id. num2); add = (button) This. findviewbyid (R. id. add); sub = (button) This. findviewbyid (R. id. sub); mult = (button) This. findviewbyid (R. id. mult); divid = (button) This. findviewbyid (R. id. divid); fuhao = (textview) This. findviewbyid (R. id. fuhao); Result = (textview) This. findviewbyid (R. Id. result); quit = (button) This. findviewbyid (R. id. quit); about = (button) This. findviewbyid (R. id. about); quit. setvisibility (view. invisible); about. setvisibility (view. invisible); add. setonclicklistener (New calculatorclicklistener (); sub. setonclicklistener (New calculatorclicklistener (); mult. setonclicklistener (New calculatorclicklistener (); divid. setonclicklistener (New calculatorclicklistener ();} privat E Class calculatorclicklistener implements onclicklistener {@ overridepublic void onclick (view v) {// If edittext is empty if (num1.gettext (). tostring (). specified signorecase ("") {toast. maketext (mainactivity. this, "No number entered! ", Toast. length_short ). show ();} else if (num2.gettext (). tostring (). specified signorecase ("") {toast. maketext (mainactivity. this, "No number entered! ", Toast. length_short ). show ();} else {number1 = double. parsedouble (num1.gettext (). tostring (); number2 = double. parsedouble (num2.gettext (). tostring (); If (v. equals (ADD) {result. settext (number1 + number2 + ""); fuhao. settext ("+");} If (v. equals (sub) {result. settext (number1-number2 + ""); fuhao. settext ("-");} If (v. equals (mult) {result. settext (number1 * number2 + ""); fuhao. settext ("*");} If (v. equals (Divid) {If (number2 = 0) {toast. maketext (mainactivity. This, "the divisor cannot be zero! ", Toast. length_short ). show ();} else {result. settext (number1/number2 + ""); fuhao. settext ("/") ;}}}}@ overridepublic Boolean oncreateoptionsmenu (menu) {super. oncreateoptionsmenu (menu); // create a menu, set the icon, and use the icon provided by the system, menu. add (0, R. id. about, 0, "about "). seticon (Android. r. drawable. ic_dialog_info); menu. add (0, R. id. quit, 1, "quit "). seticon (Android. r. drawable. ic_lock_power_off); menu. finditem (R. id. quit); // return true, that is Show menu return true;} @ override // response menu Click Event, public Boolean onoptionsitemselected (menuitem item) {Switch (item. getitemid () {case R. id. about: // about alertdialog. builder dialog = new alertdialog. builder (this); dialog. settitle ("about"); dialog. setmessage ("Easy calculator! \ N Author: vivy \ n version: 1.0 "); dialog. show (); break; case R. id. quit: // exit this. finish (); break;} return Super. onoptionsitemselected (item );}}

3 .:

Related Article

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.