本文实例讲述了Android开发实现的简单计算器功能。分享给大家供大家参考,具体如下:
这个Android计算器虽然还有点小bug,不过简单的计算功能还是没问题的哦;
先上图看效果
比较简单,所以我就没怎么写注释,应该一看就能明白的
有不明白的可以发信问我
先贴MainActivity.java代码
package com.example.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, bp, bs, bm, bd, bc, be;
ImageView delete;
TextView tv;
EditText show;
String showString = "", option = "";
int showfirst = 0;
String exception = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b0 = (Button) findViewById(R.id.bt_0);
b1 = (Button) findViewById(R.id.bt_1);
b2 = (Button) findViewById(R.id.bt_2);
b3 = (Button) findViewById(R.id.bt_3);
b4 = (Button) findViewById(R.id.bt_4);
b5 = (Button) findViewById(R.id.bt_5);
b6 = (Button) findViewById(R.id.bt_6);
b7 = (Button) findViewById(R.id.bt_7);
b8 = (Button) findViewById(R.id.bt_8);
b9 = (Button) findViewById(R.id.bt_9);
bp = (Button) findViewById(R.id.bt_plus);
bs = (Button) findViewById(R.id.bt_sub);
bm = (Button) findViewById(R.id.bt_mutilate);
bd = (Button) findViewById(R.id.bt_div);
bc = (Button) findViewById(R.id.bt_c);
be = (Button) findViewById(R.id.bt_equ);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickList