学校组织的《移动互联网应用软件开发》技能竞赛,所有参赛命题均基于 Android 4.2 版本实现,比赛推荐使用的开发环境为:ADT, JDK 1.6, Android SDK 4.1
经过了四天的培训我们小组选择在计算器应用
计算器能够实现:
目前我可以完成全部功能,虽然是扩展老师的基础程序,但还是很有成就感的。
我们先进行了对Java的培训,多线程、匿名内部类、接口及其子类异常的捕获,然后使用两本书《Android移动应用开发项目教程》和《Android编程权威指南》来开展对Android的学习。给我的感觉是,开发Android应用程序和普通的Java应用程序没有太大区别,最后运行的时候才Android虚拟机上运行就好。
开发Android项目的时候虽然老师给我们推荐谷歌推出了新的Android开发环境Android Studio,但是我们用的是带插件的Eclipse,安装好JDK配置好ADT集成开发环境之后就开始了。升级Android SDK也很重要,直接升级的话需要连接Google服务器的,所以是需要×××的,为了避免这种情况,也可以到高校自己的网站去升级。就不废话了,上代码。
MainActivity.java
package com.wangjingliang.calc;import java.util.*;import com.shiyifei.calc.R;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity { EditText result; // 保存当前输入框内容 String current = "0"; // 保存之前输入的内容 String prev = ""; // 保存输入状态,是刚刚输入了一个数字,还是输入了一个运算、控制符号 int status = 0; // 0 刚刚输入了数字, 1刚刚输入了运算符号 // 保存刚刚输入的运算符号 String com = ""; //保存存储器中的数值 String num = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 拿到输入框对象 result = (EditText) findViewById(R.id.editText1); // 让输入框一开始显示0 update(); } // 此处的方法是和在布局中给按钮们设定的onclick属性对应的 // 点完那些按钮后,执行该方法 public void click(View v) { // 现获取按钮上的内容 String btn_text = ((Button) v).getText().toString(); // 通过getId可以获取被点的按钮的id,和我们设定的id比照 switch (v.getId()) { case R.id.b0: case R.id.b1: case R.id.b2: case R.id.b3: case R.id.b4: case R.id.b5: case R.id.b6: case R.id.b7: case R.id.b8: case R.id.b9: // 检测上一次输入的是什么,如果是数字,就连接显示,如果是运算符,清空输入框, // 开始新的数字的显示 if (status == 0) { // 检测当前输入框中是否仅有0,如果仅有零,就将当前内容替换为输入的数字 // 如果不是仅有0,则将当前按下的数字,连接到当前内容的最后 if (onlyZero()) current = btn_text; else current += btn_text; // 把当前状态置为0,表示刚刚输入的是数字 status = 0; } else if (status == 1) { current = btn_text; } break; // 如果按下了加号 case R.id.b_add: case R.id.b_sub: case R.id.b_multi: case R.id.b_div: case R.id.b_sqrt: case R.id.b_rev: case R.id.b_per: // 把当前状态置为1,表示刚刚输入的是符号 status = 1; // 把当前输入框的内容,作为之前输入的内容保存在prev中 prev = current; // 保存当前运算符号 com = btn_text; break; case R.id.b_equal: compute(); break; case R.id.b_c: clearAll(); break; case R.id.b_ce: clearPart(); break; case R.id.b_back: clearSingle(); break; case R.id.b_dot: addDot(); break; case R.id.b_sign: addSign(); break; case R.id.b_ms: Store(); break; case R.id.b_mr: Out(); break; case R.id.b_mp: addStore(); break; case R.id.b_mc: clearIn(); break; } // 更新输入框的当前内容 update(); } /** * 按下等号后调用的方法,计算 * * @return 返回计算结果 */ private double compute() { double old = Double.parseDouble(prev); double cur = Double.parseDouble(current); Log.d("------获取的按钮文字-------", com); double result = 0; if (com.equals("+")) { result = old + cur; } else if (com.equals("-")) { result = old - cur; } else if (com.equals("*")) { result = old * cur; } else if (com.equals("/")) { // 除数为0检测 if (cur == 0) { warning(); } else { result = old / cur; } } else if (com.equals("sqt")) { result = Math.sqrt(old); } else if (com.equals("1/x")) { result = 1 / old; } else if (com.equals("%")) { cur = (old*cur) / 100; } else if (com.equals("")) // 计算得到结果后,立刻更新输入框 prev = current; current = result + ""; update(); return result; } /** * 清理之前和当前的数据 */ private void clearAll() { prev = ""; current = ""; com = ""; status = 0; } private void clearPart() { current = ""; } private void clearSingle() { if (current.length() > 1) { current = current.substring(0, current.length() - 1); } else { current = "0"; } result.setText(current); } private void Store() { num = current ; } private void addStore() { double cur = Double.parseDouble(current); double n = Double.parseDouble(num); n += cur; num = String.valueOf(n); } private void Out() { current = num ; } private void clearIn() { num = ""; } private void addDot() { if (current.indexOf(".") != -1) { current = ""; } else { current = current + "."; } } private void addSign() { if (current.indexOf("-") != -1) { current = current.replace('-', ' '); } else { current = "-" + current; } } // 更新输入框内容 public void update() { result.setText(current); } public void warning() { Toast.makeText(MainActivity.this, "除数不能为零", 0).show(); } /** * 检测当前输入的内容是否仅仅是0 * * @return 如果只有零,返回true,否则false */ public boolean onlyZero() { return current.length() == 1 && current.equals("0"); } }
BuildConfig.Java
/** Automatically generated file. DO NOT MODIFY */ package com.shiyifei.calc; public final class BuildConfig { public final static boolean DEBUG = true; }
R.java
/* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */package com.shiyifei.calc;public final class R { public static final class attr { } public static final class dimen { /** Default screen margins, per the Android Design guidelines. Customize dimensions originally defined in res/values/dimens.xml (such as screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here. */ public static final int activity_horizontal_margin=0x7f040000; public static final int activity_vertical_margin=0x7f040001; } public static final class drawable { public static final int ic_launcher=0x7f020000; public static final int ic_launcher_calc=0x7f020001; } public static final class id { public static final int LinearLayout1=0x7f080000; public static final int action_settings=0x7f080023; public static final int b0=0x7f08001e; public static final int b1=0x7f080017; public static final int b2=0x7f080018; public static final int b3=0x7f080019; public static final int b4=0x7f080010; public static final int b5=0x7f080011; public static final int b6=0x7f080012; public static final int b7=0x7f080009; public static final int b8=0x7f08000a; public static final int b9=0x7f08000b; public static final int b_add=0x7f080021; public static final int b_back=0x7f080004; public static final int b_c=0x7f080006; public static final int b_ce=0x7f080005; public static final int b_div=0x7f08000c; public static final int b_dot=0x7f080020; public static final int b_equal=0x7f080022; public static final int b_mc=0x7f080008; public static final int b_mp=0x7f08001d; public static final int b_mr=0x7f08000f; public static final int b_ms=0x7f080016; public static final int b_multi=0x7f080013; public static final int b_per=0x7f080014; public static final int b_rev=0x7f08001b; public static final int b_sign=0x7f08001f; public static final int b_sqrt=0x7f08000d; public static final int b_sub=0x7f08001a; public static final int editText1=0x7f080001; public static final int r1=0x7f080002; public static final int r2=0x7f080007; public static final int r3=0x7f08000e; public static final int r4=0x7f080015; public static final int r5=0x7f08001c; public static final int textView1=0x7f080003; } public static final class layout { public static final int activity_main=0x7f030000; } public static final class menu { public static final int main=0x7f070000; } public static final class string { public static final int action_settings=0x7f050001; public static final int app_name=0x7f050000; public static final int hello_world=0x7f050002; } public static final class style { /** Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.xml on newer devices. Theme customizations available in newer API levels can go in res/values-vXX/styles.xml, while customizations related to backward-compatibility can go here. Base application theme for API 11+. This theme completely replaces AppBaseTheme from res/values/styles.xml on API 11+ devices. API 11 theme customizations can go here. Base application theme for API 14+. This theme completely replaces AppBaseTheme from BOTH res/values/styles.xml and res/values-v11/styles.xml on API 14+ devices. API 14 theme customizations can go here. */ public static final int AppBaseTheme=0x7f060000; /** Application theme. All customizations that are NOT specific to a particular API-level can go here. */ public static final int AppTheme=0x7f060001; } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。