计算机源代码
package com.example.week2; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.app.Activity; public class MainActivity extends Activity implements OnClickListener{ //声明一些控件 Button btn0null; Button btn1null; Button btn2null; Button btn3null; Button btn4null; Button btn5null; Button btn6null; Button btn7null; Button btn8null; Button btn9null; Button btnBackspacenull; Button btnCEnull; Button btnCnull; Button btnAddnull; Button btnSubnull; Button btnMulnull; Button btnDivnull; Button btnEqunull; TextView tvResultnull; //声明两个参数。接收 tvResult 前后的值 double num10,num20; double Result0;//计算结果 int op0;//判断操作数, boolean isClickEqufalse;//判断是否按了“”按钮 Override protected void onCreateBundle savedInstanceState { super.onCreatesavedInstanceState; setContentViewR.layout.main; //从布局文件中获取控件, btn0ButtonfindViewByIdR.id.btn0; btn1ButtonfindViewByIdR.id.btn1; btn2ButtonfindViewByIdR.id.btn2; btn3ButtonfindViewByIdR.id.btn3; btn4ButtonfindViewByIdR.id.btn4; btn5ButtonfindViewByIdR.id.btn5; btn6ButtonfindViewByIdR.id.btn6; btn7ButtonfindViewByIdR.id.btn7; btn8ButtonfindViewByIdR.id.btn8; btn9ButtonfindViewByIdR.id.btn9; btnBackspaceButtonfindViewByIdR.id.btnBackspace; btnCEButtonfindViewByIdR.id.btnCE; btnCButtonfindViewByIdR.id.btnC; btnEquButtonfindViewByIdR.id.btnEqu; btnAddButtonfindViewByIdR.id.btnAdd; btnSubButtonfindViewByIdR.id.btnSub; btnMulButtonfindViewByIdR.id.btnMul; btnDivButtonfindViewByIdR.id.btnDiv; tvResultTextViewfindViewByIdR.id.tvResult; //添加监听\ btnBackspace.setOnClickListenerthis; btnCE.setOnClickListenerthis; btn0.setOnClickListenerthis; btn1.setOnClickListenerthis; btn2.setOnClickListenerthis; btn3.setOnClickListenerthis; btn4.setOnClickListenerthis; btn5.setOnClickListenerthis; btn6.setOnClickListenerthis; btn7.setOnClickListenerthis; btn8.setOnClickListenerthis; btn9.setOnClickListenerthis; btnAdd.setOnClickListenerthis; btnSub.setOnClickListenerthis; btnMul.setOnClickListenerthis; btnDiv.setOnClickListenerthis; btnEqu.setOnClickListenerthis; } Override public void onClickView v { switch v.getId { //btnBackspace 和 CE-------------------- case R.id.btnBackspace String myStrtvResult.getText.toString; try { tvResult.setTextmyStr.substring0, myStr.length -1; } catch Exception e { tvResult.setText““; } break; case R.id.btnCE tvResult.setTextnull; break; //btn0--9--------------------------- case R.id.btn0 ifisClickEqu { tvResult.setTextnull; isClickEqufalse; } String myStringtvResult.getText.toString; myString“0“; tvResult.setTextmyString; break; case R.id.btn1 ifisClickEqu { tvResult.setTextnull; isClickEqufalse; } String myString1tvResult.getText.toString; myString1“1“; tvResult.setTextmyString1; break; case R.id.btn2 if