您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > Android课程设计简易计算器and保存记录
Android课程设计简易计算器and保存记录JisuanqiMainActivity.javapackagecom.example.admin.myapplication;importandroid.app.Activity;importandroid.content.Context;importandroid.os.Bundle;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;importandroid.widget.CheckBox;importandroid.widget.EditText;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;publicclassJisuanqiMainActivityextendsActivity{/***Calledwhentheactivityisfirstcreated.*/privateEditTextoutput=null;privateEditTextinput=null;privateButtonbtn0=null;privateButtonbtn1=null;privateButtonbtn2=null;privateButtonbtn3=null;privateButtonbtn4=null;privateButtonbtn5=null;privateButtonbtn6=null;privateButtonbtn7=null;privateButtonbtn8=null;privateButtonbtn9=null;privateButtonbtnadd=null;privateButtonbtnsubtract=null;privateButtonbtnmultiply=null;privateButtonbtndivide=null;privateButtonbtnclear=null;privateButtonbtnresult=null;privateButtonbtndot=null;privateEditTexterrorzero=null;privateEditTextresultText=null;privateButtonwriteButton=null;privateButtonreadButton=null;privateCheckBoxappendBox=null;privateEditTexttextView=null;privateEditTextdisplayView=null;publicStringFILE_NAME=fileDemo.txt;privateStringstr=;//保存数字privateStringstrold=;//原数字privatecharact='';//记录“加减乘除等于”符号privateintcount=0;//判断要计算的次数,如果超过一个符号,先算出来一部分privateFloatresult=null;//计算的输出结果privateBooleanerrBoolean=false;//有错误的时候为true,无错为falseprivateBooleanflagBoolean=false;//一个标志,如果为true,可以响应运算消息,如果为false,不响应运算消息,只有前面是数字才可以响应运算消息privateBooleanflagDot=false;//小数点标志位@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_jisuanqi_main);output=(EditText)findViewById(R.id.output);input=(EditText)findViewById(R.id.input);errorzero=(EditText)findViewById(R.id.errorzero);resultText=(EditText)findViewById(R.id.resultText);writeButton=(Button)findViewById(R.id.writeButton);readButton=(Button)findViewById(R.id.readButton);textView=(EditText)findViewById(R.id.textView);displayView=(EditText)findViewById(R.id.displayView);appendBox=(CheckBox)findViewById(R.id.appendBox);btn0=(Button)findViewById(R.id.zero);btn1=(Button)findViewById(R.id.one);btn2=(Button)findViewById(R.id.two);btn3=(Button)findViewById(R.id.three);btn4=(Button)findViewById(R.id.four);btn5=(Button)findViewById(R.id.five);btn6=(Button)findViewById(R.id.six);btn7=(Button)findViewById(R.id.seven);btn8=(Button)findViewById(R.id.eight);btn9=(Button)findViewById(R.id.nine);btnadd=(Button)findViewById(R.id.add);btnsubtract=(Button)findViewById(R.id.subtract);btnmultiply=(Button)findViewById(R.id.multiply);btndivide=(Button)findViewById(R.id.divide);btnclear=(Button)findViewById(R.id.clear);btnresult=(Button)findViewById(R.id.result);btndot=(Button)findViewById(R.id.dot);//设置按钮侦听事件btn0.setOnClickListener(listener);btn1.setOnClickListener(listener);btn2.setOnClickListener(listener);btn3.setOnClickListener(listener);btn4.setOnClickListener(listener);btn5.setOnClickListener(listener);btn6.setOnClickListener(listener);btn7.setOnClickListener(listener);btn8.setOnClickListener(listener);btn9.setOnClickListener(listener);//执行运算btnadd.setOnClickListener(listener);btnsubtract.setOnClickListener(listener);btnmultiply.setOnClickListener(listener);btndivide.setOnClickListener(listener);btnclear.setOnClickListener(listener);btnresult.setOnClickListener(listener);btndot.setOnClickListener(listener);writeButton.setOnClickListener(writelistener);readButton.setOnClickListener(readlistener);//ATTENTION:Thiswasauto-generatedtoimplementtheAppIndexingAPI.//See}privateOnClickListenerlistener=newOnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubswitch(v.getId()){//输入数字caseR.id.zero:num(0);break;caseR.id.one:num(1);break;caseR.id.two:num(2);break;caseR.id.three:num(3);break;caseR.id.four:num(4);break;caseR.id.five:num(5);break;caseR.id.six:num(6);break;caseR.id.seven:num(7);break;caseR.id.eight:num(8);break;caseR.id.nine:num(9);break;caseR.id.dot:dot();break;//执行运算caseR.id.add:add();break;caseR.id.subtract:sub();break;caseR.id.multiply:multiply();break;caseR.id.divide:divide();break;caseR.id.clear:clear();break;//计算结果caseR.id.result:result();if(!errBoolean&&flagBoolean){output.setText(String.valueOf(result));}resultText.setText(strold+act+str+=+result+);break;default:break;}input.setText(strold+act+str);output.setText(String.valueOf(result));}};privateOnClickListenerwritelistener=newOnClickListener(){@OverridepublicvoidonClick(Viewview){//textView.setText();FileOutputStreamfos=null;try{if(appendBox.isChecked()){fos=openFileOutput(FILE_NAME,Context.MODE_APPEND);}else{fos=openFileOutput(FILE_NAME,Context.MODE_PRIVATE);}Stringtext=resultText.getText().toString();fos.write(text.getBytes());textView.setText(文件写入成功,写入长度:+text.length());//resultText.setText();}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace
本文标题:Android课程设计简易计算器and保存记录
链接地址:https://www.777doc.com/doc-5069608 .html