计算器Android版源码.docx
- 文档编号:11581567
- 上传时间:2023-03-19
- 格式:DOCX
- 页数:17
- 大小:43.37KB
计算器Android版源码.docx
《计算器Android版源码.docx》由会员分享,可在线阅读,更多相关《计算器Android版源码.docx(17页珍藏版)》请在冰豆网上搜索。
计算器Android版源码
100%运行通过
这是自己编写的,通过eclipse运行
简易计算器Android版(源码)
这是自己整理得,欢迎大家下载
说明:
每完成一次计算后,需先clear清除记忆,当然也可以连算
XML布局
xmlversion="1.0"encoding="utf-8"?
>
android=" android: layout_width="fill_parent" android: layout_height="fill_parent" android: orientation="vertical"> android: id="@+id/tableLayout1" android: layout_width="match_parent" android: layout_height="wrap_content" android: collapseColumns="4"> android: id="@+id/tableRow_et" android: layout_width="fill_parent" android: layout_height="fill_parent"> android: id="@+id/et" android: layout_width="fill_parent" android: layout_height="fill_parent" android: layout_span="4" android: focusable="false" android: singleLine="true" android: inputType="text" android: gravity="right"> android: id="@+id/tableRow1" android: layout_width="fill_parent" android: layout_height="fill_parent"> android: id="@+id/tableRow2" android: layout_width="fill_parent" android: layout_height="fill_parent"> android: id="@+id/bt_4" android: layout_width="40px" android: layout_height="40px" android: text="4"/> android: id="@+id/bt_5" android: layout_width="40px" android: layout_height="40px" android: text="5"/> android: id="@+id/bt_6" android: layout_width="40px" android: layout_height="40px" android: text="6"/> android: id="@+id/bt_divide" android: layout_width="40px" android: layout_height="40px" android: text="/"/> android: id="@+id/tableRow3" android: layout_width="fill_parent" android: layout_height="fill_parent"> android: id="@+id/bt_1" android: layout_width="40px" android: layout_height="40px" android: text="1"/> android: id="@+id/bt_2" android: layout_width="40px" android: layout_height="40px" android: text="2"/> android: id="@+id/bt_3" android: layout_width="40px" android: layout_height="40px" android: text="3"/> android: id="@+id/bt_multiply" android: layout_width="40px" android: layout_height="40px" android: text="*"/> android: id="@+id/tableRow4" android: layout_width="fill_parent" android: layout_height="fill_parent"> android: id="@+id/bt_0" android: layout_width="50px" android: layout_height="40px" android: text="0"/> android: id="@+id/bt_point" android: layout_width="50px" android: layout_height="40px" android: text="."/> android: id="@+id/bt_add" android: layout_width="50px" android: layout_height="40px" android: text="+"/> android: id="@+id/bt_sub" android: layout_width="50px" android: layout_height="40px" android: text="-"/> android: id="@+id/tableRow4" android: layout_width="fill_parent" android: layout_height="fill_parent"> android: id="@+id/bt_equal" android: layout_width="fill_parent" android: layout_height="fill_parent" android: layout_span="3" android: text="="/> android: id="@+id/bt_clear" android: layout_width="50px" android: layout_height="40px" android: text="clear"/> 源代码: packageandroid.sdk; importandroid.app.Activity; importandroid.os.Bundle; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; importandroid.widget.EditText; importandroid.widget.Toast; publicclassAndroid_calculatorActivityextendsActivity { privateButtonbt_1; privateButtonbt_2; privateButtonbt_3; privateButtonbt_4; privateButtonbt_5; privateButtonbt_6; privateButtonbt_7; privateButtonbt_8; privateButtonbt_9; privateButtonbt_0; privateButtonbt_add; privateButtonbt_sub;//减 privateButtonbt_multiply;//乘 privateButtonbt_divide;//除 privateButtonbt_back; privateButtonbt_equal;//等于 privateButtonbt_point;//点 privateButtonbt_clear;//清除 privateEditTextet_play;//显示 privateStringstr_oper="+";//运算符 privateStringBufferstr_display=newStringBuffer();;//显示 privateStringstr_result;//结果显示 privatedoublenum1; privatedoublenum2; privatebooleanflag=true;//小数点个数开关控制; privatebooleanb_sub,b_mul,b_div;//运算符开关控制 @Override publicvoidonCreate(BundlesavedInstanceState) { 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); 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=(Button)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)findViewById(R.id.bt_clear); et_play=(EditText)findViewById(R.id.et); et_play.setText("0.0"); bt_0.setOnClickListener(newOnClickListener() { @Override publicvoidonClick(Viewv) { str_display.append("0"); et_play.setText(str_display.toString()); } }); bt_1.setOnClickListener(newOnClickListener() { @Override publicvoidonClick(Viewv) { str_display.append("1"); et_play.setText(str_display.toString()); } }); bt_2.setOnClickListener(newOnClickListener() { @Override publicvoidonClick(Viewv) { str_display.append("2"); et_play.setText(str_display.toString()); } }); bt_3.setOnClickListener(newOnClickListener() { @Override publicvoidonClick(Viewv) { str_display.append("3"); et_play.setText(str_display.toString()); } }); bt_4.setOnClickListener(newOnClickListener() { @Override publicvoidonClick(Viewv) { str_display.append("4"); et_play.setText(str_display.toString()); } }); bt_5.setOnClickListener(newOnClickListener() { @Override publicvoidonClick(Viewv) { str_display.append("5"); et_play.setText(str_display.toString()); } }); bt_6.setOnClickListener(newOnClickListener() { @Override publicvoidonClick(Viewv) { str_display.append("6"); et_play.setText(str_display.toString()); } }); bt_7.setOnClickListener(newOnClickListener() { @Override publicvoidonClick(Viewv) { str_display.append("7"); et_play.setText(str_display.toString()); } }); bt_8.setOnClickListener(newOnClickListener() { @Override publicvoidonClick(Viewv) { str_display.append("8"); et_play.setText(str_display.toString()); } }); bt_9.setOnClickListener(newOnClickListener() { @Override publicvoidonClick(Viewv) { str_display.append("9"); et_play.setText(str_display.toString()); } }); bt_point.setOnClickListener(newOnClickListener() { @Override publicvoidonClick(Viewv) { if(flag) { str_display.append("."); flag=false; } } }); bt_back.setOnClickListener(newOnClickListener() { @Override publicvoidonClick(Viewv) { if(str_display.length()! =0) { str_display.deleteCharAt(str_display.length()-1); et_play.setText(str_display.toString()); } } }); bt_add.setOnClickListener(newOnClickListener() { @Override publicvoidonClick(Viewv) { str_oper="+"; if(! (str_display.toString()=="")) { num1+=Double.parseDouble(str_display.toString()); str_display=newStringBuffer(""); } if(! (str_result==null)) { num1=Double.parseDouble(str_result); str_result=null; } et_play.setText(String.valueOf(num1)); flag=true; } }); bt_sub.setOnClickListener(newOnClickListener() { @Override publicvoidonClick(Viewv) { str_oper="-"; if(! b_sub&&! (str_display.toString()=="")) { num1=Double.parseDouble(str_display.toString()); et_play.setText(String.valueOf(num1)); str_display=newStringBuffer(""); b_sub=true; }else { if(! (str_display.toString()=="")) { num1-=Double.parseDouble(str_display.toString()); str_display=newStringBuffer(""); } if(! (str_result==null)) { num1=Double.parseDouble(str_result); str_result=null; } et_play.setText(String.valueOf(num1)); } flag=true; } }); bt_multiply.setOnClickListener(newOnClickListener() { @Override publicvoidonClick(Viewv) { str_oper="*"; if(! b_mul&&! (str_display.toString()=="")) { num1=Double.parseDouble(str_display.toString()); et_play.setText(String.valueOf(num1)); str_display=newStringBuffer(""); b_mul=true; }else { if
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算器 Android 源码