基于安卓5.0 material design仿ios界面简单计算器
来源:互联网 发布:什么网游有mac版 编辑:程序博客网 时间:2024/06/05 22:07
本人的第一篇博客,写了个简单的计算器,开学的第一个作业,适合初学者打发下时间,高手可以飘过,如有错漏之处,还望指出,定当改之
顺手贴上github 仓库地址:https://github.com/liheming/calculator
MainActivity.java如下
package com.example.haily.calculator;import android.content.DialogInterface;import android.os.Bundle;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.TextView;import java.text.DecimalFormat;/**author haily* date :2016.3.26* sample calculator 1.0* contact qq:1325789491*/public class MainActivity extends AppCompatActivity implements View.OnClickListener {private String result,oprating="";//真正的结果, //获取对应的操作符int op1length = 0;//第一个操作数的长度,为了定位第二个操作数的起止位置private boolean isOperate = false;//是否点击过操作符private double op1 = 0, op2 = 0, preRes = 0;//定义操作数1,操作数2,double类型结果private TextView text_show, text_result;//表达式和结果显示textView;private Button one, two, three, four, five, six, seven, eight, nine, zero, add, min, mul, div, delete, dot, equ;//所有按钮@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);init();//调用初始化函数init()}//初始化各组件private void init() {text_show = (TextView) findViewById(R.id.text_show);text_result = (TextView) findViewById(R.id.text_result);text_show.setText("");text_result.setText("");one = (Button) this.findViewById(R.id.one);two = (Button) this.findViewById(R.id.two);three = (Button) this.findViewById(R.id.three);four = (Button) this.findViewById(R.id.four);five = (Button) this.findViewById(R.id.five);six = (Button) this.findViewById(R.id.six);seven = (Button) this.findViewById(R.id.seven);eight = (Button) this.findViewById(R.id.eight);nine = (Button) this.findViewById(R.id.nine);zero = (Button) this.findViewById(R.id.zero);add = (Button) this.findViewById(R.id.add);min = (Button) this.findViewById(R.id.min);mul = (Button) this.findViewById(R.id.mul);div = (Button) this.findViewById(R.id.div);delete = (Button) this.findViewById(R.id.delete);dot = (Button) this.findViewById(R.id.dot);equ = (Button) this.findViewById(R.id.equ);//实现按钮监听事件one.setOnClickListener(this);two.setOnClickListener(this);three.setOnClickListener(this);four.setOnClickListener(this);five.setOnClickListener(this);six.setOnClickListener(this);seven.setOnClickListener(this);eight.setOnClickListener(this);nine.setOnClickListener(this);zero.setOnClickListener(this);dot.setOnClickListener(this);add.setOnClickListener(this);min.setOnClickListener(this);mul.setOnClickListener(this);div.setOnClickListener(this);equ.setOnClickListener(this);delete.setOnClickListener(this);delete.setOnLongClickListener(new View.OnLongClickListener() {//长按删除监听事件@Overridepublic boolean onLongClick(View v) {text_show.setText("");text_result.setText("");return true;}});}private void clickNuber(String s) {text_show.append(s);if (isOperate) {equ();}}@Overridepublic void onClick(View v) {dot.setEnabled(true);switch (v.getId()) {case R.id.one:clickNuber("1");break;case R.id.two:clickNuber("2");break;case R.id.three:clickNuber("3");break;case R.id.four:clickNuber("4");break;case R.id.five:clickNuber("5");break;case R.id.six:clickNuber("6");break;case R.id.seven:clickNuber("7");break;case R.id.eight:clickNuber("8");break;case R.id.nine:clickNuber("9");break;case R.id.zero:clickNuber("0");break;case R.id.dot:if (!text_show.getText().toString().equals("")) {for (int i = 0; i < text_show.getText().length(); i++) {if (text_show.getText().charAt(i) == '.') {System.out.println(text_show.getText().charAt(i) == '.');// dot.setEnabled(false);return;}}text_show.append(".");}break;case R.id.add://加法compute("+");break;case R.id.min://减法compute("-");break;case R.id.mul://乘法compute("×");break;case R.id.div://除法compute("÷");break;case R.id.equ:// 计算结果if (isOperate) {equ();isOperate = false;text_show.setText(result);text_result.setText("");}break;case R.id.delete://后退和删除if (!text_show.getText().toString().equals("")) {text_show.setText(text_show.getText().subSequence(0, text_show.getText().length() - 1));try {if (isOperate) {equ();}} catch (Exception e) {text_result.setText("");}isOperate = false;} else {isOperate = false;text_show.setText("");}break;default:}}private void compute(String oprate) {if (!text_show.getText().toString().equals("")&&!isOperate) {op1length = text_show.getText().length();try {op1 = Double.parseDouble(text_show.getText().toString());text_show.append(oprate);oprating = oprate;isOperate = true;} catch (Exception e) {Log.e("TAG", "第二次按操作符");}// System.out.println(op1);//// text_show.setText("");}}private void equ() {if (!text_show.getText().toString().equals("")) {System.out.println("error" + text_show.getText().toString().equals(""));int op2Length = text_show.getText().length();// System.out.println("op1Length"+op1length+"----op2Length"+op2Length);String str = text_show.getText().subSequence(op1length + 1, op2Length).toString();op2 = Double.parseDouble(str);System.out.println(op2);preRes = op2;switch (oprating) {case "+":preRes = add(op1, op2);break;case "-":preRes = min(op1, op2);break;case "×":preRes = mul(op1, op2);break;case "÷":preRes = div(op1, op2);break;}DecimalFormat df = new DecimalFormat("#0.0");result = df.format(preRes);text_result.setText(result);}}//以下4个方法为四则运算public double add(double op1, double op2) {return op1 + op2;}public double min(double op1, double op2) {return op1 - op2;}public double mul(double op1, double op2) {return op1 * op2;}public double div(double op1, double op2) {return op1 / op2;}//以下为菜单初始化@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.menu_main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {int id = item.getItemId();if (id == R.id.about) {AlertDialog builder = new AlertDialog.Builder(this).setTitle("关于").setMessage("简易计算器1.0\n小数点后保留1位小数\n长按删除所有数字\nmade by haily on 2016.3.26").setNegativeButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// finish();}}).show();return true;}return super.onOptionsItemSelected(item);}}
layout资源文件如下
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.haily.calculator.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="150dp" android:orientation="vertical" android:background="#f9f5f5"> <TextView android:paddingTop="18dp" android:id="@+id/text_show" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#141313" android:layout_weight="1" android:gravity="right" android:inputType="text|textNoSuggestions" android:text="464646646546" android:textColor="#eeeeee" android:textSize="35sp" /> <TextView android:paddingTop="18dp" android:id="@+id/text_result" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#141313" android:gravity="right" android:inputType="text|textNoSuggestions" android:text="464646646546" android:textColor="#eeeeee" android:textSize="35sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:weightSum="1"> <TableLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="0.81" android:background="#dce3e9"> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/seven" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#00000000" android:text="7" android:textColor="#262626" android:textSize="25sp" /> <Button android:id="@+id/eight" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#00000000" android:text="8" android:textColor="#262626" android:textSize="25sp" /> <Button android:id="@+id/nine" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#00000000" android:text="9" android:textColor="#262626" android:textSize="25sp" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> <Button android:id="@+id/four" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#00000000" android:text="4" android:textColor="#262626" android:textSize="25sp" /> <Button android:id="@+id/five" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#00000000" android:text="5" android:textColor="#262626" android:textSize="25sp" /> <Button android:id="@+id/six" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#00000000" android:text="6" android:textColor="#262626" android:textSize="25sp" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> <Button android:id="@+id/one" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#00000000" android:text="1" android:textColor="#262626" android:textSize="25sp" /> <Button android:id="@+id/two" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#00000000" android:text="2" android:textColor="#262626" android:textSize="25sp" /> <Button android:id="@+id/three" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#00000000" android:text="3" android:textColor="#262626" android:textSize="25sp" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> <Button android:id="@+id/dot" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#00000000" android:cursorVisible="true" android:text="." android:textColor="#262626" android:textSize="25sp" /> <Button android:id="@+id/zero" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#00000000" android:text="0" android:textColor="#262626" android:textSize="25sp" /> <Button android:id="@+id/equ" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#00000000" android:text="=" android:textColor="#262626" android:textSize="25sp" /> </TableRow> </TableLayout> <LinearLayout android:layout_width="89dp" android:layout_height="match_parent" android:layout_weight="0.40" android:background="#f7b93c" android:orientation="vertical"> <Button android:id="@+id/delete" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#00000000" android:text="删除" android:textColor="#262626" android:textSize="25sp" /> <Button android:id="@+id/div" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#00000000" android:text="÷" android:textColor="#262626" android:textSize="25sp" /> <Button android:id="@+id/mul" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#00000000" android:text="×" android:textColor="#262626" android:textSize="25sp" /> <Button android:id="@+id/min" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#00000000" android:text="-" android:textColor="#262626" android:textSize="25sp" /> <Button android:id="@+id/add" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#00000000" android:text="+" android:textColor="#262626" android:textSize="25sp" /> </LinearLayout> </LinearLayout></LinearLayout>
0 0
- 基于安卓5.0 material design仿ios界面简单计算器
- 安卓 Material Design
- Android View 仿iOS SwitchButton [Material Design]
- Android View 仿iOS SwitchButton [Material Design]
- Android View 仿iOS SwitchButton [Material Design]
- Android View 仿iOS SwitchButton [Material Design]
- [Android]我的安卓5.0+设计开发规范(基于Material Design)
- iOS模仿安卓Material Design的涟漪动画按钮
- 安卓5.0 Material Design设计简单的一个使用特效
- 安卓material design 主题调色介绍
- 安卓布局计算器界面
- Material Design简单实践
- Material Design 简单实例
- 安卓简单计算器整理
- 安卓简单加法计算器
- 安卓 Material Design规范 底部导航框架介绍
- 使用Material design仿JD顶部搜索
- Android Material Design 简单控件
- 如何解决eclipse中ADT22升级到ADT23产生冲突的问题
- IS LCS n^2和nlogn解法 以及LCIS
- 队列在Android中是使用
- iOS开发 ☞ 常用终端命令
- 设计模式 之 单例模式 (C++ 懒汉经典实现 & DCL实现 & 饿汉经典实现)
- 基于安卓5.0 material design仿ios界面简单计算器
- 数组1
- Spark中的矩阵乘法源码分析
- Java获取当前进程ID以及所有Java进程的进程ID
- 牛客网 算法第一题 二叉树按层次打印问题
- document.body 和 document.documentElement 的区别
- NetLoader开发文档一类轻松解决网络请求
- 大数的乘法
- HDU 5560 5561 5562 5563 (Bestcoder#77 div2 A B C D)