Android Development Implementation Calculator example

Source: Internet
Author: User

Example

The code is as follows Copy Code

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/warpcontent"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >

Android:id= "@+id/scrollview"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" >

&lt;textview


Android:id= "@+id/resulttext"


Android:layout_width= "Wrap_content"


android:layout_height= "Wrap_content"


Android:layout_gravity= "Right"


Android:gravity= "Right"


Android:padding= "8DP"


android:paddingleft= "10DP"


android:paddingright= "10DP"


android:text= "@string/number0"


Android:textsize= "40sp"/&gt;


&lt;/horizontalscrollview &gt;

<tablelayout
Android:id= "@+id/buttontable"
Android:layout_width= "Fill_parent"
android:layout_height= "0DP"
android:layout_weight= "1"
android:stretchcolumns= "*" >

        <tablerow
             android:layout_width= "Fill_parent"
             android:layout_height= "0DP"
            android:layout_weight= "1"

            <button
                 android:id= "@+id/button7"
                 android:layout_width= "Wrap_ Content "
                android: layout_height= "Fill_parent"
                 android:onclick= "Onclicklistener"
                 android:text= "@string/number7"/>

            <button
                 android:id= "@+id/button8"
                 android:layout_width= "Wrap_ Content "
                android: layout_height= "Fill_parent"
                 android:onclick= "Onclicklistener"
                 android:text= "@string/number8"/>

            <button
                 android:id= "@+id/button9"
                 android:layout_width= "Wrap_ Content "
                android: layout_height= "Fill_parent"
                 android:onclick= "Onclicklistener"
                 android:text= "@string/number9"/>

            <button
                 android:id= "@+id/buttonbackspace"
                 android:layout_width= "Wrap _content "
                Android : layout_height= "fill_parent"
                 android:onclick= "Onclicklistener"
                 android:text= "@string/backspace"/>

            <button
                 android:id= "@+id/buttonclear"
                 android:layout_width= "Wrap _content "
                Android : layout_height= "fill_parent"
                 android:onclick= "Onclicklistener"
                 android:text= "@string/clear"/>
        </tablerow>

        <tablerow
             android:layout_width= "Fill_parent"
             android:layout_height= "0DP"
            android:layout_weight= "1"

            <button
                 android:id= "@+id/button4"
                 android:layout_width= "Wrap_ Content "
                android: layout_height= "Fill_parent"
                 android:onclick= "Onclicklistener"
                 android:text= "@string/number4"/>

            <button
                 android:id= "@+id/button5"
                 android:layout_width= "Wrap_ Content "
                android: layout_height= "Fill_parent"
                 android:onclick= "Onclicklistener"
                 android:text= "@string/number5"/>

            <button
                 android:id= "@+id/button6"
                 android:layout_width= "Wrap_ Content "
                android: layout_height= "Fill_parent"
                 android:onclick= "Onclicklistener"
                 android:text= "@string/number6"/>

            <button
                 android:id= "@+id/buttonmultiply"
                 android:layout_width= "Wrap _content "
                Android : layout_height= "fill_parent"
                 android:onclick= "Onclicklistener"
                 android:text= "@string/multiply"/>

            <button
                 android:id= "@+id/buttondivide"
                 android:layout_width= "Wrap _content "
                Android : layout_height= "fill_parent"
                 android:onclick= "Onclicklistener"
                 android:text= "@string/divide"/>
        </tablerow>
    </tablelayout>

<linearlayout
Android:layout_width= "Fill_parent"
android:layout_height= "0DP"
android:layout_weight= "1"
android:orientation= "Horizontal" >

