Android Studio实现简易计算器

目的

根据书本例题的计算器界面,开发一个简单的计算器,并实现功能。

工具及环境

使用java语言,在Android studio平台上进行开发

功能设计

实现加减乘除的运算功能和清屏的功能

设计思路

首先设计一个可视化的界面,供用户输入数据并查看结果。
用户通过点击相应按钮输入正确的表达式,计算器进行相应的加减乘除运算,且可以进行小数和整数的运算

代码

selector_orange_bg.xml(点击按钮会有灰色阴影效果)

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

selector_white_bg.xml (点击按钮会有灰色阴影效果)

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

shape_rectangle_orange.xml (运算符功能键的背景颜色设置)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#e47126"/>
    <stroke
        android:width="1dp"
        android:color="#333333"/>

</shape>

shape_rectangle_orange_grey.xml (点击按钮之后的灰色阴影设置)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#b55f1c"/>
    <stroke
        android:width="1dp"
        android:color="#333333"/>

</shape>

shape_rectangle_white.xml 

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#ffffff"/>
    <stroke
        android:width="1dp"
        android:color="#333333"/>

</shape>

shape_rectangle_white_grey.xml 

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#999999"/>
    <stroke
        android:width="1dp"
        android:color="#333333"/>

</shape>

 activity_main.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"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/result"
        android:textColor="#000000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="65sp"
        android:enabled="false"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal">


        <Button
            android:id="@+id/cls"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="清除"
            android:background="@drawable/selector_white_bg"
            android:gravity="center"
            android:textSize="30sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal">


        <Button
            android:id="@+id/one"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="1"
            android:background="@drawable/selector_white_bg"
            android:gravity="center"
            android:textSize="30sp" />

        <Button
            android:id="@+id/two"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="2"
            android:background="@drawable/selector_white_bg"
            android:gravity="center"
            android:textSize="30sp" />

        <Button
            android:id="@+id/three"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="3"
            android:background="@drawable/selector_white_bg"
            android:gravity="center"
            android:textSize="30sp" />

        <Button
            android:id="@+id/plus"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="+"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_orange_bg"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal">


        <Button
            android:id="@+id/four"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="4"
            android:background="@drawable/selector_white_bg"
            android:gravity="center"
            android:textSize="30sp" />

        <Button
            android:id="@+id/five"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="5"
            android:background="@drawable/selector_white_bg"
            android:gravity="center"
            android:textSize="30sp" />

        <Button
            android:id="@+id/six"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="6"
            android:background="@drawable/selector_white_bg"
            android:gravity="center"
            android:textSize="30sp" />

        <Button
            android:id="@+id/min"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="-"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_orange_bg"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal">


        <Button
            android:id="@+id/seven"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="7"
            android:background="@drawable/selector_white_bg"
            android:gravity="center"
            android:textSize="30sp" />

        <Button
            android:id="@+id/eight"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="8"
            android:background="@drawable/selector_white_bg"
            android:gravity="center"
            android:textSize="30sp" />

        <Button
            android:id="@+id/nine"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="9"
            android:background="@drawable/selector_white_bg"
            android:gravity="center"
            android:textSize="30sp" />

        <Button
            android:id="@+id/mul"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="×"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_orange_bg"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal">


        <Button
            android:id="@+id/spot"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="."
            android:background="@drawable/selector_white_bg"
            android:gravity="center"
            android:textSize="30sp" />

        <Button
            android:id="@+id/zero"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="0"
            android:background="@drawable/selector_white_bg"
            android:gravity="center"
            android:textSize="30sp" />

        <Button
            android:id="@+id/equal"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="="
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_white_bg"/>

        <Button
            android:id="@+id/div"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="÷"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_orange_bg"/>
    </LinearLayout>

</LinearLayout>

 界面效果图:

 MainActivity.java

package com.xdw.calculator;

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对象   也就是activity_main.xml里所设置的ID
    Button zero,one,two,three,four,five,six,seven,eight,nine,spot;
    Button mul,div,plus,min;
    Button cls,equal;
    EditText result;
    boolean clr_flag;    //判断et编辑文本框中是否清空

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //实例化对象
        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);
        spot= (Button) findViewById(R.id.spot);
        plus= (Button) findViewById(R.id.plus);
        min= (Button) findViewById(R.id.min);
        mul= (Button) findViewById(R.id.mul);
        div= (Button) findViewById(R.id.div);
        cls= (Button) findViewById(R.id.cls);
        equal= (Button) findViewById(R.id.equal);
        result= (EditText) findViewById(R.id.result);

        //给按钮设置点击事件
        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);
        spot.setOnClickListener(this);
        plus.setOnClickListener(this);
        min.setOnClickListener(this);
        mul.setOnClickListener(this);
        div.setOnClickListener(this);
        cls.setOnClickListener(this);
        equal.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String str=result.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.spot:
                if(clr_flag){
                    clr_flag=false;
                    str="";
                    result.setText("");
                }
                result.setText(str+((Button)v).getText());
                break;
            case R.id.plus:
            case R.id.min:
            case R.id.mul:
            case R.id.div:
                if(clr_flag){
                    clr_flag=false;
                    str="";
                    result.setText("");
                }
                if(str.contains("+")||str.contains("-")||str.contains("×")||str.contains("÷")) {
                    str=str.substring(0,str.indexOf(" "));
                }
                result.setText(str+" "+((Button)v).getText()+" ");
                break;
            case R.id.cls:
                if(clr_flag)
                    clr_flag=false;
                str="";
                result.setText("");
                break;
            case R.id.equal: //单独运算最后结果
                getResult();//调用下面的方法
                break;
        }
    }

    private void getResult() {
        String exp=result.getText().toString();
        if(exp==null||exp.equals("")) return ;
        //因为没有运算符所以不用运算
        if(!exp.contains(" ")){
            return ;
        }
        if(clr_flag){
            clr_flag=false;
            return;
        }
        clr_flag=true;
        //截取运算符前面的字符串
        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(d2==0) cnt=0;
                else cnt=d1/d2;
            }
            if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")) {
                int res = (int) cnt;
                result.setText(res+"");
            }else {
                result.setText(cnt+"");}
        }
        //如果s1不是空    s2是空  就执行下一步
        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;
                result.setText(res+"");
            }else {
                result.setText(cnt+"");}
        }
        //如果s1是空    s2不是空  就执行下一步
        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;
                result.setText(res+"");
            }else {
                result.setText(cnt+"");}
        }
        else {
            result.setText("");
        }
    }
}




功能实现效果:

     

     

总结

     这次的计算器实验收获很多,在操作和学习的过程中,结合了书本,同学和网上视频的知识,一边操作一边加深着Android Studio的界面设置命令和效果,以及功能的实现加强了java语言的基础,是很好的一次学习实践。当然中间会有很多的问题,比如说减法不知道为什么实现不了运算,看了很多遍也找到究竟什么问题,慢慢改着改着突然就成功了。java语言的学习,还要继续加强,移动编程技术,也要好好的学,好好的练。没有分组,一个人,所以要更加的努力,提高自己的水平,跟上老师的进度,努力完成每一次的任务,只有自己真真正正的去努力了,才会有收获,才会成功。


 

猜你喜欢

转载自blog.csdn.net/MofukYXM/article/details/88618818