Android---9---simple calculator

Source: Internet
Author: User

Do a simple calculator, can only achieve a simple arithmetic.

Activity_main.xml:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "Android:orienta tion= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_         Vertical_margin "tools:context=" com.example.calculator.MainActivity "> <textview android:id=" @+id/show " Android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:gravity= "Center_vert ical "android:hint=" 0 "android:minlines=" 3 "/> <button android:id=" @+id/clean "Androi D:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_below= "@id/show" and roid:text= "Clear"/> <button android:id= "@+Id/seven "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_bel ow= "@+id/clean" android:text= "7"/> <button android:id= "@+id/eight" android:layout_width= "WR Ap_content "android:layout_height=" wrap_content "android:layout_alignbottom=" @+id/seven "Android:layo ut_torightof= "@+id/seven" android:text= "8"/> <button android:id= "@+id/nine" Android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignbaseline= "@+id/eight" a Ndroid:layout_alignbottom= "@+id/eight" android:layout_torightof= "@+id/eight" android:text= "9"/> <B        Utton android:id= "@+id/add" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_alignbaseline= "@+id/nine" android:layout_alignbottom= "@+id/nine" Android:layout_torigh Tof= "@+id/nine" android:text= "+"/> <button android:id= "@+id/four" android:layout_width= "Wrap_content" Androi        d:layout_height= "Wrap_content" android:layout_alignright= "@+id/seven" android:layout_below= "@+id/seven" android:text= "4"/> <button android:id= "@+id/five" android:layout_width= "Wrap_content" an droid:layout_height= "Wrap_content" android:layout_alignbaseline= "@+id/four" android:layout_alignbottom= "@+id /four "android:layout_alignleft=" @+id/eight "android:text=" 5 "/> <button android:id=" @+id/si X "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_alignbasel Ine= "@+id/five" android:layout_alignbottom= "@+id/five" android:layout_torightof= "@+id/five" android:t ext= "6"/> <button android:id= "@+id/subtract" android:layout_width= "Wrap_content" Android:la Yout_height= "Wrap_contenT "android:layout_alignbaseline=" @+id/six "android:layout_alignbottom=" @+id/six "Android:layout_torig htof= "@+id/six" android:text= "-"/> <button android:id= "@+id/one" android:layout_width= "wrap _content "android:layout_height=" wrap_content "android:layout_alignleft=" @+id/four "Android:layout_be low= "@+id/four" android:text= "1"/> <button android:id= "@+id/two" android:layout_width= "wrap _content "android:layout_height=" wrap_content "android:layout_alignleft=" @+id/five "Android:layout_be low= "@+id/five" android:text= "2"/> <button android:id= "@+id/three" android:layout_width= "WR Ap_content "android:layout_height=" wrap_content "android:layout_alignbaseline=" @+id/two "Android:layo Ut_alignbottom= "@+id/two" android:layout_alignleft= "@+id/six" android:text= "3"/> <button an Droid:id= "@+id/multiply"        Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_alignbaselin E= "@+id/three" android:layout_alignbottom= "@+id/three" android:layout_torightof= "@+id/three" Android: text= "*"/> <button android:id= "@+id/zero" android:layout_width= "Wrap_content" android:layou t_height= "Wrap_content" android:layout_alignleft= "@+id/one" android:layout_alignright= "@+id/one" Android oid:layout_below= "@+id/one" android:text= "0"/> <button android:id= "@+id/point" Android:layo        Ut_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignbaseline= "@+id/zero" Android:layout_alignbottom= "@+id/zero" android:layout_toleftof= "@+id/multiply" android:text= "."/> &L T Button android:id= "@+id/divide" android:layout_width= "wrap_content" android:layout_height= "Wrap_conte NT "Android:layout_alignleft= "@+id/multiply" android:layout_below= "@+id/multiply" android:text= "/"/> <Button        Android:id= "@+id/symbol" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"        android:layout_below= "@+id/one" android:layout_torightof= "@+id/one" android:text= "-/+"/> <Button        Android:id= "@+id/result" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layout_alignleft= "@+id/zero" android:layout_alignright= "@+id/divide" android:layout_below= "@+id/z Ero "android:text=" = "/></relativelayout>