<tablelayout
Android:layout_width= "0DP"
android:layout_height= "Fill_parent"
android:layout_weight= "3"
android:stretchcolumns= "*" >

            <tablerow
                 android:layout_width= "Fill_parent"
                 android:layout_height= "0DP "
                Android:layout_ weight= "1"

                <button
                     android:id= "@+id/button1"
                     android:layout_width= "Wrap_content"
                     Android:layout_ height= "Fill_parent"
                     android:onclick= "Onclicklistener"
                     android:text= "@string/number1"/>

                <button
                     android:id= "@+id/button2"
                     android:layout_width= "Wrap_content"
                     Android:layout_ height= "Fill_parent"
                     android:onclick= "Onclicklistener"
                     android:text= "@string/number2"/>

                <button
                     android:id= "@+id/button3"
                     android:layout_width= "Wrap_content"
                     Android:layout_ height= "Fill_parent"
                     android:onclick= "Onclicklistener"
                     android:text= "@string/number3"/>
            </tablerow>

            <tablerow
                 android:layout_width= "Fill_parent"
                 android:layout_height= "0DP "
                Android:layout_ weight= "1"

                <button
                     android:id= "@+id/button0"
                     android:layout_width= "Wrap_content"
                     Android:layout_ height= "Fill_parent"
                     android:onclick= "Onclicklistener"
                     android:text= "@string/number0"/>

<button
Android:id= "@+id/buttondot"
Android:layout_width= "Wrap_content"
android:layout_height= "Fill_parent"
android:onclick= "Onclicklistener"
android:text= "@string/dot"/>

&lt;button


Android:id= "@+id/buttonnegsign"


Android:layout_width= "Wrap_content"


android:layout_height= "Fill_parent"


android:onclick= "Onclicklistener"


android:text= "@string/negsign"/&gt;


&lt;/TableRow&gt;


&lt;/TableLayout&gt;

<button
Android:id= "@+id/buttonplus"
Android:layout_width= "0DP"
android:layout_height= "Fill_parent"
android:layout_weight= "1"
android:onclick= "Onclicklistener"
android:text= "@string/plus"/>

        <linearlayout
             android:layout_width= "0DP"
             android:layout_height= "Fill_parent"
             android:layout_weight= "1"
            android:orientation= "Vertical"

            <button
                 android:id= "@+id/buttonminus"
                 android:layout_width= "Fill _parent "
                android: layout_height= "0DP"
                 android:layout_weight= "1"
                 android:onclick= "Onclicklistener"
                 android:text= "@string/minus"/>

<button
Android:id= "@+id/buttoneval"
Android:layout_width= "Fill_parent"
android:layout_height= "0DP"
android:layout_weight= "1"
android:onclick= "Onclicklistener"
android:text= "@string/eval"/>
</LinearLayout>
</LinearLayout>

</LinearLayout>

The Mainactivity class is similar to the previous Java calculator:

The code is as follows Copy Code

Package Me.jerrys.calc;

Import Java.math.BigDecimal;

Import android.support.v7.app.ActionBarActivity;
Import Android.app.AlertDialog;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.widget.TextView;
Import Android.widget.Toast;

public class Mainactivity extends Actionbaractivity {
Private TextView TextView;
Private BigDecimal Lastnum = new BigDecimal ("0");
Private BigDecimal Currentnum = new BigDecimal ("0");
Private String Currenttext;
Private Boolean Typeclean = false;
Private Boolean hadcalced = false;
Private mode CurrentMode = mode. PLUS;
Private enum MODE {
PLUS, minus, MULTIPLY, DIVIDE
}

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
TextView = (TextView) Findviewbyid (R.id.resulttext);
}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}

