package com.ct.mysimplecalculator; import android.app.Activity; import android.os.Bundle; import android.text.InputType; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private Button[] btn = new Button[20]; private Button calculator_return_btn; private EditText edittext; private String number = ""; private double temp1, temp2;// 计算的第一个数和第二个数 private int index;// + - * /的索引 private double result = 0;// 运算的结果 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); getInstance(); } private void getInstance(){ btn[0] = (Button) findViewById(R.id.zero); btn[1] = (Button) findViewById(R.id.one); btn[2] = (Button) findViewById(R.id.two); btn[3] = (Button) findViewById(R.id.three); btn[4] = (Button) findViewById(R.id.four); btn[5] = (Button) findViewById(R.id.five); btn[6] = (Button) findViewById(R.id.six); btn[7] = (Button) findViewById(R.id.seven); btn[8] = (Button) findViewById(R.id.eight); btn[9] = (Button) findViewById(R.id.nine); btn[10] = (Button) findViewById(R.id.clean); btn[11] = (Button) findViewById(R.id.dot); btn[12] = (Button) findViewById(R.id.equal); btn[13] = (Button) findViewById(R.id.add); btn[14] = (Button) findViewById(R.id.subtraction); btn[15] = (Button) findViewById(R.id.multiply); btn[16] = (Button) findViewById(R.id.divide); btn[17] = (Button) findViewById(R.id.income); btn[18] = (Button) findViewById(R.id.expense); btn[19] = (Button) findViewById(R.id.transfer); calculator_return_btn=(Button) findViewById(R.id.calculator_return_btn); calculator_return_btn.setOnClickListener(this); edittext = (EditText) findViewById(R.id.screen); edittext.setInputType(InputType.TYPE_NULL); edittext.setText(number); for (int i = 0; i < 20; i++) { btn[i].setOnClickListener(this); } } private boolean idDouble(String str){ try { Double.parseDouble(str); return true; } catch(NumberFormatException ex){} return false; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.clean: if ("".equals(number)) { edittext.setText(number); edittext.setSelection(number.length()); } else { number = number.substring(0, number.length() - 1); edittext.setText(number); edittext.setSelection(number.length()); if (("".equals(number)) || ("-".equals(number))) { edittext.setText(number); } else { temp2 = Double.parseDouble(number); } } break; case R.id.add: if (!("".equals(number))) { edittext.setText(number); index = 0; temp1 = Double.parseDouble(number); number = ""; temp2 = 0; } break; case R.id.subtraction: if (!("".equals(number))) { edittext.setText(number); index = 1; temp1 = Double.parseDouble(number); number = ""; temp2 = 0; } break; case R.id.multiply: if (!("".equals(number))) { edittext.setText(number); index = 2; temp1 = Double.parseDouble(number); number = ""; temp2 = 0; break; } case R.id.divide: if (!("".equals(number))) { edittext.setText(number); index = 3; temp1 = Double.parseDouble(number); number = ""; temp2 = 0; } break; case R.id.dot: if(!(number.contains("."))){ number += "0."; edittext.setText(number); break; } case R.id.income: String income = edittext.getText().toString(); if(idDouble(income)){ Bundle bundle=new Bundle(); bundle.putInt("category", 1);//1表示收入,2表示支出,3表示转账 bundle.putDouble("amount", Double.parseDouble(income)); finish(); }else{ Toast.makeText(this, "不能记入,格式不正确", Toast.LENGTH_SHORT).show(); } break; case R.id.expense: String expense = edittext.getText().toString(); if(idDouble(expense)){ Bundle bundle=new Bundle(); bundle.putInt("category", 2); bundle.putDouble("amount", Double.parseDouble(expense)); finish(); }else{ Toast.makeText(this, "不能记入,格式不正确", Toast.LENGTH_SHORT).show(); } break; case R.id.transfer: String transfer = edittext.getText().toString(); if(idDouble(transfer)){ Bundle bundle=new Bundle(); bundle.putInt("category", 3); bundle.putDouble("amount", Double.parseDouble(transfer)); finish(); }else{ Toast.makeText(this, "不能记入,格式不正确", Toast.LENGTH_SHORT).show(); } break; case R.id.equal: switch (index) { case 0: result = temp1 + temp2; edittext.setText(String.valueOf(result)); number = String.valueOf(result); result = 0; break; case 1: result = temp1 - temp2; edittext.setText(String.valueOf(result)); number = String.valueOf(result); result = 0; break; case 2: result = temp1 * temp2; edittext.setText(String.valueOf(result)); number = String.valueOf(result); result = 0; break; case 3: if (temp2 == 0) { edittext.setText("∞"); number = ""; result = 0; } else { result = temp1 / temp2; edittext.setText(String.valueOf(result)); number = String.valueOf(result); result = 0; } break; default: break; } break; case R.id.calculator_return_btn: MainActivity.this.finish(); break; default: Button btn = (Button) v; String temp = btn.getText().toString(); number += temp; edittext.setText(number); temp2 = Double.parseDouble(number); } } }
<?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:layout_gravity="center" android:background="@drawable/wallpaper" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="44dp" > <Button android:id="@+id/calculator_return_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:text="返回" android:textSize="14sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="计算器" android:textSize="24sp" /> </RelativeLayout> <EditText android:id="@+id/screen" android:layout_width="272dp" android:layout_height="64dp" android:layout_gravity="center" android:background="@drawable/calculator_screen_bg" android:cursorVisible="false" android:editable="false" android:inputType="numberDecimal" android:gravity="right|center_vertical" android:hint="0.00" android:textColor="@android:color/white" android:textColorHint="@android:color/white" android:textSize="45sp" android:layout_marginTop="12dp"/> <TableLayout android:id="@+id/tableLayout" android:layout_width="match_parent" android:layout_height="wrap_content" > <TableRow android:id="@+id/row_1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="32dp" android:layout_weight="1" android:gravity="center" > <Button android:id="@+id/income" android:layout_width="55dp" android:layout_height="48dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:background="@drawable/calculator_icon_bg" android:text="收入" android:textColor="@android:color/white" android:textSize="18sp" /> <Button android:id="@+id/expense" android:layout_width="55dp" android:layout_height="48dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:background="@drawable/calculator_icon_bg" android:text="支出" android:textColor="@android:color/white" android:textSize="18sp" /> <Button android:id="@+id/transfer" android:layout_width="55dp" android:layout_height="48dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:background="@drawable/calculator_icon_bg" android:text="转账" android:textColor="@android:color/white" android:textSize="18sp" /> <Button android:id="@+id/clean" android:layout_width="55dp" android:layout_height="48dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:background="@drawable/calculator_icon_bg" android:text="C" android:textColor="@android:color/white" android:textSize="30sp" /> </TableRow> <TableRow android:id="@+id/row0" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="16dp" android:layout_weight="1" android:gravity="center" > <Button android:id="@+id/one" android:layout_width="55dp" android:layout_height="48dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:background="@drawable/calculator_icon_bg" android:text="1" android:textColor="@android:color/white" android:textSize="30sp" /> <Button android:id="@+id/two" android:layout_width="55dp" android:layout_height="48dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:background="@drawable/calculator_icon_bg" android:text="2" android:textColor="@android:color/white" android:textSize="30sp" /> <Button android:id="@+id/three" android:layout_width="55dp" android:layout_height="48dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:background="@drawable/calculator_icon_bg" android:text="3" android:textColor="@android:color/white" android:textSize="30sp" /> <Button android:id="@+id/add" android:layout_width="55dp" android:layout_height="48dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:background="@drawable/calculator_icon_bg" android:text="+" android:textColor="@android:color/white" android:textSize="30sp" /> </TableRow> <TableRow android:id="@+id/row1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="16dp" android:layout_weight="1" android:gravity="center" > <Button android:id="@+id/four" android:layout_width="55dp" android:layout_height="48dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:background="@drawable/calculator_icon_bg" android:text="4" android:textColor="@android:color/white" android:textSize="30sp" /> <Button android:id="@+id/five" android:layout_width="55dp" android:layout_height="48dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:background="@drawable/calculator_icon_bg" android:text="5" android:textColor="@android:color/white" android:textSize="30sp" /> <Button android:id="@+id/six" android:layout_width="55dp" android:layout_height="48dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:background="@drawable/calculator_icon_bg" android:text="6" android:textColor="@android:color/white" android:textSize="30sp" /> <Button android:id="@+id/subtraction" android:layout_width="55dp" android:layout_height="48dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:background="@drawable/calculator_icon_bg" android:text="-" android:textColor="@android:color/white" android:textSize="30sp" /> </TableRow> <TableRow android:id="@+id/row2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="16dp" android:layout_weight="7" android:gravity="center" > <Button android:id="@+id/seven" android:layout_width="55dp" android:layout_height="48dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:background="@drawable/calculator_icon_bg" android:text="7" android:textColor="@android:color/white" android:textSize="30sp" /> <Button android:id="@+id/eight" android:layout_width="55dp" android:layout_height="48dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:background="@drawable/calculator_icon_bg" android:text="8" android:textColor="@android:color/white" android:textSize="30sp" /> <Button android:id="@+id/nine" android:layout_width="55dp" android:layout_height="48dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:background="@drawable/calculator_icon_bg" android:text="9" android:textColor="@android:color/white" android:textSize="30sp" /> <Button android:id="@+id/multiply" android:layout_width="55dp" android:layout_height="48dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:background="@drawable/calculator_icon_bg" android:text="*" android:textColor="@android:color/white" android:textSize="30sp" /> </TableRow> <TableRow android:id="@+id/row3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="16dp" android:layout_weight="0" android:gravity="center" > <Button android:id="@+id/zero" android:layout_width="55dp" android:layout_height="48dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:background="@drawable/calculator_icon_bg" android:text="0" android:textColor="@android:color/white" android:textSize="30sp" /> <Button android:id="@+id/dot" android:layout_width="55dp" android:layout_height="48dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:background="@drawable/calculator_icon_bg" android:text="." android:textColor="@android:color/white" android:textSize="30sp" /> <Button android:id="@+id/equal" android:layout_width="55dp" android:layout_height="48dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:background="@drawable/calculator_icon_bg" android:text="=" android:textColor="@android:color/white" android:textSize="30sp" /> <Button android:id="@+id/divide" android:layout_width="55dp" android:layout_height="48dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:background="@drawable/calculator_icon_bg" android:text="/" android:textColor="@android:color/white" android:textSize="30sp" /> </TableRow> </TableLayout> </LinearLayout>
(在F:\java\MySimpleCalculator)