用android studio写一个简单并且bug奇多的计算器
2017-11-28 19:32
267 查看
思路:先布局,在将相应的按键实例化,创建监听器,计算。可以说是非常简单了。
贴一下代码:
布局代码:
简单的布局,建议不要拖动布局,用手打的布局更方便。
main_activity文件:
bug有好几个,但是由于是练手的程序,所以不想再做修改。
贴一下代码:
布局代码:
<?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:background="@android:color/darker_gray" tools:context="com.example.fxh98.cal.MainActivity"> <EditText android:id="@+id/input" android:layout_width="match_parent" android:layout_height="90dp" android:ems="10" android:enabled="false" android:textSize="30sp" android:gravity="right|bottom" android:inputType="textPersonName" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="290dp" android:layout_height="match_parent" android:text="C" android:id="@+id/btn_C" android:textSize="30sp" /> <Button android:layout_width="90dp" android:layout_height="match_parent" android:text="DEL" android:id="@+id/btn_Del" android:textSize="30sp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="95dp" android:layout_height="80dp" android:text="7" android:id="@+id/btn_7" android:textSize="40sp"/> <Button android:layout_width="95dp" android:layout_height="80dp" android:text="8" android:id="@+id/btn_8" android:textSize="40sp"/> <Button android:layout_width="95dp" android:layout_height="80dp" android:text="9" android:id="@+id/btn_9" android:textSize="40sp"/> <Button android:layout_width="95dp" android:layout_height="80dp" android:text="/" android:id="@+id/btn_div" android:textSize="40sp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="95dp" android:layout_height="80dp" android:text="4" android:id="@+id/btn_4" android:textSize="40sp"/> <Button android:layout_width="95dp" android:layout_height="80dp" android:text="5" android:id="@+id/btn_5" android:textSize="40sp"/> <Button android:layout_width="95dp" android:layout_height="80dp" android:text="6" android:id="@+id/btn_6" android:textSize="40sp"/> <Button android:layout_width="95dp" android:layout_height="80dp" android:text="*" android:id="@+id/btn_mul" android:textSize="40sp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="95dp" android:layout_height="80dp" android:text="1" android:id="@+id/btn_1" android:textSize="40sp"/> <Button android:layout_width="95dp" android:layout_height="80dp" android:text="2" android:id="@+id/btn_2" android:textSize="40sp"/> <Button android:layout_width="95dp" android:layout_height="80dp" android:text="3" android:id="@+id/btn_3" android:textSize="40sp"/> <Button android:layout_width="95dp" android:layout_height="80dp" android:text="-" android:id="@+id/btn_sub" android:textSize="40sp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="95dp" android:layout_height="80dp" android:text="." android:id="@+id/btn_de" android:textSize="40sp"/> <Button android:layout_width="95dp" android:layout_height="80dp" android:text="0" android:id="@+id/btn_0" android:textSize="40sp"/> <Button android:layout_width="95dp" android:layout_height="80dp" android:text="=" android:id="@+id/btn_eql" android:textSize="40sp"/> <Button android:layout_width="95dp" android:layout_height="80dp" android:text="+" android:id="@+id/btn_add" android:textSize="40sp"/> </LinearLayout> </LinearLayout>
简单的布局,建议不要拖动布局,用手打的布局更方便。
main_activity文件:
package com.example.fxh98.cal; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button bt_0,bt_1,bt_2,bt_3,bt_4,bt_5,bt_6,bt_7,bt_8,bt_9,bt_de; Button bt_mul,bt_div,bt_add,bt_sub; Button bt_clr,bt_del,bt_eql; EditText et_input; boolean clear_flag; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //实例化对象 bt_0 = findViewById(R.id.btn_0); bt_1 = findViewById(R.id.btn_1); bt_2 = findViewById(R.id.btn_2); bt_3 = findViewById(R.id.btn_3); bt_4 = findViewById(R.id.btn_4); bt_5 = findViewById(R.id.btn_5); bt_6 = findViewById(R.id.btn_6); bt_7 = findViewById(R.id.btn_7); bt_8 = findViewById(R.id.btn_8); bt_9 = findViewById(R.id.btn_9); bt_add = findViewById(R.id.btn_add); bt_sub = findViewById(R.id.btn_sub); bt_mul = findViewById(R.id.btn_mul); bt_div = findViewById(R.id.btn_div); bt_de = findViewById(R.id.btn_de); bt_eql = findViewById(R.id.btn_eql); bt_clr = findViewById(R.id.btn_C); bt_del = findViewById(R.id.btn_Del); et_input = findViewById(R.id.input); //设置监听器 bt_0.setOnClickListener(this); bt_1.setOnClickListener(this); bt_2.setOnClickListener(this); bt_3.setOnClickListener(this); bt_4.setOnClickListener(this); bt_5.setOnClickListener(this); bt_6.setOnClickListener(this); bt_7.setOnClickListener(this); bt_8.setOnClickListener(this); bt_9.setOnClickListener(this); bt_eql.setOnClickListener(this); bt_del.setOnClickListener(this); bt_de.setOnClickListener(this); bt_div.setOnClickListener(this); bt_mul.setOnClickListener(this); bt_add.setOnClickListener(this); bt_sub.setOnClickListener(this); bt_clr.setOnClickListener(this); } @Override public void onClick(View view) { String str = et_input.getText().toString(); switch (view.getId()){ case R.id.btn_0: case R.id.btn_1: case R.id.btn_2: case R.id.btn_3: 4000 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_de: if (clear_flag){ clear_flag = false; str = ""; et_input.setText(""); } et_input.setText(str + ((Button)view).getText()); break; case R.id.btn_add: case R.id.btn_mul: case R.id.btn_div: case R.id.btn_sub: if (clear_flag){ clear_flag = false; str = ""; et_input.setText(""); } if(str.contains("+")||str.contains("-")||str.contains("*")||str.contains("/")){ et_input.setText(str.substring(0,str.length()-3) + " " + ((Button)view).getText() + " "); } else et_input.setText(str + " " + ((Button)view).getText() + " "); break; case R.id.btn_Del: if (clear_flag){ clear_flag = false; str = ""; et_input.setText(""); } else if (str != null && !str.equals("")){ et_input.setText(str.substring(0,str.length()-1)); } break; case R.id.btn_C: clear_flag = false; str = ""; et_input.setText(""); break; case R.id.btn_eql: getResult(); break; } } public void getResult() { String exp = et_input.getText().toString(); if(exp == null||exp.equals("")) return; if(!exp.contains(" ")){//dont hava + - * / return ; } if(clear_flag){ clear_flag = false; return; } String s1=exp.substring(0,exp.indexOf(" ")); String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2); String s2=exp.substring(exp.indexOf(" ")+3); double cnt=0; if(!s1.equals("")&&!s2.equals("")){ double d1=Double.parseDouble(s1); double d2=Double.parseDouble(s2); if(op.equals("+")){ cnt = d1 + d2; } if(op.equals("-")){ cnt = d1 - d2; } if(op.equals("*")){ cnt = d1 * d2; } if(op.equals("/")){ if(d1 != 0&&d2 != 0) cnt = d1 / d2; if(d2 == 0) cnt = 0; } if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")) { int res = (int) cnt; et_input.setText(res+""); }else { et_input.setText(cnt+"");} } else if(!s1.equals("")&&s2.equals("")){ double d1=Double.parseDouble(s1); if(op.equals("+")){ cnt=d1; } if(op.equals("-")){ cnt=d1; } if(op.equals("*")){ cnt=0; } if(op.equals("/")){ cnt=0; } if(!s1.contains(".")) { int res = (int) cnt; et_input.setText(res+""); }else { et_input.setText(cnt+"");} } else if(s1.equals("")&&!s2.equals("")){ double d2=Double.parseDouble(s2); if(op.equals("+")){ cnt=d2; } if(op.equals("-")){ cnt=0-d2; } if(op.equals("*")){ cnt=0; } if(op.equals("/")){ cnt=0; } if(!s2.contains(".")) { int res = (int) cnt; et_input.setText(res+""); }else { et_input.setText(cnt+"");} } else { et_input.setText(""); } } }
bug有好几个,但是由于是练手的程序,所以不想再做修改。
相关文章推荐
- 自己写的一个计算器,极其简单,并且很多功能不完备
- [置顶] Android Studio 2.2 实现一个简单的计算器
- 1-前端之路的开始-一个简单的计算器
- python+PyQt4写的一个简单的计算器
- 利用ANTLR4实现一个简单的四则运算计算器
- 一个非常非常简单的计算器
- 一个简单的计算器模拟程序-2011腾讯笔试填空题
- 栈的应用——一个简单的整数计算器
- J2ME应用实例——一个简单的计算器实现(附源代码)
- Debian下用Glade开发一个简单的计算器
- shell实现一个简单的计算器功能小脚本
- Android studio简单做一个音乐界面
- PHP实现一个简单的计算器(在html页面和php文件之间传值)
- 一个简单的计算器程序
- 用Visual studio2005的单元测试框架实现一个简单的计算器
- 使用Node.js的express框架搭建一个简单项目并且添加了一个路由
- Html+JS创建一个简单的计算器
- 利用java做一个简单的计算器
- J2ME应用实例——一个简单的计算器实现(附源代码)
- C语言不用scanf函数的一个简单的计算器