public void Onclicklistener (View v) {


Currenttext = Textview.gettext (). toString ();


Switch (V.getid ()) {


Case R.ID.BUTTON0:


Addnumber ("0");


Break


Case R.id.button1:


Addnumber ("1");


Break


Case R.id.button2:


Addnumber ("2");


Break


Case R.ID.BUTTON3:


Addnumber ("3");


Break


Case R.ID.BUTTON4:


Addnumber ("4");


Break


Case R.ID.BUTTON5:


Addnumber ("5");


Break


Case R.ID.BUTTON6:


Addnumber ("6");


Break


Case R.ID.BUTTON7:


Addnumber ("7");


Break


Case R.ID.BUTTON8:


Addnumber ("8");


Break


Case R.ID.BUTTON9:


Addnumber ("9");


Break


Case R.id.buttonnegsign:


Addsign ();


Break


Case R.id.buttonclear:


Clear ();


Break


Case R.id.buttonbackspace:


BackSpace ();


Break


Case R.id.buttonplus:


SetMode (MODE. PLUS);


Break


Case R.id.buttonminus:


SetMode (MODE. Minus);


Break


Case R.id.buttonmultiply:


SetMode (MODE. MULTIPLY);


Break


Case R.id.buttondivide:


SetMode (MODE. DIVIDE);


Break


Case R.id.buttoneval:


Calc ();


Break


Case R.ID.BUTTONDOT:


Addnumber (".");


Break


}


}


private void Calc () {


if (hadcalced &amp;&amp; Typeclean) return;


Currentnum = new BigDecimal (currenttext);


try {


Switch (currentmode) {


Case PLUS:


Lastnum = Lastnum.add (currentnum);


Break


Case minus:


Lastnum = Lastnum.subtract (currentnum);


Break


Case MULTIPLY:


Lastnum = lastnum.multiply (currentnum);


Break


Case DIVIDE:


Lastnum = Lastnum.divide (Currentnum, 16,


Bigdecimal.round_half_down);


Break


}


catch (Exception e) {


Toast.maketext (Getapplicationcontext (), "Error occurred:" +e.getmessage (), Toast.length_short). Show ();


Return


}


String Resulttext = lastnum.tostring ();


if (Resulttext.indexof (".") &gt; 0) {//elimination of decimal extra digits


Resulttext = Resulttext.replaceall ("0+?$", "");


Resulttext = Resulttext.replaceall ("[.] $", "");


}


Setresulttext (Resulttext);


Typeclean = true;


hadcalced = false;


}


private void SetMode (Mode mode) {


if (!hadcalced &amp;&amp;!typeclean) {


Calc ();


CurrentMode = mode;


Return


}


Lastnum = new BigDecimal (currenttext);


CurrentMode = mode;


Typeclean = true;


hadcalced = true;


}





private void Addsign () {


if (Currenttext.equals ("0") | | currenttext.equals ("")) return;


if (Currenttext.startswith ("-")) Setresulttext (currenttext.substring (1));


Else Setresulttext ("-" + currenttext);


}





private void Clear () {


Currentnum = new BigDecimal ("0");


Lastnum = new BigDecimal ("0");


Typeclean = false;


CurrentMode = MODE. PLUS;


Setresulttext ("0");


}





private void Setresulttext (string string) {


Textview.settext (string);


}

public void Addnumber (String num) {


String resulttext = Currenttext;


if (Typeclean = = True) {


Resulttext = "0";


Typeclean = false;


}


if (Num.equals (".") &amp;&amp; Currenttext.lastindexof (".")!=-1) return;


if ((Resulttext.equals ("0") | | resulttext.equals (""))


&amp;&amp;!num.equals ("."))


Resulttext = "";


Resulttext = Resulttext + num;


Setresulttext (Resulttext);


}





private void BackSpace () {


if (Currenttext.equals ("0") | | currenttext.equals ("")) return;


if (currenttext.length () = = 1) {


Setresulttext ("0");


Return


}


Setresulttext (currenttext.substring (0, Currenttext.length ()-1));


}





@Override


public boolean onoptionsitemselected (MenuItem item) {


if (item.getitemid () = = R.id.action_about) {


New Alertdialog.builder (this). Settitle ("about"). Setmessage ("" Android www.111cn.net Simple Calculator "n Author jerrylocken blog n Source code/ Android-simple-calc "). Setpositivebutton (" OK ", null). Show ();


return true;


}


return super.onoptionsitemselected (item);


}


}


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.