版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、100%運行通過這是自己編寫的,通過eclipse運行簡易計算器Android版(源碼)這是自己整理得,歡迎大家下載說明:每完成一次計算后,需先clear 清除記憶,當然也可以連算XML布局<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orie
2、ntation="vertical" > <TableLayout android:id="+id/tableLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:collapseColumns="4" > <TableRow android:id="+id/tableRow_et" android:layout_width=&
3、quot;fill_parent" android:layout_height="fill_parent" > <EditText android:id="+id/et" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_span="4" android:focusable="false" android:singleLine=&qu
4、ot;true" android:inputType="text" android:gravity="right"> </EditText> </TableRow> <TableRow android:id="+id/tableRow1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="+id
5、/bt_7" android:layout_width="40px" android:layout_height="40px" android:text="7" /> <Button android:id="+id/bt_8" android:layout_width="40px" android:layout_height="40px" android:text="8" /> <Button android:id=&qu
6、ot;+id/bt_9" android:layout_width="40px" android:layout_height="40px" android:text="9" /> <Button android:id="+id/bt_back" android:layout_width="40px" android:layout_height="40px" android:text="back" /> </TableRow
7、> <TableRow android:id="+id/tableRow2" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="+id/bt_4" android:layout_width="40px" android:layout_height="40px" android:text="4" /&
8、gt; <Button android:id="+id/bt_5" android:layout_width="40px" android:layout_height="40px" android:text="5" /> <Button android:id="+id/bt_6" android:layout_width="40px" android:layout_height="40px" android:text="6&qu
9、ot; /> <Button android:id="+id/bt_divide" android:layout_width="40px" android:layout_height="40px" android:text="/" /> </TableRow> <TableRow android:id="+id/tableRow3" android:layout_width="fill_parent" android:layout_heig
10、ht="fill_parent" > <Button android:id="+id/bt_1" android:layout_width="40px" android:layout_height="40px" android:text="1" /> <Button android:id="+id/bt_2" android:layout_width="40px" android:layout_height="40px&q
11、uot; android:text="2" /> <Button android:id="+id/bt_3" android:layout_width="40px" android:layout_height="40px" android:text="3" /> <Button android:id="+id/bt_multiply" android:layout_width="40px" android:layout_height
12、="40px" android:text="*" /> </TableRow> <TableRow android:id="+id/tableRow4" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="+id/bt_0" android:layout_width="50px" andr
13、oid:layout_height="40px" android:text="0" /> <Button android:id="+id/bt_point" android:layout_width="50px" android:layout_height="40px" android:text="." /> <Button android:id="+id/bt_add" android:layout_width="50p
14、x" android:layout_height="40px" android:text="+" /> <Button android:id="+id/bt_sub" android:layout_width="50px" android:layout_height="40px" android:text="-" /> </TableRow> <TableRow android:id="+id/tableRow4&quo
15、t; android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="+id/bt_equal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_span="3" android:text="=" /&
16、gt; <Button android:id="+id/bt_clear" android:layout_width="50px" android:layout_height="40px" android:text="clear" /> </TableRow> </TableLayout></LinearLayout>源代碼:package android.sdk;import android.app.Activity;import android.os.Bundle
17、;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class Android_calculatorActivity extends Activityprivate Button bt_1;private Button bt_2;private Button bt_3;private Button bt_4;private B
18、utton bt_5;private Button bt_6;private Button bt_7;private Button bt_8;private Button bt_9;private Button bt_0;private Button bt_add;private Button bt_sub; / 減private Button bt_multiply; / 乘private Button bt_divide; / 除private Button bt_back;private Button bt_equal; / 等于private Button bt_point; / 點p
19、rivate Button bt_clear; / 清除private EditText et_play; / 顯示private String str_oper = "+" / 運算符private StringBuffer str_display = new StringBuffer(); / 顯示private String str_result; / 結(jié)果顯示private double num1;private double num2;private boolean flag = true; / 小數(shù)點個數(shù)開關(guān)控制;private boolean b_sub, b
20、_mul, b_div; / 運算符開關(guān)控制Overridepublic void onCreate(Bundle savedInstanceState)super.onCreate(savedInstanceState);setContentView(R.layout.main);bt_0 = (Button) findViewById(R.id.bt_0);bt_1 = (Button) findViewById(R.id.bt_1);bt_2 = (Button) findViewById(R.id.bt_2);bt_3 = (Button) findViewById(R.id.bt_3
21、);bt_4 = (Button) findViewById(R.id.bt_4);bt_5 = (Button) findViewById(R.id.bt_5);bt_6 = (Button) findViewById(R.id.bt_6);bt_7 = (Button) findViewById(R.id.bt_7);bt_8 = (Button) findViewById(R.id.bt_8);bt_9 = (Button) findViewById(R.id.bt_9);bt_add = (Button) findViewById(R.id.bt_add);bt_sub = (Butt
22、on) findViewById(R.id.bt_sub);bt_multiply = (Button) findViewById(R.id.bt_multiply);bt_divide = (Button) findViewById(R.id.bt_divide);bt_back = (Button) findViewById(R.id.bt_back);bt_equal = (Button) findViewById(R.id.bt_equal);bt_point = (Button) findViewById(R.id.bt_point);bt_clear = (Button) find
23、ViewById(R.id.bt_clear);et_play = (EditText) findViewById(R.id.et);et_play.setText("0.0");bt_0.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)str_display.append("0");et_play.setText(str_display.toString(););bt_1.setOnClickListener(new OnClickListener()
24、Overridepublic void onClick(View v)str_display.append("1");et_play.setText(str_display.toString(););bt_2.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)str_display.append("2");et_play.setText(str_display.toString(););bt_3.setOnClickListener(new OnClick
25、Listener()Overridepublic void onClick(View v)str_display.append("3");et_play.setText(str_display.toString(););bt_4.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)str_display.append("4");et_play.setText(str_display.toString(););bt_5.setOnClickListener(n
26、ew OnClickListener()Overridepublic void onClick(View v)str_display.append("5");et_play.setText(str_display.toString(););bt_6.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)str_display.append("6");et_play.setText(str_display.toString(););bt_7.setOnClick
27、Listener(new OnClickListener()Overridepublic void onClick(View v)str_display.append("7");et_play.setText(str_display.toString(););bt_8.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)str_display.append("8");et_play.setText(str_display.toString(););bt_9.
28、setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)str_display.append("9");et_play.setText(str_display.toString(););bt_point.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)if (flag)str_display.append(".");flag = false;);bt_back.
29、setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)if (str_display.length() != 0)str_display.deleteCharAt(str_display.length() - 1);et_play.setText(str_display.toString(););bt_add.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)str_oper = "+&qu
30、ot;if (!(str_display.toString() = "")num1 += Double.parseDouble(str_display.toString();str_display = new StringBuffer("");if (!(str_result = null)num1 = Double.parseDouble(str_result);str_result = null;et_play.setText(String.valueOf(num1);flag = true;);bt_sub.setOnClickListener(n
31、ew OnClickListener()Overridepublic void onClick(View v)str_oper = "-"if (!b_sub && !(str_display.toString() = "")num1 = Double.parseDouble(str_display.toString();et_play.setText(String.valueOf(num1);str_display = new StringBuffer("");b_sub = true; elseif (!(str_
32、display.toString() = "")num1 -= Double.parseDouble(str_display.toString();str_display = new StringBuffer("");if (!(str_result = null)num1 = Double.parseDouble(str_result);str_result = null;et_play.setText(String.valueOf(num1);flag = true;);bt_multiply.setOnClickListener(new OnCli
33、ckListener()Overridepublic void onClick(View v)str_oper = "*"if (!b_mul && !(str_display.toString() = "")num1 = Double.parseDouble(str_display.toString();et_play.setText(String.valueOf(num1);str_display = new StringBuffer("");b_mul = true; elseif (!(str_display.
34、toString() = "")num1 *= Double.parseDouble(str_display.toString();str_display = new StringBuffer("");if (!(str_result = null)num1 = Double.parseDouble(str_result);str_result = null;et_play.setText(String.valueOf(num1);flag = true;);bt_divide.setOnClickListener(new OnClickListener
35、()Overridepublic void onClick(View v)str_oper = "/"if (!b_div && !(str_display.toString() = "")num1 = Double.parseDouble(str_display.toString();et_play.setText(String.valueOf(num1);str_display = new StringBuffer("");b_div = true; elseif (!(str_display.toString()
36、 = "")if (Double.parseDouble(str_display.toString() = 0)Toast.makeText(Android_calculatorActivity.this,"除數(shù)不能為0!", Toast.LENGTH_LONG).show(); elsenum1 /= Double.parseDouble(str_display.toString();str_display = new StringBuffer("");if (!(str_result = null)num1 = Double.parseDouble(str_result);str_result = null;et_play.setText(String.valueOf(num1);flag = true;);bt_clear.setOnClickListener(new OnClickListener()Ov
溫馨提示
- 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 云計算服務(wù)安全指南
- 美容美發(fā)服務(wù)流程與服務(wù)規(guī)范
- 醫(yī)院心理咨詢室建設(shè)方案
- 健身服務(wù)行業(yè)會員管理手冊
- 中醫(yī)院給排水系統(tǒng)改造方案
- 鋼結(jié)構(gòu)焊接工藝評定方案
- 非政府組織財務(wù)管理規(guī)范
- 醫(yī)院設(shè)備維護保養(yǎng)管理方案
- 幼兒園戶外自然角活動對探究興趣的激發(fā)-基于某園“四季種植”項目活動記錄
- 健身教練服務(wù)與運動指導手冊
- 貿(mào)易公司成本管理制度
- 國家中小學智慧教育平臺應(yīng)用指南
- 常見動物致傷診療規(guī)范(2021年版)
- 九年級年級組長工作總結(jié)
- 2025屆安徽省省級示范高中高一物理第一學期期末經(jīng)典試題含解析
- 現(xiàn)金日記賬模板(出納版)
- DB34T 1948-2013 建設(shè)工程造價咨詢檔案立卷標準
- 2024中藥藥渣處理協(xié)議
- 心源性暈厥的查房
- 機械氣道廓清技術(shù)臨床應(yīng)用專家共識(2023版)解讀
- 壓力性損傷風險評估與管理護理課件
評論
0/150
提交評論