版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、Android課程設(shè)計(jì)簡易計(jì)算器and保存記錄JisuanqiMainActivity.javapackage com.example.admin.myapplication;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;im
2、port android.widget.EditText;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class JisuanqiMainActivity extends Activity /* * Called when the activity is first created. */ private EditText output = null; private Ed
3、itText input = null; private Button btn0 = null; private Button btn1 = null; private Button btn2 = null; private Button btn3 = null; private Button btn4 = null; private Button btn5 = null; private Button btn6 = null; private Button btn7 = null; private Button btn8 = null; private Button btn9 = null;
4、 private Button btnadd = null; private Button btnsubtract = null; private Button btnmultiply = null; private Button btndivide = null; private Button btnclear = null; private Button btnresult = null; private Button btndot = null; private EditText errorzero = null; private EditText resultText = null;
5、private Button writeButton = null; private Button readButton = null; private CheckBox appendBox = null; private EditText textView = null; private EditText displayView = null; public String FILE_NAME = "fileDemo.txt" private String str = ""/保存數(shù)字 private String strold = ""
6、;/原數(shù)字 private char act = ' '/記錄“加減乘除等于”符號 private int count = 0;/判斷要計(jì)算的次數(shù),如果超過一個(gè)符號,先算出來一部分 private Float result = null;/計(jì)算的輸出結(jié)果 private Boolean errBoolean = false;/有錯(cuò)誤的時(shí)候?yàn)閠rue,無錯(cuò)為false private Boolean flagBoolean = false;/一個(gè)標(biāo)志,如果為true,可以響應(yīng)運(yùn)算消息,如果為false,不響應(yīng)運(yùn)算消息,只有前面是數(shù)字才可以響應(yīng)運(yùn)算消息 private Boolea
7、n flagDot = false; /小數(shù)點(diǎn)標(biāo)志位 Override public void onCreate(Bundle savedInstanceState) 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.i
8、d.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 = (Ch
9、eckBox) 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) f
10、indViewById(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); btndiv
11、ide = (Button) findViewById(R.id.divide); btnclear = (Button) findViewById(R.id.clear); btnresult = (Button) findViewById(R.id.result); btndot = (Button) findViewById(R.id.dot); /設(shè)置按鈕偵聽事件 btn0.setOnClickListener(listener); btn1.setOnClickListener(listener); btn2.setOnClickListener(listener); btn3.se
12、tOnClickListener(listener); btn4.setOnClickListener(listener); btn5.setOnClickListener(listener); btn6.setOnClickListener(listener); btn7.setOnClickListener(listener); btn8.setOnClickListener(listener); btn9.setOnClickListener(listener); /執(zhí)行運(yùn)算 btnadd.setOnClickListener(listener); btnsubtract.setOnCl
13、ickListener(listener); btnmultiply.setOnClickListener(listener); btndivide.setOnClickListener(listener); btnclear.setOnClickListener(listener); btnresult.setOnClickListener(listener); btndot.setOnClickListener(listener); writeButton.setOnClickListener(writelistener); readButton.setOnClickListener(re
14、adlistener); / ATTENTION: This was auto-generated to implement the App Indexing API. / See https:/g.co/AppIndexing/AndroidStudio for more information. private OnClickListener listener = new OnClickListener() public void onClick(View v) / TODO Auto-generated method stub switch (v.getId() /輸入數(shù)字 case R
15、.id.zero: num(0); break; case R.id.one: num(1); break; case R.id.two: num(2); break; case R.id.three: num(3); break; case R.id.four: num(4); break; case R.id.five: num(5); break; case R.id.six: num(6); break; case R.id.seven: num(7); break; case R.id.eight: num(8); break; case R.id.nine: num(9); bre
16、ak; case R.id.dot: dot(); break; /執(zhí)行運(yùn)算 case R.id.add: add(); break; case R.id.subtract: sub(); break; case R.id.multiply: multiply(); break; case R.id.divide: divide(); break; case R.id.clear: clear(); break; /計(jì)算結(jié)果 case R.id.result: result(); if (!errBoolean && flagBoolean) output.setText(St
17、ring.valueOf(result); resultText.setText(strold + act + str + "=" + result+" "); break; default: break; input.setText(strold + act + str); output.setText(String.valueOf(result); ; private OnClickListener writelistener = new OnClickListener() Override public void onClick(View view
18、) /textView.setText(""); FileOutputStream fos = null; try if (appendBox.isChecked() fos = openFileOutput(FILE_NAME, Context.MODE_APPEND); else fos = openFileOutput(FILE_NAME, Context.MODE_PRIVATE); String text = resultText.getText().toString(); fos.write(text.getBytes(); textView.setText(&
19、quot;文件寫入成功,寫入長度:" + text.length(); /resultText.setText(""); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); finally if (fos != null) try fos.flush(); fos.close(); catch (IOException e) e.printStackTrace(); ; private OnClickListener read
20、listener = new OnClickListener() Override public void onClick(View view) displayView.setText(""); FileInputStream fis = null; try fis = openFileInput(FILE_NAME); if (fis.available() = 0) return; byte readBytes = new bytefis.available(); while (fis.read(readBytes) != -1) String text = new S
21、tring(readBytes); displayView.setText(text); textView.setText("文件讀取成功,寫入長度:" + text.length(); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); ; private void dot() / TODO Auto-generated method stub if (!flagDot) str = str + "." flagB
22、oolean = false; flagDot = true; private void clear() / TODO Auto-generated method stub str = strold = "" count = 0; act = ' ' result = null; flagBoolean = false; flagDot = false; input.setText(strold + act + str); output.setText(""); errorzero.setText(""); displ
23、ayView.setText(""); textView.setText(""); resultText.setText(""); private void divide() / TODO Auto-generated method stub if (flagBoolean) check(); act = '/' flagBoolean = false; private void multiply() / TODO Auto-generated method stub if (flagBoolean) check();
24、 act = '*' flagBoolean = false; private void sub() / TODO Auto-generated method stub if (flagBoolean) check(); act = '-' flagBoolean = false; private void add() / TODO Auto-generated method stub if (flagBoolean) check(); act = '+' flagBoolean = false; private void check() / T
25、ODO Auto-generated method stub if (count >= 1) result(); str = String.valueOf(result); strold = str; str = "" count+; flagDot = false; errorzero.setText(""); /計(jì)算輸出結(jié)果 private void result() / TODO Auto-generated method stub if (flagBoolean) Float a, b; a = Float.parseFloat(strol
26、d); b = Float.parseFloat(str); if (b = 0 && act = '/') clear(); errorzero.setText("除數(shù)不能為零!"); /output.setText("除數(shù)不能為零!"); /errBoolean=true; if (!errBoolean) switch (act) case '+': result = a + b; break; case '-': result = a - b; break; case '*&
27、#39;: result = a * b; break; case '/': result = a / b; break; default: break; private void num(int i) / TODO Auto-generated method stub str = str + String.valueOf(i); flagBoolean = true; errorzero.setText(""); activity_jisuanqi_main.xml<?xml version="1.0" encoding=&quo
28、t;utf-8"?><ScrollView xmlns:android=" android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"
29、> <EditText android:id="+id/input" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:editable="false" android:hint="string/shuru" /> <EditText android:id="+id/ou
30、tput" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:editable="true" android:gravity="right" android:hint="string/shuchu" /> <RelativeLayout android:layout_width=&q
31、uot;fill_parent" android:layout_height="wrap_content" > <Button android:id="+id/seven" android:layout_width="80dp" android:layout_height="70dp" android:layout_alignParentLeft="true" android:text="string/seven" android:textSize=&q
32、uot;40sp" /> <Button android:id="+id/eight" android:layout_width="80dp" android:layout_height="70dp" android:layout_toRightOf="id/seven" android:text="string/eight" android:textSize="40sp" /> <Button android:id="+id/ni
33、ne" android:layout_width="80dp" android:layout_height="70dp" android:layout_toRightOf="id/eight" android:text="string/nine" android:textSize="40sp" /> <Button android:id="+id/add" android:layout_width="80dp" android:lay
34、out_height="70dp" android:layout_alignParentRight="true" android:layout_toRightOf="id/nine" android:text="string/add" android:textSize="40sp" /> <Button android:id="+id/four" android:layout_width="80dp" android:layout_height
35、="70dp" android:layout_alignParentLeft="true" android:layout_below="id/seven" android:text="string/four" android:textSize="40sp" /> <Button android:id="+id/five" android:layout_width="80dp" android:layout_height="70dp&q
36、uot; android:layout_below="id/eight" android:layout_toRightOf="id/four" android:text="string/five" android:textSize="40sp" /> <Button android:id="+id/six" android:layout_width="80dp" android:layout_height="70dp" android:layo
37、ut_below="id/nine" android:layout_toRightOf="id/five" android:text="string/six" android:textSize="40sp" /> <Button android:id="+id/subtract" android:layout_width="80dp" android:layout_height="70dp" android:layout_alignParent
38、Right="true" android:layout_below="id/add" android:layout_toRightOf="id/six" android:text="string/subtract" android:textSize="40sp" /> <Button android:id="+id/one" android:layout_width="80dp" android:layout_height="70dp
39、" android:layout_alignParentLeft="true" android:layout_below="id/four" android:text="string/one" android:textSize="40sp" /> <Button android:id="+id/two" android:layout_width="80dp" android:layout_height="70dp" android:l
40、ayout_below="id/five" android:layout_toRightOf="id/one" android:text="string/two" android:textSize="40sp" /> <Button android:id="+id/three" android:layout_width="80dp" android:layout_height="70dp" android:layout_below="
41、id/six" android:layout_toRightOf="id/two" android:text="string/three" android:textSize="40sp" /> <Button android:id="+id/multiply" android:layout_width="80dp" android:layout_height="70dp" android:layout_alignParentRight="tru
42、e" android:layout_below="id/subtract" android:layout_toRightOf="id/three" android:text="string/multiply" android:textSize="40sp" /> <Button android:id="+id/zero" android:layout_width="80dp" android:layout_height="70dp"
43、android:layout_alignParentLeft="true" android:layout_below="id/one" android:text="string/zero" android:textSize="40sp" /> <Button android:id="+id/clear" android:layout_width="80dp" android:layout_height="70dp" android:layout
44、_below="id/two" android:layout_toRightOf="id/zero" android:text="string/clear" android:textSize="40sp" /> <Button android:id="+id/result" android:layout_width="80dp" android:layout_height="70dp" android:layout_below="id/three&quo
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 勞務(wù)包工合同范例
- 建材買賣門窗合同范例
- 工程物料采購合同范例
- 住建部住房合同模板
- 學(xué)校元旦文藝匯演開幕式講話
- 鄉(xiāng)村組織員個(gè)人總結(jié)
- 徐州造價(jià)咨詢合同范例
- 建材供應(yīng)合作合同范例
- 小冰袋原料采購合同范例
- 人工挖樁合同范例
- 2024年國家公務(wù)員考試《行測》真題卷(行政執(zhí)法)答案和解析
- 《陸上風(fēng)電場工程設(shè)計(jì)概算編制規(guī)定及費(fèi)用標(biāo)準(zhǔn)》(NB-T 31011-2019)
- 貨運(yùn)證明范本
- 冷庫工程施工質(zhì)量保證體系及質(zhì)量保證措施
- 幕墻使用、保養(yǎng)、維修說明書
- GB∕T 33014.2-2016 道路車輛 電氣電子部件對窄帶輻射電磁能的抗擾性試驗(yàn)方法 第2部分:電波暗室法
- 數(shù)碼插畫課件新版]
- 八年級數(shù)學(xué)上冊期中考試試卷分析
- 毽球校本課程開發(fā)綱要
- 飛機(jī)試飛測試的現(xiàn)狀與對策
- 圖案—國外現(xiàn)代圖案ppt課件
評論
0/150
提交評論