新手初学安卓一周,勿喷。

在看完Activity之后决定做一个计算器。根据网上的资料和慕课网计算器的教材,仿照着做了一个iPone计算器。

先看看效果图

好了,直接给源码,XML用的是LinearLayout, 试过用TableLayout,发现总是会有间距,做出来不像。

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"><EditText
        android:gravity="right|bottom"
        android:id="@+id/output"
        android:textSize="60dp"
        android:background="#000000"
        android:textColor="#ffffff"
        android:editable="false"
        android:layout_width="match_parent"
        android:layout_height="100dp" /><LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"><LinearLayout
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            ><Button
                android:id="@+id/clear"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="0.1dp"
                android:background="@drawable/grey_selector"
                android:text="AC" /><Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="0.1dp"
                android:background="@drawable/grey_selector"
                android:text="+/-" /><Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="0.1dp"
                android:background="@drawable/grey_selector"
                android:text="%" /><Button
                android:id="@+id/div"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="0.1dp"
                android:background="@drawable/orange_seletor"
                android:text="÷" /></LinearLayout><LinearLayout
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" ><Button
                android:id="@+id/seven"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="0.1dp"
                android:layout_weight="1"
                android:background="@drawable/grey_selector"
                android:text="7" /><Button
                android:id="@+id/eight"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="0.1dp"
                android:layout_weight="1"
                android:background="@drawable/grey_selector"
                android:text="8" /><Button
                android:id="@+id/nine"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="0.1dp"
                android:layout_weight="1"
                android:background="@drawable/grey_selector"
                android:text="9" /><Button
                android:id="@+id/mul"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="0.1dp"
                android:layout_weight="1"
                android:background="@drawable/orange_seletor"
                android:text="×" /></LinearLayout><LinearLayout
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" ><Button
                android:id="@+id/four"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="0.1dp"
                android:layout_weight="1"
                android:background="@drawable/grey_selector"
                android:text="4" /><Button
                android:id="@+id/five"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="0.1dp"
                android:layout_weight="1"
                android:background="@drawable/grey_selector"
                android:text="5" /><Button
                android:id="@+id/six"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="0.1dp"
                android:layout_weight="1"
                android:background="@drawable/grey_selector"
                android:text="6" /><Button
                android:id="@+id/sub"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="0.1dp"
                android:layout_weight="1"
                android:background="@drawable/orange_seletor"
                android:text="-" /></LinearLayout><LinearLayout
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" ><Button
                android:id="@+id/one"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="0.1dp"
                android:layout_weight="1"
                android:background="@drawable/grey_selector"
                android:text="1" /><Button
                android:id="@+id/two"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="0.1dp"
                android:layout_weight="1"
                android:background="@drawable/grey_selector"
                android:text="2" /><Button
                android:id="@+id/three"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="0.1dp"
                android:layout_weight="1"
                android:background="@drawable/grey_selector"
                android:text="3" /><Button
                android:id="@+id/add"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="0.1dp"
                android:layout_weight="1"
                android:background="@drawable/orange_seletor"
                android:text="+" /></LinearLayout><LinearLayout
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" ><Button
                android:id="@+id/zero"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/grey_selector"
                android:text="0" /><Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/grey_selector" /><Button
                android:id="@+id/dot"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="0.1dp"
                android:layout_weight="1"
                android:background="@drawable/grey_selector"
                android:text="." /><Button
                android:id="@+id/equal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="0.1dp"
                android:layout_weight="1"
                android:background="@drawable/orange_seletor"
                android:text="=" /></LinearLayout></LinearLayout>
</LinearLayout>

下面是Java的源代码,其思想是慕课网加上自己的一些修改。

package com.example.administrator.calculatoriphone;import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;public class MainActivity extends ActionBarActivity implements View.OnClickListener{/* 数字按钮 */
    private Button zero;private Button one;private Button two;private Button three;private Button four;private Button five;private Button six;private Button seven;private Button eight;private Button nine;//加减乘除
    private Button add;private Button sub;private Button mul;private Button div;//等号,清楚
    private Button dot;private Button equal;private Button clear;private EditText output;String op;int needclear;@Override
    protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);ActionBar actionBar = getSupportActionBar();actionBar.hide();setContentView(R.layout.activity_main);//初始化
        zero = (Button)findViewById(R.id.zero);one = (Button)findViewById(R.id.one);two = (Button)findViewById(R.id.two);three = (Button)findViewById(R.id.three);four = (Button)findViewById(R.id.four);five = (Button)findViewById(R.id.five);six = (Button)findViewById(R.id.six);seven = (Button)findViewById(R.id.seven);eight = (Button)findViewById(R.id.eight);nine = (Button)findViewById(R.id.nine);add = (Button)findViewById(R.id.add);sub = (Button)findViewById(R.id.sub);mul = (Button)findViewById(R.id.mul);div = (Button)findViewById(R.id.div);dot = (Button)findViewById(R.id.dot);equal = (Button)findViewById(R.id.equal);clear = (Button)findViewById(R.id.clear);output = (EditText)findViewById(R.id.output);//设置监听器
        zero.setOnClickListener(this);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);add.setOnClickListener(this);sub.setOnClickListener(this);mul.setOnClickListener(this);div.setOnClickListener(this);dot.setOnClickListener(this);equal.setOnClickListener(this);clear.setOnClickListener(this);}@Override
    public boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);return true;}@Override
    public boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();//noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}@Override
    public void onClick(View v) {String str = output.getText().toString();switch (v.getId()) {case R.id.zero:case R.id.one:case R.id.two:case R.id.three:case R.id.four:case R.id.five:case R.id.six:case R.id.seven:case R.id.eight:case R.id.nine:case R.id.dot:if(needclear == 1){output.setText(((Button) v).getText());needclear = 0;}else {output.setText(str + ((Button) v).getText());}break;case R.id.add:op = "add";output.setText(str + " " + ((Button) v).getText() + " ");break;case R.id.sub:op = "sub";output.setText(str + " " + ((Button)v).getText() + " ");break;case R.id.mul:op = "mul";output.setText(str + " " + ((Button)v).getText() + " ");break;case R.id.div:op = "div";output.setText(str + " " + ((Button)v).getText() + " ");break;case R.id.equal:getResult();break;case R.id.clear:output.setText("");default:break;}}private void getResult() {String exp = output.getText().toString();int space = exp.indexOf(' ');//以空格为标志,截取运算符号前后的数字
        String s1 = exp.substring(0, space);String s2 = exp.substring(space + 3);double arg1 = Double.parseDouble(s1);double arg2 = Double.parseDouble(s2);double result = 0;if (op.equals("add")){result = arg1 + arg2;}else if(op.equals("sub")){result = arg1 - arg2;}else if (op.equals("mul")) {result = arg1 * arg2;}else if (op.equals("div")) {result = arg1 / arg2;}else {return;}output.setText(result+"");needclear = 1;}
}

另外实现点击之后按钮颜色转换是在drawable下面写了资源文件

deep_grey.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android ="http://schemas.android.com/apk/res/android"><solid android:color="#c6c7c9" />
</shape>

grey_seletor.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/deep_grey" android:state_pressed="true"/><item android:drawable="@drawable/light_grey"/>
</selector>

当然只是实现普通的加减乘除,负号,百分号等其他按钮没有实现,并且里面还是会出现一些或多或少的bug,例如双击等号,会出现错误等。可见一个真正发行的计算器的设计和制作也是相当繁琐的。

原创博客,转载注明出处。参考了http://wenku.baidu.com/view/1284de8ca8956bec0875e349,以及慕课网的安卓入门教学视频。

