收藏 分销(赏)

安卓计算器开发实验报告.doc

上传人:二*** 文档编号:4525118 上传时间:2024-09-26 格式:DOC 页数:20 大小:2.87MB 下载积分:5 金币
下载 相关 举报
安卓计算器开发实验报告.doc_第1页
第1页 / 共20页
本文档共20页,全文阅读请下载到手机保存,查看更方便
资源描述
上海电力学院 嵌入式WebOS应用开发 实验报告 实验名称: 使用Android Developer SDK开发应用程序     专 业: 姓 名:    班 级: 学 号: 一、 作品的运行环境及安卓SDK基础操作 SDK Android Developer是一款在windows系统上运行的针对Android应用开发的谷歌官方软件(需要JAVA环境支持)。 1、导入工程 2、建立虚拟机 在运行虚拟机是为保证机器的顺畅运行建议选择分辨率较低的虚拟机,但是其RAM最好设为512MB,因为部分程序如果调用资源过大会导致虚拟机无法运行。 3、虚拟机界面 二、 作品介绍 我的应用是一个计算器。能实现包括小数的加减乘除运算,结果过大会自动用科学记数法表示,另外还有退格跟清屏功能键。 三、 编程以及运行调试 (一)、在MyDesktop主界面中添加应用图标 1. 首先在我的桌面上添加你应用的图标以及文字,双击图标后就可以看见对应的代码,可直接在代码中进行修改图片文字的大小颜色等等。 以下是对应图像图标的代码 图片可以在左侧的选 项中自行进行挑选; 也可以添加自己的图片, 只要将图片放到对应的 文件夹之下在刷新就可 以,但不建议放分辨率 过高图片可能会出现超 出界面的等错误。 (二)、在res/layout目录下新建.xml文件,由于计算器的按钮很多,要在xml界面中添加排版: xml代码首末的 </AbsoluteLayout> 格式较为自由可以直接在界面中拖动图标位置以及修改大小,而其他layout则更会自动排列,各有优劣。 (三)、在src/weibo.test.ui目录下新建.java文件,计算器的按钮算法等都在此实现。 (三)、声明工程名 1、在应用中有三处需要声明,首先是在AndroidManifest.xml 2、然后是在MainActivity.java 四、 代码展示 (一)、.xml界面代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="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="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:gravity="right" android:inputType="text" android:singleLine="true" > </EditText> </TableRow> <TableRow android:id="@+id/tableRow1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/bt_7" android:layout_width="80px" android:layout_height="80px" android:text="7" /> <Button android:id="@+id/bt_8" android:layout_width="80px" android:layout_height="80px" android:text="8" /> <Button android:id="@+id/bt_9" android:layout_width="1px" android:layout_height="80px" android:text="9" /> <Button android:id="@+id/bt_back" android:layout_width="80px" android:layout_height="80px" android:text="back" /> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/bt_4" android:layout_width="80px" android:layout_height="80px" android:text="4" /> <Button android:id="@+id/bt_5" android:layout_width="80px" android:layout_height="80px" android:text="5" /> <Button android:id="@+id/bt_6" android:layout_width="80px" android:layout_height="80px" android:text="6" /> <Button android:id="@+id/bt_divide" android:layout_width="80px" android:layout_height="80px" android:text="/" /> </TableRow> <TableRow android:id="@+id/tableRow3" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/bt_1" android:layout_width="80px" android:layout_height="80px" android:text="1" /> <Button android:id="@+id/bt_2" android:layout_width="80px" android:layout_height="80px" android:text="2" /> <Button android:id="@+id/bt_3" android:layout_width="80px" android:layout_height="80px" android:text="3" /> <Button android:id="@+id/bt_multiply" android:layout_width="80px" android:layout_height="80px" 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" android:layout_height="80px" android:text="0" /> <Button android:id="@+id/bt_point" android:layout_width="50px" android:layout_height="80px" android:text="." /> <Button android:id="@+id/bt_add" android:layout_width="50px" android:layout_height="80px" android:text="+" /> <Button android:id="@+id/bt_sub" android:layout_width="50px" android:layout_height="80px" android:text="-" /> </TableRow> <TableRow android:id="@+id/tableRow4" 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="=" /> <Button android:id="@+id/bt_clear" android:layout_width="50px" android:layout_height="80px" android:text="clear" /> </TableRow> </TableLayout> </LinearLayout> (二)、.java功能代码 package weibo.test.ui; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import weibo.lixiaodaoaaa.ui.R; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ListView; import com.zsy.flipper.AppInfUtil; import com.zsy.flipper.AppInfo; public class fffActivity extends Activity { private Button bt_1; private Button bt_2; private Button bt_3; private Button bt_4; private Button 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; // 点 private Button bt_clear; // 清除 private EditText et_play; // 显示 private String str_oper = "+"; // 运算符 private StringBuffer str_display = new StringBuffer();; // 显示 private String str_result; // 结果显示 private double num1; private double num2; private boolean flag = true; // 小数点个数开关控制; private boolean b_sub, b_mul, b_div; // 运算符开关控制 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fff); 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"); bt_0.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { str_display.append("0"); et_play.setText(str_display.toString()); } }); bt_1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { str_display.append("1"); et_play.setText(str_display.toString()); } }); bt_2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { str_display.append("2"); et_play.setText(str_display.toString()); } }); bt_3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { str_display.append("3"); et_play.setText(str_display.toString()); } }); bt_4.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { str_display.append("4"); et_play.setText(str_display.toString()); } }); bt_5.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { str_display.append("5"); et_play.setText(str_display.toString()); } }); bt_6.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { str_display.append("6"); et_play.setText(str_display.toString()); } }); bt_7.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { str_display.append("7"); et_play.setText(str_display.toString()); } }); bt_8.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { str_display.append("8"); et_play.setText(str_display.toString()); } }); bt_9.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { str_display.append("9"); et_play.setText(str_display.toString()); } }); bt_point.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (flag) { str_display.append("."); flag = false; } } }); bt_back.setOnClickListener(new OnClickListener() { @Override public 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() { @Override public void onClick(View v) { str_oper = "+"; 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(new OnClickListener() { @Override public 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; } else { 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_multiply.setOnClickListener(new OnClickListener() { @Override public 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; } else { 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_divide.setOnClickListener(new OnClickListener() { @Override public 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; } else { if (!(str_display.toString() == "")) { if (Double.parseDouble(str_display.toString()) == 0) { Toast.makeText(fffActivity.this, "除数不能为0!", Toast.LENGTH_LONG).show(); } else { 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_clear.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { str_oper = "+"; str_display = new StringBuffer(""); str_result = null; num1 = 0; num2 = 0; flag = true; b_sub = false; b_mul = false; b_div = false; et_play.setText("0"); } }); bt_equal.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (s
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 学术论文 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服