Android Simple Calculator

Source: Internet
Author: User

This example is a calculator implementation example based on the Android GOOGL SDK book:

1 Create three TextView objects, two EditText objects, four button objects

Main.xml for

<?xml version= "1.0" encoding= "Utf-8"?>

<absolutelayout

Android:id= "@+id/widget0"

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent"

Xmlns:android= "Http://schemas.android.com/apk/res/android"

>

<edittext android:id= "@+id/edittext1"

android:layout_height= "Wrap_content"

Android:layout_width= "47PX"

Android:textsize= "18SP"

android:layout_x= "26PX"

Android:layout_y= "52px"

/>

<edittext android:id= "@+id/edittext2"

android:layout_height= "Wrap_content"

Android:layout_width= "45px"

Android:textsize= "18SP"

android:layout_x= "128px"

android:layout_y= "51px"/>

<textview

Android:id= "@+id/mytextview1"

Android:layout_width= "30px"

android:layout_height= "33PX"

Android:textsize= "18SP"

android:layout_x= "92px"

Android:layout_y= "60px"

/>

<textview

Android:id= "@+id/mytextview3"

Android:layout_width= "18px"

android:layout_height= "Wrap_content"

android:text= "="

Android:textsize= "18SP"

android:layout_x= "189PX"

Android:layout_y= "63PX"

/>

<textview

Android:id= "@+id/mytextview2"

Android:layout_width= "30px"

android:layout_height= "Wrap_content"

Android:textsize= "18SP"

android:layout_x= "219PX"

Android:layout_y= "61px"

/>

<button

Android:id= "@+id/button1"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

Android:textsize= "25SP"

android:layout_x= "12px"

Android:layout_y= "161px"

android:text= "@string/but_str1"/>

<button

Android:id= "@+id/button2"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

Android:textsize= "25SP"

android:layout_x= "85px"

Android:layout_y= "159PX"

android:text= "@string/but_str2"/>

<button

Android:id= "@+id/button3"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

Android:textsize= "25SP"

android:layout_x= "158px"

Android:layout_y= "161px"

android:text= "@string/but_str3"/>

<button

Android:id= "@+id/button4"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

Android:textsize= "25SP"

android:layout_x= "239PX"

Android:layout_y= "161px"

android:text= "@string/but_str4"/>

</AbsoluteLayout>

There's a little bit of a problem here.

The contents of the button display are obtained through string.xml. You can also directly modify the android:text= "+" in Main.xml to specify "+" as specified above.

Activity Object

Package com.app.android;


Import Java.text.DecimalFormat;

Import Java.text.NumberFormat;


Import android.app.Activity;

Import Android.app.AlertDialog;

Import Android.app.AlertDialog.Builder;

Import Android.content.DialogInterface;

Import Android.content.DialogInterface.OnClickListener;

Import Android.os.Bundle;

Import Android.view.View;

Import Android.widget.Button;

Import Android.widget.EditText;

Import Android.widget.TextView;


public class Myfirstactivity extends Activity {

/** called when the activity is first created. */

Private Button button1;

Private TextView TextView1;

Private TextView textView2;

Private Button button2;

Private Button Button3;

Private Button Button4;

Private EditText EditText1;

Private EditText editText2;

@Override

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);

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

Button2= (Button) Findviewbyid (R.id.button2);

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

button4= (Button) Findviewbyid (R.ID.BUTTON4);

edittext1= (EditText) Findviewbyid (R.ID.EDITTEXT1);

Edittext2= (EditText) Findviewbyid (R.ID.EDITTEXT2);

textview1= (TextView) Findviewbyid (R.ID.MYTEXTVIEW1);

Textview2= (TextView) Findviewbyid (R.ID.MYTEXTVIEW2);

Button1.setonclicklistener (New Button.onclicklistener ()

{

public void OnClick (View v)

{

TODO auto-generated Method Stub

if (Confirm (edittext1,edittext2)) {

Textview1.settext ("+");

String Strret=format (Float.parsefloat (Edittext1.gettext (). toString ()) +float.parsefloat (Edittext2.gettext (). ToString ()));

Textview2.settext (strret);

}

}

});

Button2.setonclicklistener (New Button.onclicklistener ()

{

public void OnClick (View v)

{

TODO auto-generated Method Stub

if (Confirm (edittext1,edittext2)) {

Textview1.settext ("-");

String Strret=format (Float.parsefloat (Edittext1.gettext (). toString ())-float.parsefloat (Edittext2.gettext (). ToString ()));

Textview2.settext (strret);

}

}

});

Button3.setonclicklistener (New Button.onclicklistener ()

{

public void OnClick (View v)

{

TODO auto-generated Method Stub

if (Confirm (edittext1,edittext2)) {

Textview1.settext ("*");

String Strret=format (Float.parsefloat (Edittext1.gettext (). toString ()) *float.parsefloat (Edittext2.gettext (). ToString ()));

Textview2.settext (strret);

}}

});

Button4.setonclicklistener (New Button.onclicklistener ()

{

public void OnClick (View v)

{

TODO auto-generated Method Stub

if (Confirm (edittext1,edittext2)) {;

Textview1.settext ("/");

String Strret=format (Float.parsefloat (Edittext1.gettext (). toString ())/float.parsefloat (Edittext2.gettext (). ToString ()));

Textview2.settext (strret);

}}

});

}

/**

* Formatting results

* @param num

* @return

*/

Private String format (float num) {

NumberFormat formater=new DecimalFormat ("0.00");

String S=formater.format (num);

return s;

}

/**

* Verify that the operation is complete

* @param Text1

* @param text2

* @return

*/

Private Boolean confirm (EditText Text1,edittext text2) {

String Edittext1=text1.gettext (). toString ();

String Edittext2=text2.gettext (). toString ();

System.out.println (edittext1+ "," +edittext2);

if (Edittext1!=null&&edittext1.equals ("")) | | (Edittext2!=null&&edittext2.equals (""))) {

New Alertdialog.builder (Myfirstactivity.this). Settitle (R.string.title). Setmessage (r.string.msg). Setpositivebutton (

R.string.ok,new Dialoginterface.onclicklistener () {

public void OnClick (Dialoginterface dialoginterface,int i) {

Dialoginterface.dismiss ();

}

}

). Show ();

return false;

}

Else{return true;}

}

}

Methods are already commented.


For more examples, refer to: http://itlanbao.com/preview.aspx#1,0


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