Results
The XML file is as follows:
<framelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:id= "@+id/container"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
tools:context= "Com.example.wxhcalculator.MainActivity"
Tools:ignore= "Mergerootframe" >
<tablelayout
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:stretchcolumns= "1"
Android:textsize= "42SP" >
<TableRow>
<edittext
Android:id= "@+id/result"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:layout_span= "4"
android:background= "@android:d rawable/editbox_background"
Android:cursorvisible= "false"
Android:editable= "false"
Android:gravity= "Right|center_vertical"
android:lines= "1"
Android:textsize= "60sp"/>
</TableRow>
<TableRow>
<linearlayout
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:orientation= "Horizontal"
Android:textsize= "42SP" >
<button
Android:id= "@+id/num7"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "7"
Android:textsize= "42SP"/>
<button
Android:id= "@+id/num8"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "8"
Android:textsize= "42SP"/>
<button
Android:id= "@+id/num9"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "9"
Android:textsize= "42SP"/>
<button
Android:id= "@+id/divide"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "/"
Android:textsize= "42SP"/>
</LinearLayout>
</TableRow>
<TableRow>
<linearlayout
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:orientation= "Horizontal"
Android:textsize= "42SP" >
<button
Android:id= "@+id/num4"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "4"
Android:textsize= "42SP"/>
<button
Android:id= "@+id/num5"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "5"
Android:textsize= "42SP"/>
<button
Android:id= "@+id/num6"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "6"
Android:textsize= "42SP"/>
<button
Android:id= "@+id/multiply"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "*"
Android:textsize= "42SP"/>
</LinearLayout>
</TableRow>
<TableRow>
<linearlayout
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:orientation= "Horizontal"
Android:textsize= "42SP" >
<button
Android:id= "@+id/num1"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "1"
Android:textsize= "42SP"/>
<button
Android:id= "@+id/num2"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "2"
Android:textsize= "42SP"/>
<button
Android:id= "@+id/num3"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "3"
Android:textsize= "42SP"/>
<button
Android:id= "@+id/subtract"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "-"
Android:textsize= "42SP"/>
</LinearLayout>
</TableRow>
<TableRow>
<linearlayout
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:orientation= "Horizontal"
Android:textsize= "42SP" >
<button
Android:id= "@+id/num0"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "0"
Android:textsize= "42SP"/>
<button
Android:id= "@+id/point"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
Android:text= "."
Android:textsize= "42SP"/>
<button
Android:id= "@+id/add"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "+"
Android:textsize= "42SP"/>
<button
Android:id= "@+id/equal"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "="
Android:textsize= "42SP"/>
</LinearLayout>
</TableRow>
<TableRow>
<button
Android:id= "@+id/clear"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:layout_span= "4"
Android:gravity= "Center_vertical|center_horizontal"
android:text= "Clear"
Android:textsize= "30sp"/>
</TableRow>
</TableLayout>
</FrameLayout>
-----------------------------------------------------------------------
The mainactivity main function is as follows:
Package com.example.wxhcalculator;
Import android.support.v7.app.ActionBarActivity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
public class Mainactivity extends Actionbaractivity {
Private button[] Btnnum = new button[11];//value button
Private button[] Btncommand = new button[5];//symbol button
Private EditText EditText = null;//display area
Private Button btnclear = null; Clear button
Private String Lastcommand; For saving operators
Private Boolean Clearflag; Used to determine if the value of the display area is emptied, true requires, false does not need
Private Boolean Firstflag; Used to determine if the first input is true, false is not the first
private double result; Calculation results
Public mainactivity () {
Initialize the value of each item
result = 0; The value of X
Firstflag = true; is the first operation
Clearflag = false; No need to empty
Lastcommand = "="; Operator
}
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Get operator
Btncommand[0] = (Button) Findviewbyid (R.id.add);
BTNCOMMAND[1] = (Button) Findviewbyid (r.id.subtract);
BTNCOMMAND[2] = (Button) Findviewbyid (r.id.multiply);
BTNCOMMAND[3] = (Button) Findviewbyid (r.id.divide);
BTNCOMMAND[4] = (Button) Findviewbyid (r.id.equal);
Get numbers
Btnnum[0] = (Button) Findviewbyid (R.ID.NUM0);
BTNNUM[1] = (Button) Findviewbyid (R.ID.NUM1);
BTNNUM[2] = (Button) Findviewbyid (r.id.num2);
BTNNUM[3] = (Button) Findviewbyid (R.ID.NUM3);
BTNNUM[4] = (Button) Findviewbyid (R.ID.NUM4);
BTNNUM[5] = (Button) Findviewbyid (R.ID.NUM5);
BTNNUM[6] = (Button) Findviewbyid (R.ID.NUM6);
BTNNUM[7] = (Button) Findviewbyid (R.ID.NUM7);
BTNNUM[8] = (Button) Findviewbyid (R.ID.NUM8);
BTNNUM[9] = (Button) Findviewbyid (R.ID.NUM9);
BTNNUM[10] = (Button) Findviewbyid (r.id.point);
Initialize display result area
EditText = (editText) Findviewbyid (R.id.result);
Edittext.settext ("0.0");
Instantiating listener objects
Numberaction na = new Numberaction ();
Commandaction CA = new Commandaction ();
for (Button Bc:btncommand) {
Bc.setonclicklistener (CA);
}
for (Button bc:btnnum) {
Bc.setonclicklistener (NA);
}
The action of the Clear button
Btnclear = (Button) Findviewbyid (r.id.clear);
Btnclear.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (view view) {
Edittext.settext ("0.0");
Initialize the value of each item
result = 0; The value of X
Firstflag = true; is the first operation
Clearflag = false; No need to empty
Lastcommand = "="; Operator
}
});
}
Digital Button Listener
Private class Numberaction implements Onclicklistener {
@Override
public void OnClick (view view) {
Button btn = (button) view;
String input = Btn.gettext (). toString ();
if (Firstflag) {//First input
One on the "." And you don't do anything.
if (Input.equals (".")) {
Return
}
If it's "0.0", empty it.
if (Edittext.gettext (). toString (). Equals ("0.0")) {
Edittext.settext ("");
}
Firstflag = false;//Change whether the tag value was first entered
} else {
String edittextstr = Edittext.gettext (). toString ();
Determine if the value of the display area already has ".", and if so, the input is ".", do nothing.
if (Edittextstr.indexof (".")! =-1 && input.equals (".")) {
Return
}
Judging the value of the display area only "-", the input is ".", do nothing
if (Edittextstr.equals ("-") && input.equals (".")) {
Return
}
Determine the value of the display area if it is "0", the input is not ".", do nothing
if (Edittextstr.equals ("0") &&!input.equals (".")) {
Return
}
}
If I click on the operator and then enter a number, the value of the display area will be emptied.
if (Clearflag) {
Edittext.settext ("");
Clearflag = false;//Restore initial value, do not need to empty
}
Edittext.settext (Edittext.gettext (). toString () + input);//Set the value of the display area
}
}
Symbol Button Listener
Private class Commandaction implements Onclicklistener {
@Override
public void OnClick (view view) {
Button btn = (button) view;
String Inputcommand = (string) btn.gettext ();
if (Firstflag) {//First input "-" case
if (Inputcommand.equals ("-")) {
Edittext.settext ("-");//The content of the display area is set to "-"
Firstflag = false;//Change the first input marker
}
} else {
if (!clearflag) {//If Flag=false does not need to empty the value of the display area, the method is called to calculate
Calculate (Double.parsedouble (Edittext.gettext (). toString ()));//Save the value of the display area and calculate
}
Save the operator you clicked on
Lastcommand = Inputcommand;
Clearflag = true;//Because I've already entered the operator here,
}
}
}
Method of calculation
private void Calculate (Double x) {
if (lastcommand.equals ("+")) {
result + = x;
} else if (Lastcommand.equals ("-")) {
Result-= x;
} else if (Lastcommand.equals ("*")) {
Result *= x;
} else if (Lastcommand.equals ("/")) {
Result/= x;
} else if (lastcommand.equals ("=")) {
result = x;
}
Edittext.settext ("" + result);
}
}
A simple calculator for Android table layout development