Mainactivity.java:


Package Com.example.calculator;import Android. R.integer;import android.app.activity;import android.os.bundle;import Android.util.log;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;public class MainActivity Extends Activity implements Onclicklistener {Private Button One, I, three, four, five, six, seven, eight, nine, Zero;pri Vate button Add, subtract, multiply, divide;private button Symbol, point;private button clean, result;private TextView Sho Wresult;double num1, num2, num;int op = 0;protected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main);//0---9one = (Button) Findviewbyid (R.id.one); 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);//+-*/add = (Button) Findviewbyid (R.id.add); Subtract = (button) Findviewbyid (r.id.subtract), multiply = (button) Findviewbyid (r.id.multiply);d ivide = (Button) Findviewbyid (r.id.divide);p oint = (button) Findviewbyid (r.id.point); clean = (Button) Findviewbyid (r.id.clean); result = (Button) Findviewbyid (R.id.result); Showresult = (TextView) Findviewbyid (r.id.show); Clean.setonclicklistener (this), Zero.setonclicklistener (this); O Ne.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); Add.setonclicklistener (This), Subtract.setonclicklistener (This), Multiply.setonclicklistener (this); Divide.setonclicklistEner (This);p Oint.setonclicklistener (This), Result.setonclicklistener (this);} @Overridepublic void OnClick (View v) {String content = ""; Switch (V.getid ()) {//0---9case r.id.one:content = showresult.g Ettext (). toString () + "1"; Showresult.settext (content); Break;case r.id.two:content = Showresult.gettext (). toString () + "2"; Showresult.settext (content); Break;case r.id.three:content = Showresult.gettext (). toString () + "3"; Showresult.settext (content); Break;case r.id.four:content = Showresult.gettext (). toString () + "4"; Showresult.settext (content); Break;case r.id.five:content = Showresult.gettext (). toString () + "5"; Showresult.settext (content); Break;case r.id.six:content = Showresult.gettext (). toString () + "6"; Showresult.settext (content); Break;case r.id.seven:content = Showresult.gettext (). toString () + "7"; Showresult.settext (content); Break;case r.id.eight:content = Showresult.gettext (). toString () + "8"; Showresult.settext (content); Break;case r.id.nine:content = Showresult.gettext (). toString () + "9";Showresult.settext (content); Break;case r.id.zero:content = Showresult.gettext (). toString () + "0"; Showresult.settext (content); Break;case r.id.point:content = Showresult.gettext (). toString () + "."; Showresult.settext (content), break;//+-*/case r.id.add:string addstring = Showresult.gettext (). toString (); if ( Addstring.equals ("")) {return;} NUM1 = double.valueof (addstring); op = 1; Showresult.settext (""); break;case r.id.subtract:string subString = Showresult.gettext (). toString (); if ( Substring.equals ("")) {return;} NUM1 = double.valueof (subString); op = 2; Showresult.settext (""); break;case r.id.multiply:string mulstring = Showresult.gettext (). toString (); if ( Mulstring.equals ("")) {return;} NUM1 = double.valueof (mulstring); op = 3; Showresult.settext (""); break;case r.id.divide:string divstring = Showresult.gettext (). toString (); if ( Divstring.equals ("")) {return;} NUM1 = double.valueof (divstring); op = 4; Showresult.settext (""); break;//cleancase R.id.clean:showresult.settext (""); break;//=case R.id.result:STring resultstring = Showresult.gettext (). toString (); if (Resultstring.equals ("")) {return;} num2 = double.valueof (resultstring); switch (OP) {case 0:num = num2;break;case 1:num = num1 + num2;break;case 2:num = Num1 -Num2;break;case 3:num = num1 * Num2;break;case 4:num = num1/num2;break;default:break;} Showresult.settext (num + ""); break;default:break;}}}


Android---9---simple 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.