Design a simple calculator.
The first Activity interface.
The second Activity shows the calculation and the results of calculations.
First Activity code:
Import Android.app.activity;import android.content.intent;import Android.os.bundle;import Android.view.Menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;public class MainActivity Extends Activity {Button One,two,three,four,five,six,seven,eight,nine,zero,div,mod,mul,fenshu,sub,equal,point,add ;d ouble num1,num2,num,xiaoshudian;int flag,ispoint;char operation[]=new char[2];p rotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); num1=0;num2=0; Num=0;flag=0;ispoint=0;xiaoshudian=0.1;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);d iv= (button ) Findviewbyid (r.id. Div); mod= (Button) Findviewbyid (r.id.mod); mul= (Button) Findviewbyid (R.id.mul); fenshu= (button) Findviewbyid ( R.id.fenshu), sub= (Button) Findviewbyid (r.id.sub), add= (Button) Findviewbyid (R.id.add), equal= (button) Findviewbyid ( r.id.equal);p oint= (Button) Findviewbyid (r.id.point); One.setonclicklistener (new Onclicklistener () {public void OnClick (View arg0) {if (flag==0) {if (ispoint==0) num1=num1*10+1;else {num1=num1+1*xiaoshudian;xiaoshudian/=10;}} Else{if (ispoint==0) num2=num2*10+1;else{num2=num2+1*xiaoshudian;xiaoshudian/=10;}}}); Two.setonclicklistener (New Onclicklistener () {public void OnClick (View arg0) {if (flag==0) {if (ispoint==0) num1=num1*10 +2;else {num1=num1+2*xiaoshudian;xiaoshudian/=10;}} Else{if (ispoint==0) num2=num2*10+2;else{num2=num2+2*xiaoshudian;xiaoshudian/=10;}}}); Three.setonclicklistener (New Onclicklistener () {public void OnClick (View arg0) {if (flag==0) {if (ispoint==0) num1=num1* 10+3;else {num1=num1+3*xiaoshudian;xiaoshudian/=10;}} Else{if (ispoint==0) Num2=num2*10+3;else{num2=num2+3*xiaoshudian; xiaoshudian/=10;}}}); Four.setonclicklistener (New Onclicklistener () {public void OnClick (View arg0) {if (flag==0) {if (ispoint==0) num1=num1* 10+4;else {num1=num1+4*xiaoshudian;xiaoshudian/=10;}} Else{if (ispoint==0) num2=num2*10+4;else{num2=num2+4*xiaoshudian;xiaoshudian/=10;}}}); Five.setonclicklistener (New Onclicklistener () {public void OnClick (View arg0) {if (flag==0) {if (ispoint==0) num1=num1* 10+5;else {num1=num1+5*xiaoshudian;xiaoshudian/=10;}} Else{if (ispoint==0) num2=num2*10+5;else{num2=num2+5*xiaoshudian;xiaoshudian/=10;}}}); Six.setonclicklistener (New Onclicklistener () {public void OnClick (View arg0) {if (flag==0) {if (ispoint==0) num1=num1*10 +6;else {num1=num1+6*xiaoshudian;xiaoshudian/=10;}} Else{if (ispoint==0) num2=num2*10+6;else{num2=num2+6*xiaoshudian;xiaoshudian/=10;}}}); Seven.setonclicklistener (New Onclicklistener () {public void OnClick (View arg0) {if (flag==0) {if (ispoint==0) num1=num1* 10+7;else {num1=num1+7*xiaoshudian;xiaoshudian/=10;}} Else{if (ispoint==0) num2=num2*10+7;else{num2=num2+7*xiaoshudian;xiaoshudian/=10;}}}); Eight.setonclicklistener (New Onclicklistener () {public void OnClick (View arg0) {if (flag==0) {if (ispoint==0) num1=num1* 10+8;else {num1=num1+8*xiaoshudian;xiaoshudian/=10;}} Else{if (ispoint==0) num2=num2*10+8;else{num2=num2+8*xiaoshudian;xiaoshudian/=10;}}}); Nine.setonclicklistener (New Onclicklistener () {public void OnClick (View arg0) {if (flag==0) {if (ispoint==0) num1=num1* 10+9;else {num1=num1+9*xiaoshudian;xiaoshudian/=10;}} Else{if (ispoint==0) num2=num2*10+9;else{num2=num2+9*xiaoshudian;xiaoshudian/=10;}}}); Zero.setonclicklistener (New Onclicklistener () {public void OnClick (View arg0) {if (flag==0) {if (ispoint==0) num1=num1* 10;else {xiaoshudian/=10;}} Else{if (ispoint==0) num2=num2*10;else{xiaoshudian/=10;}}}); Point.setonclicklistener (New Onclicklistener () {public void OnClick (View arg0) {ispoint=1;}}); Add.setonclicklistener (New Onclicklistener () {public void OnClick (View arg0) {flag=1;ispoint=0;}}); Sub.setonclicklistener (New Onclicklistener () {public void OnclicK (View arg0) {flag=2;ispoint=0;}}); Mul.setonclicklistener (New Onclicklistener () {public void OnClick (View arg0) {flag=3;ispoint=0;}}); Div.setonclicklistener (New Onclicklistener () {public void OnClick (View arg0) {flag=4;ispoint=0;}}); Equal.setonclicklistener (New Onclicklistener () {public void OnClick (View arg0) {if (flag==1) {num=num1+num2;operation[ 0]= ' + ';} else if (flag==2) {num=num1-num2;operation[0]= '-';} else if (flag==3) {num=num1*num2;operation[0]= ' * ';} else if (flag==4) {num=num1/num2;operation[0]= '/';} Intent intent=new Intent (mainactivity.this,secondactivity.class); Intent.putextra ("Num1", New Double (NUM1). toString ()); Intent.putextra ("Opreation", new String (operation). ToString ()); Intent.putextra ("num2", New Double (num2). ToString ()); Intent.putextra ("Num", new Double (num). ToString ()); startactivity (intent);});} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
A second activity code:
Import Android.app.activity;import android.content.intent;import Android.os.bundle;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;public Class Secondactivity extends Activity{edittext text; Button mybutton;protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.second); text= (EditText) Findviewbyid (R.id.edt); mybutton= (Button) Findviewbyid (R.ID.BTN) ; Intent intent=getintent (); String Num1=intent.getstringextra ("Num1"); String Operation=intent.getstringextra ("opreation"); String Num2=intent.getstringextra ("num2"); String Num=intent.getstringextra ("num"); Text.settext (num1+operation+num2+ "=" +num); Mybutton.setonclicklistener ( New Mybuttonlistener ());} Class Mybuttonlistener implements Onclicklistener{public void OnClick (View arg0) {Intent intent=new Intent ( Secondactivity.this,mainactivity.class); startactivity (intent);}}
First Activity layout:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "><button android:id=" @+id/seven "android:layout_width=" 40px "android:layout_height=" Wrap_ Content "android:text=" 7 "/><button android:id=" @+id/eight "android:layout_torightof=" @id/seven " Android:layout_width= "40px" android:layout_height= "Wrap_content" android:text= "8"/><button Android Oid:id= "@+id/nine" android:layout_torightof= "@id/eight" android:layout_width= "40px" android:layout_height= "WR Ap_content "android:text=" 9 "/><buttOn android:id= "@+id/div" android:layout_torightof= "@id/nine" android:layout_width= "40px" android:layout_he ight= "Wrap_content" android:text= "/"/><button android:id= "@+id/mod" android:layout_torightof= "@id/D IV "Android:layout_width=" 40px "android:layout_height=" wrap_content "android:text="% "/><button Android:id= "@+id/four" android:layout_below= "@id/seven" android:layout_width= "40px" android:layout_height= "Wrap_content" android:text= "4"/><button android:id= "@+id/five" android:layout_torightof= "@id/four" android:layout_below= "@id/eight" android:layout_width= "40px" android:layout_height= "Wrap_content" and roid:text= "5"/><button android:id= "@+id/six" android:layout_torightof= "@id/five" android:layout_below= " @id/nine "android:layout_width=" 40px "android:layout_height=" Wrap_content "android:text=" 6 "/><b Utton android:id= "@+iD/mul "android:layout_torightof=" @id/six "android:layout_below=" @id/div "android:layout_width=" 40px "Andro id:layout_height= "Wrap_content" android:text= "*"/><button android:id= "@+id/fenshu" android:layout_t orightof= "@id/mul" android:layout_below= "@id/mod" android:layout_width= "40px" android:layout_height= "Wrap_con Tent "android:text=" 1/x "/><button android:id=" @+id/one "android:layout_below=" @id/four "Android: Layout_width= "40px" android:layout_height= "Wrap_content" android:text= "1"/><button android:id= " @+id/two "android:layout_torightof=" @id/one "android:layout_below=" @id/five "android:layout_width=" 40px "a ndroid:layout_height= "Wrap_content" android:text= "2"/><button android:id= "@+id/three" android:layou t_torightof= "@id/two" android:layout_below= "@id/six" android:layout_width= "40px" android:layout_height= "Wrap_ Content "Android:text= "3"/><button android:id= "@+id/sub" android:layout_torightof= "@id/three" android:layout_below= "@id/ Mul "android:layout_width=" 40px "android:layout_height=" wrap_content "android:text="-"/><button Android:id= "@+id/equal" android:layout_torightof= "@id/sub" android:layout_below= "@id/fenshu" Android:layout_w Idth= "40px" android:layout_height= "70px" android:text= "="/><button android:id= "@+id/zero" an droid:layout_below= "@id/one" android:layout_width= "80px" android:layout_height= "Wrap_content" Android:tex t= "0"/><button android:id= "@+id/point" android:layout_torightof= "@id/zero" android:layout_below= "@id/th Ree "android:layout_width=" 40px "android:layout_height=" Wrap_content "android:text=". " /><button android:id= "@+id/add" android:layout_torightof= "@id/point" android:layout_below= "@id/sub" Android Oid:layout_width= "40px" Android:layout_height= "Wrap_content" android:text= "+"/></relativelayout>
A second activity layout:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <edittext android:id= "@+id/edt" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_below= "@id/one" /> <button Android:id= "@+id/btn" android:layout_width= "wrap_content" android:layout_height= "Wrap_content " android:layout_below= "@id/edt" android:text= "back" /> </LinearLayout>