安卓开发的自制计算器源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
这是本人自己用安卓编写的计算器,代码的优化可能有所问题,不过大家都在学习阶段,欢迎大家批评指正,还是上源码吧。
先来运行图
package com.example.nanchen.exam_1_1;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button btn0;
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
private Button btn5;
private Button btn6;
private Button btn7;
private Button btn8;
private Button btn9;
private Button btnAdd;
private Button btnSub;
private Button btnMul;
private Button btnDiv;
private Button btnDec;
private Button btnEqual;
private Button btnClear;
private Button btnBackSpace;
private TextView textView_result;
double num1 = 0,num2 = 0,result = 0;//定义存储结果和两个操作数
boolean isCheckEqu = false;//检测是否点击了等于
int op = 0;//定义运算符
boolean hasDec = false;
boolean hasOp = false;
@Override
protected void onCreate Bundle savedInstanceState
super.onCreate savedInstanceState;
setContentView yout.activity_main;
textView_result = TextView findViewById R.id.result;
btn0 = Button findViewById R.id.btn0;
btn1 = Button findViewById R.id.btn1;
btn2 = Button findViewById R.id.btn2;
btn3 = Button findViewById R.id.btn3;
btn4 = Button findViewById R.id.btn4;
btn5 = Button findViewById R.id.btn5;
btn6 = Button findViewById R.id.btn6;
btn7 = Button findViewById R.id.btn7;
btn8 = Button findViewById R.id.btn8;
btn9 = Button findViewById R.id.btn9;
btnAdd = Button findViewById R.id.btn10;
btnSub = Button findViewById R.id.btn11;
btnMul = Button findViewById R.id.btn12;
btnDiv = Button findViewById R.id.btn13;
btnDec = Button findViewById R.id.btn14;
btnEqual = Button findViewById R.id.btn15;
btnClear = Button findViewById R.id.clear;
btnBackSpace = Button findViewById R.id.backSpace;
btn0.setOnClickListener this;
btn1.setOnClickListener this;
btn2.setOnClickListener this;
btn3.setOnClickListener this;
btn4.setOnClickListener this;
btn5.setOnClickListener this;
btn6.setOnClickListener this;
btn7.setOnClickListener this;
btn8.setOnClickListener this;
btn9.setOnClickListener this;
btnAdd.setOnClickListener this;
btnSub.setOnClickListener this;
btnDec.setOnClickListener this;
btnDiv.setOnClickListener this;
btnEqual.setOnClickListener this;
btnMul.setOnClickListener this;
btnClear.setOnClickListener this;
btnBackSpace.setOnClickListener this;
@Override
public void onClick View v
switch v.getId
case R.id.btn0:
if isCheckEqu
textView_result.setText null;
isCheckEqu=false;
String myString0 =
textView_result.getText.toString;
if myString0.equals"0"
myString0 = "";