自己第一个Android作品,Android计算器
2016-07-17 15:50
447 查看
写此时为了记录自己的开发历程,方便以后的查询。二则运算(能力有限,也没有想往更深处写),菜鸟级别,代码为参考慕课网上,听课代码,并加上自己的理解和参考的一些博客!有问题的地方欢迎指正,感激不尽!开发工具为Android studio。(第一次写博客,可读性估计很差)
废话不多说,代码轮上来(顺序按慕课老师讲解顺序)
1.先是xml(UI吧)
效果图:
1.首先我们需要一个在value/下添加一个colors.xml文件用来存放我们需要的颜色
,不然你全用#xxxxxx得多累,而且还不还用颜色,这里我们可以百度,有配好的颜色的代码。我们直接复制到我们的代码中,然后引用。
2.为了在方便我们布局和颜色的搭配,也是在慕课老师那里学了一招。可以在res/drawable下面建立xml。
我们来依次看看这些文件都是怎么做的。
(1).ashend_bg.xml(几个_bg都是颜色不一样,意义差不多)
目的是提供背景颜色,其实也可以不要颜色。直接在xml中添加颜色,这个主要是提供圆角
此处使用shape。(这里有更加完美的解释 http://www.cnblogs.com/cyanfei/archive/2012/07/27/2612023.html)
(2).orange_select.xml(select也是颜色不一样,内容差不多)
目的是为了按钮提供颜色和点击时会有另外一种颜色
2.然后是我们的Activity(代码都很简单,认真学了java都不会看不懂,而且注释很详细。只有几处需要解释下下的)
效果图:
废话不多说,代码轮上来(顺序按慕课老师讲解顺序)
1.先是xml(UI吧)
效果图:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.sorgs.administrator.caclulatordemo.MainActivity" tools:showIn="@layout/activity_main" android:background="@color/bisque"> <EditText android:layout_width="fill_parent" android:layout_marginTop="20dp" android:layout_height="70dip" android:id="@+id/et_input" android:background="@color/white" android:editable="false" android:gravity="right|bottom" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:orientation="horizontal" android:gravity="center_horizontal" > <Button android:layout_width="70dp" android:layout_height="70dp" android:text="C" android:paddingRight="10dp" android:paddingBottom="10dp" android:background="@drawable/white_select" android:textSize="20sp" android:gravity="right|bottom" android:id="@+id/btn_clear" /> <Button android:layout_width="70dp" android:layout_height="70dp" android:text="DEL" android:paddingRight="10dp" android:paddingBottom="10dp" android:background="@drawable/white_select" android:layout_marginLeft="10dp" android:textSize="20sp" android:gravity="right|bottom" android:id="@+id/btn_del" /> <Button android:layout_width="70dp" android:layout_height="70dp" android:text="÷" android:paddingRight="10dp" android:paddingBottom="10dp" android:background="@drawable/white_select" android:layout_marginLeft="10dp" android:textSize="20sp" android:gravity="right|bottom" android:id="@+id/btn_divide" /> <Button android:layout_width="70dp" android:layout_height="70dp" android:text="×" android:paddingRight="10dp" android:paddingBottom="10dp" android:background="@drawable/white_select" android:layout_marginLeft="10dp" android:textSize="20sp" android:gravity="right|bottom" android:id="@+id/btn_multiply" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal" android:gravity="center_horizontal" > <Button android:layout_width="70dp" android:layout_height="70dp" android:text="7" android:paddingRight="10dp" android:paddingBottom="10dp" android:background="@drawable/white_select" android:textSize="20sp" android:gravity="right|bottom" android:id="@+id/btn_7" /> <Button android:layout_width="70dp" android:layout_height="70dp" android:text="8" android:paddingRight="10dp" android:paddingBottom="10dp" android:background="@drawable/white_select" android:layout_marginLeft="10dp" android:textSize="20sp" android:gravity="right|bottom" android:id="@+id/btn_8" /> <Button android:layout_width="70dp" android:layout_height="70dp" android:text="9" android:paddingRight="10dp" android:paddingBottom="10dp" android:background="@drawable/white_select" android:layout_marginLeft="10dp" android:textSize="20sp" android:gravity="right|bottom" android:id="@+id/btn_9" /> <Button android:layout_width="70dp" android:layout_height="70dp" android:text="-" android:paddingRight="10dp" android:paddingBottom="10dp" android:background="@drawable/white_select" android:layout_marginLeft="10dp" android:textSize="20sp" android:gravity="right|bottom" android:id="@+id/btn_minus" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal" android:gravity="center_horizontal" > <Button android:layout_width="70dp" android:layout_height="70dp" android:text="4" android:paddingRight="10dp" android:paddingBottom="10dp" android:background="@drawable/white_select" android:textSize="20sp" android:gravity="right|bottom" android:id="@+id/btn_4" /> <Button android:layout_width="70dp" android:layout_height="70dp" android:text="5" android:paddingRight="10dp" android:paddingBottom="10dp" android:background="@drawable/white_select" android:layout_marginLeft="10dp" android:textSize="20sp" android:gravity="right|bottom" android:id="@+id/btn_5" /> <Button android:layout_width="70dp" android:layout_height="70dp" android:text="6" android:paddingRight="10dp" android:paddingBottom="10dp" android:background="@drawable/white_select" android:layout_marginLeft="10dp" android:textSize="20sp" android:gravity="right|bottom" android:id="@+id/btn_6" /> <Button android:layout_width="70dp" android:layout_height="70dp" android:text="+" android:paddingRight="10dp" android:paddingBottom="10dp" android:background="@drawable/white_select" android:layout_marginLeft="10dp" android:textSize="20sp" android:gravity="right|bottom" android:id="@+id/btn_plus" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_horizontal" android:layout_marginTop="10dp"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="70dp" android:layout_height="70dp" android:text="1" android:paddingRight="10dp" android:paddingBottom="10dp" android:background="@drawable/white_select" android:textSize="20sp" android:gravity="right|bottom" android:id="@+id/btn_1" /> <Button android:layout_width="70dp" android:layout_height="70dp" android:text="2" android:paddingRight="10dp" android:paddingBottom="10dp" android:background="@drawable/white_select" android:textSize="20sp" android:layout_marginLeft="10dp" android:gravity="right|bottom" android:id="@+id/btn_2" /> <Button android:layout_width="70dp" android:layout_height="70dp" android:text="3" android:paddingRight="10dp" android:paddingBottom="10dp" android:background="@drawable/white_select" android:textSize="20sp" android:gravity="right|bottom" android:layout_marginLeft="10dp" android:id="@+id/btn_3" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="10dp"> <Button android:layout_width="150dp" android:layout_height="70dp" android:text="0" android:paddingRight="10dp" android:paddingBottom="10dp" android:background="@drawable/white_select" android:textSize="20sp" android:gravity="right|bottom" android:id="@+id/btn_0" /> <Button android:layout_width="70dp" android:layout_height="70dp" android:text="." android:paddingRight="10dp" android:paddingBottom="10dp" android:background="@drawable/white_select" android:textSize="20sp" android:layout_marginLeft="10dp" android:gravity="right|bottom" android:id="@+id/btn_point" /> </LinearLayout> </LinearLayout> <Button android:layout_width="70dp" android:layout_height="150dp" android:text="=" android:background="@drawable/orange_select" android:layout_marginLeft="10dp" android:paddingRight="10dp" android:paddingBottom="10dp" android:textSize="20sp" android:gravity="right|bottom" android:id="@+id/btn_equal"/> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center" android:editable="false" android:text="能力有限,只能进行二则运算——by sorgs" android:textSize="16dp" android:textColor="@color/black" android:layout_marginTop="30dp" android:background="@color/sandybrown" android:id="@+id/textureView" /> </LinearLayout >xml里面注释不多,想必也不需太多解释,都是很简单布局和控件。其中有个背景颜色的问题需要详细说明
1.首先我们需要一个在value/下添加一个colors.xml文件用来存放我们需要的颜色
,不然你全用#xxxxxx得多累,而且还不还用颜色,这里我们可以百度,有配好的颜色的代码。我们直接复制到我们的代码中,然后引用。
2.为了在方便我们布局和颜色的搭配,也是在慕课老师那里学了一招。可以在res/drawable下面建立xml。
我们来依次看看这些文件都是怎么做的。
(1).ashend_bg.xml(几个_bg都是颜色不一样,意义差不多)
目的是提供背景颜色,其实也可以不要颜色。直接在xml中添加颜色,这个主要是提供圆角
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="5dp"/> <solid android:color="@color/beige" /> </shape>
此处使用shape。(这里有更加完美的解释 http://www.cnblogs.com/cyanfei/archive/2012/07/27/2612023.html)
(2).orange_select.xml(select也是颜色不一样,内容差不多)
目的是为了按钮提供颜色和点击时会有另外一种颜色
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/ashend_bg" android:state_pressed="true"/> <item android:drawable="@drawable/orange_bg"/> </selector></span>此处使用select(详细用法请参考http://blog.csdn.net/shakespeare001/article/details/7788400/)
2.然后是我们的Activity(代码都很简单,认真学了java都不会看不懂,而且注释很详细。只有几处需要解释下下的)
package com.sorgs.administrator.caclulatordemo; 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; public class MainActivity extends Activity implements OnClickListener{ Button btn_0;//0数字按钮 Button btn_1;//1数字按钮 Button btn_2;//2数字按钮 Button btn_3;//3数字按钮 Button btn_4;//4数字按钮 Button btn_5;//5数字按钮 Button btn_6;//6数字按钮 Button btn_7;//7数字按钮 Button btn_8;//8数字按钮 Button btn_9;//9数字按钮 Button btn_point;//小数点按钮 Button btn_clear;//清除按钮 Button btn_del;//删除按钮 Button btn_plus;//加好按钮 Button btn_minus;//减号按钮 Button btn_divide;//除号按钮 Button btn_multiply;//乘号按钮 Button btn_equle;//等于按钮 //以上建立按钮 EditText et_input;//显示输出内容的显示屏 boolean clear_flag;//清空标识,用于等号之后清空 @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.content_main);//控制xml为content_main btn_0 = (Button) findViewById(R.id.btn_0); btn_1 = (Button) findViewById(R.id.btn_1); btn_2 = (Button) findViewById(R.id.btn_2); btn_3 = (Button) findViewById(R.id.btn_3); btn_4 = (Button) findViewById(R.id.btn_4); btn_5 = (Button) findViewById(R.id.btn_5); btn_6 = (Button) findViewById(R.id.btn_6); btn_7 = (Button) findViewById(R.id.btn_7); btn_8 = (Button) findViewById(R.id.btn_8); btn_9 = (Button) findViewById(R.id.btn_9); btn_point = (Button) findViewById(R.id.btn_point); btn_del = (Button) findViewById(R.id.btn_del); btn_plus = (Button) findViewById(R.id.btn_plus); btn_clear = (Button) findViewById(R.id.btn_clear); btn_minus = (Button) findViewById(R.id.btn_minus); btn_multiply = (Button) findViewById(R.id.btn_multiply); btn_divide = (Button) findViewById(R.id.btn_divide); btn_equle = (Button) findViewById(R.id.btn_equal); //以上是实例化按钮 et_input = (EditText) findViewById(R.id.et_input);//实例化显示屏 btn_0.setOnClickListener(this); btn_1.setOnClickListener(this); btn_2.setOnClickListener(this); btn_3.setOnClickListener(this); btn_4.setOnClickListener(this); btn_5.setOnClickListener(this); btn_6.setOnClickListener(this); btn_7.setOnClickListener(this); btn_8.setOnClickListener(this); btn_9.setOnClickListener(this); btn_point.setOnClickListener(this); btn_del.setOnClickListener(this); btn_plus.setOnClickListener(this); btn_clear.setOnClickListener(this); btn_minus.setOnClickListener(this); btn_multiply.setOnClickListener(this); btn_divide.setOnClickListener(this); btn_equle.setOnClickListener(this); //以上设置按钮的点击事件 } @Override public void onClick(View v) { String str = et_input.getText().toString(); //取出显示屏内容 switch (v.getId()){ //判断点的是那个按钮 case R.id.btn_0: //建立数字0—9和. case R.id.btn_1: case R.id.btn_2: case R.id.btn_3: case R.id.btn_4: case R.id.btn_5: case R.id.btn_6: case R.id.btn_7: case R.id.btn_8: case R.id.btn_9: case R.id.btn_point: if(clear_flag){ // clear_flag =false; str = ""; //计算下一个时候,应将原来的设置为空 et_input.setText(""); } et_input.setText(str+((Button)v).getText()); //将点击的文字添加到输入框里面(str原来输入框中内容) break; case R.id.btn_plus: //建立+-×÷ case R.id.btn_minus: case R.id.btn_multiply: case R.id.btn_divide: if(clear_flag){ clear_flag =false; str = ""; //计算下一个时候,应将原来的设置为空 et_input.setText(""); } et_input.setText(str+" "+((Button)v).getText()+" ");//将点击的运算符添加到输入框前后有“ ”用于区别 break; case R.id.btn_del: //建立删除 if(clear_flag){ clear_flag= false; str = ""; //计算下一个时候,应将原来的设置为空 et_input.setText(""); }else if (str != null &&!str.equals("")) { //如果显示屏里面不是NULL也不是空 et_input.setText(str.substring(0,str.length()-1)); //从后面长度减一 } break; case R.id.btn_clear: //建立清除 clear_flag = false; str = ""; //计算下一个时候,应将原来的设置为空 et_input.setText(""); //将显示屏内容置空 break; case R.id.btn_equal: //建立等于 getResult(); //获取结算结果 break; } } //进行计算 private void getResult(){ String exp = et_input.getText().toString(); //取出显示屏内容并转化为String if (exp == null||exp.equals("")){//如果内容为null和空,直接返回 return; } if(!exp.contains(" ")){//如果不包含空格(运算符前面有空格),直接返回(比如点了数字,没有运算符) return; } if(clear_flag){ clear_flag = false; return; } clear_flag = true; double result = 0; //定义一个double的result=0 String s1 = exp.substring(0,exp.indexOf(' '));//截取运算符前面的字符 String op = exp.substring(exp.indexOf(' ')+1,exp.indexOf(' ')+2);//截取运算符 String s2 = exp.substring(exp.indexOf(' ')+3);//截取运算符后面的字符 if(!s1.equals("")&&!s2.equals("")){ //如果S1或者S2不为空 double d1 = Double.parseDouble(s1); //强制将S1转换为double类型 double d2 = Double.parseDouble(s2); //强制将S2转换为double类型 if(op.equals("+")){ //如果op为四中情况的方案 result = d1+d2; }else if(op.equals("-")){ result = d1-d2; }else if(op.equals("×")){ result = d1*d2; }else if(op.equals("÷")){ if(d2==0){ Toast.makeText(MainActivity.this, "除数不能为0!!!",Toast.LENGTH_LONG).show(); et_input.setText("0"); }else{ result = d1/d2; } } if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")){ //如果没有小数点则为int类型且op不为÷ int r = (int)result; //强制转换为int类型 et_input.setText(r+""); }else{ //其中含有小数点,则输出double类型 et_input.setText(result+""); } }else if(!s1.equals("")&&s2.equals("")){ //S1不为空,S2为空 double d1 = Double.parseDouble(s1); result = d1; Toast.makeText(MainActivity.this, "不具备运算",Toast.LENGTH_LONG).show(); et_input.setText(result+""); //不进行计算,返回S1 }else if(s1.equals("")&&!s2.equals("")){ //S1为空,S2不为空 double d2 = Double.parseDouble(s2); if(op.equals("+")){ result = 0+d2; }else if(op.equals("-")){ result = 0-d2; }else if(op.equals("×")){ result = 0; }else if(op.equals("÷")){ result = 0; } if(!s2.contains(".")){ int r = (int)result; et_input.setText(r+""); }else{ et_input.setText(result+""); } }else{ et_input.setText(""); } } }
Toast.makeText(MainActivity.this, "除数不能为0!!!",Toast.LENGTH_LONG).show();这里采用消息显示的一个方法,个人觉得这样很好看。
效果图:
Toast.makeText的方法也不在赘述了,因为很多博客都有写(http://www.cnblogs.com/ycxyyzw/archive/2013/03/12/2955845.html)
相关文章推荐
- Android 高德地图API学习笔记(2)
- Android消息机制
- 解决ScrollView.setOnScrollChangeListener() API23问题;
- Ionic- Android 开发环境搭建
- Android Image Loader 第三方库对比测试
- Android 高德地图API学习笔记
- Mac下Android Studio快捷键
- Android之Activity生命周期的浅析(二)
- Android之Activity生命周期浅析(一)
- Android手机与多个BLE设备通信
- 【转】Android端与Android端利用WIFI进行FTP通信
- Android 自定义数字加减器
- [Android Pro] Android 性能分析工具dumpsys的使用
- (OK) [android-x86-6.0-rc1] grub - set timeout=5
- Android启动模式与onNewIntent
- android fragment重影问题
- Android 隐式意图和显示意图的使用场景
- Android 自定义ViewGroup 实战篇 -> 实现FlowLayout
- android软件盘弹出一系列问题
- android 线程小笔记2