自制安卓版iPhone计算器相关推荐

  1. 雷军把小米发布会开到华为门口,发布了安卓版iPhone X

    郭一璞 假装发自 深圳  量子位 报道 | 公众号 QbitAI 刚刚,小米在华为家门口深圳,发布了一款最像iPhone X的安卓旗舰机. 这是小米史上人数最多的发布会.新品最多的发布会,并且可能是 ...

  2. Swift项目开发实战-基于分层架构的多版本iPhone计算器-直播公开课

    Swift项目开发实战-基于分层架构的多版本iPhone计算器-直播公开课 本课程采用Q Q群直播方式进行直播,价值99元视频课程免费直播.完整的基于Swift项目实战,手把手教你做一个Swift版i ...

  3. 安卓bmi项目_BMI计算器安卓版下载

    BMI计算器安卓版下载软件介绍: BMI计算器app官方下载!此应用程序是有用的BMI(身体质量指数)计算器.BMI计算器为你的BMI进行计算,看你是否在提供上面是否标准,用国际性的指标来为你进行确定 ...

  4. android 日倒计时计算器,分秒倒数日app下载-分秒倒数日(时间计算器)安卓版下载 v2.0.0安卓版_5577安卓网...

    分秒倒数日安卓版app下载分享给大家,这是一款非常好用的时间管理神奇,用户可以使用这款软件记录重要的时间,可以设置与恋人相恋的时间,也可以设置高考的倒数日,非常的方便,感兴趣的用户快来本站下载吧. [ ...

  5. android角度计算器,Calckit高级版一款安卓多功能计算器,内置高度定制的科学计算器...

    前言 手机中的计算器功能你多久用一次呢,我相信对于多数人来说计算器是一个低频使用软件. 其原因可能是用不上计算功能也很可能是单一的计算功能并不能满足你对于计算的需求. 那么如果现在有一款可以完成中学到 ...

  6. 安卓版炉石修改服务器,越南玩家自制炉石传说安卓版客户端 可能公开移植方法...

    炉石传说安卓版何时才能上线?相信这个一定是广大安卓炉友 关心的话题了.对此,暴雪早在今年年初就已提到:除了已经上架的iPad版外,对应安卓系统的炉石客户端也在紧密制作中,预计将会在年底上线. 凭什么苹 ...

  7. python安卓程序,python软件安卓版

    大家好,小编来为大家解答以下问题,python3安卓版下载,python软件安卓版,现在让我们一起来看看吧! 1.如何用手机编程Python? 1.QPython3:这是一个在上运行python3的脚 ...

  8. android 人生日历,android版人生日历日子怎么用 安卓版人生日历日子使用教程

    人生日历android版新发3.3.05.10版本,新增日子功能,那么android版人生日历日子怎么用呢?今天小编就为大家分享安卓版人生日历日子使用教程,一起来看看吧! 人生日历的日子,设计成四叶草 ...

  9. 震惊!微软小冰公众号被封?示好国内,苹果拟推出特色版iPhone;谷歌更新Google Play支持名单:华为荣耀多款在列……...

    关注并标星星CSDN云计算 极客头条:速递.最新.绝对有料.这里有企业新动.这里有业界要闻,打起十二分精神,紧跟fashion你可以的! 每周三次,打卡即read 更快.更全了解泛云圈精彩news g ...

最新文章

  1. 自己动手,丰衣足食:从零开始写个 IDEA 插件,要啥功能就做啥!
  2. 用sql统计vintage,滚动率,迁移率,逾期率
  3. 程序员的创业陷阱:接私活
  4. 查看jvm 默认的垃圾回收器
  5. mysql into_MYSQL中replace into的用法
  6. 企业实战(Jenkins+GitLab+SonarQube)_12_Jenkins+soanr服务器搭建和代码检查
  7. python3的soker模块实现功能
  8. 一个Repeater排序用的控件
  9. TCPClient例子(3)基于委托和事件的TcpHelper程序
  10. 交易系统开发(七)——交易延迟分析
  11. 机器人仿真软件 V-REP学习记录(序言)
  12. 一文读懂单点登录系统对企业的价值
  13. 深入理解JVM---JVM垃圾回收机制
  14. Spring3第一天
  15. 计算机4级英语词汇,四级词汇,2020英语四级必背词汇?
  16. Intel处理器原子操作支持
  17. php程序root权限,root权限是什么
  18. 飞塔防火墙常用命令集合
  19. linux pdf 宋宝华,51CTO博客-专业IT技术博客创作平台-技术成就梦想
  20. activemq保证消息顺序原理

热门文章

  1. 记一次 Centos7 安装 GitLab CE v12.6.1
  2. Oracle-查询重复值
  3. java开发 mvc框架
  4. 中国大学程序设计入门c语言答案,中国大学MOOC课程《程序设计入门——C语言》 第8周编程练习...
  5. smack android 示例代码,Smack-Android客户端入门一
  6. 【NOIP模拟】Competing Souls
  7. 从计算机向外接硬盘传输照片,请各位老师指点:今天在往移动硬盘传输数据(修改的照片),最后剩1? 爱问知识人...
  8. java计算机毕业设计智能道路交通管理系统源代码+系统+数据库+lw文档
  9. 从指定文件夹下很多个excel中读取指定内容,并合并成一个表,再增加字段
  10. lopatkin俄大神精简中文系统Windows 10 Enterprise 18362.116 19H1 Release x86-x64 ZH-CN PIP