Android项目——不成熟的计算器

Android项目——不成熟的计算器

编译环境介绍

编译器为:Android studio 3.1.2;
SDK Lever 28,Revision 6;
java version 1.8.0_171;
Android studio 3.1.2

ui设计:

界面是根据老师画的原型图来设计的,所以在界面设计上,我使用了6行4列的网格布局,但是这样布局有个问题是在不同机型和横屏显示时各控件不能自动调整比例,影响了整体的感官。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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:rowCount="6"
    android:columnCount="4"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:layout_marginLeft="4px"
        android:gravity="left"
        android:text="0"
        android:textSize="50dip"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:id="@+id/clean"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:text="清除"
        android:textSize="26sp" />
    <Button android:text="1" android:textSize="26sp" android:id="@+id/btn_1"/>
    <Button android:text="2" android:textSize="26sp" android:id="@+id/btn_2"/>
    <Button android:text="3" android:textSize="26sp" android:id="@+id/btn_3"/>
    <Button android:text="+" android:textSize="26sp" android:id="@+id/btn_a"/>
    <Button android:text="4" android:textSize="26sp" android:id="@+id/btn_4"/>
    <Button android:text="5" android:textSize="26sp" android:id="@+id/btn_5"/>
    <Button android:text="6" android:textSize="26sp" android:id="@+id/btn_6"/>
    <Button android:text="-" android:textSize="26sp" android:id="@+id/btn_s"/>
    <Button android:text="7" android:textSize="26sp" android:id="@+id/btn_7"/>
    <Button android:text="8" android:textSize="26sp" android:id="@+id/btn_8"/>
    <Button android:text="9" android:textSize="26sp" android:id="@+id/btn_9"/>
    <Button android:text="*" android:textSize="26sp" android:id="@+id/btn_m"/>
    <Button android:text="." android:textSize="26sp" android:id="@+id/btn_p"/>
    <Button android:text="0" android:textSize="26sp" android:id="@+id/btn_0"/>
    <Button android:text="=" android:textSize="26sp" android:id="@+id/btn_e"/>
    <Button android:text="/" android:textSize="26sp" android:id="@+id/btn_d"/>
</GridLayout>

逻辑设计

在进行逻辑设计时,我对以下问题进行了思考:

屏幕输出的规则。

第一种方案是把所有的输入都一股脑输出出来,当按下等于号时再刷新屏幕输出结果;
第二种方案是只输出数字,当点击运算符后再次输入数字时会覆盖掉输入运算符前显示在屏幕上的数字;

计算的规则。

	第一种是把中缀表达式改成后缀表达式再计算,这种方法能够进行更复杂一些的运算。
	第二种是分别把数和运算符分别记录起来,当再一次输入运算符的时候再对前面的数和运算符进行运算并输出。

此外就是对小数点个数的检查和对除数的检查和处理。
以下是MainActivity.java文件的内容:

MainActivity.java

package com.example.ex2_9;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.*;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private TextView txt;
    private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,btnA,btnS,btnM,btnP,btnE,btnD,btnClean;
    String text="";
    Double num1 = new Double(0.0),num2 = new Double(0.0),num = new Double(0.0);
    final double zero=0;
    /*state:输入框的状态,0为覆盖,1为追加*/
    /*Calstate:存储上一个计算符的状态,0为无,1加2减3乘4除*/
    int state = 0,Calstate = 0;
    int point=0; //小数点个数
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = (TextView)findViewById(R.id.textView);
        b0 = (Button)findViewById(R.id.btn_0);
        b1 = (Button)findViewById(R.id.btn_1);
        b2 = (Button)findViewById(R.id.btn_2);
        b3 = (Button)findViewById(R.id.btn_3);
        b4 = (Button)findViewById(R.id.btn_4);
        b5 = (Button)findViewById(R.id.btn_5);
        b6 = (Button)findViewById(R.id.btn_6);
        b7 = (Button)findViewById(R.id.btn_7);
        b8 = (Button)findViewById(R.id.btn_8);
        b9 = (Button)findViewById(R.id.btn_9);
        btnA = (Button)findViewById(R.id.btn_a);
        btnS = (Button)findViewById(R.id.btn_s);
        btnM = (Button)findViewById(R.id.btn_m);
        btnP = (Button)findViewById(R.id.btn_p);
        btnE = (Button)findViewById(R.id.btn_e);
        btnD = (Button)findViewById(R.id.btn_d);
        btnClean  = (Button)findViewById(R.id.clean);
        btnClean.setOnClickListener(new click());
        b0.setOnClickListener(new click());
        b1.setOnClickListener(new click());
        b2.setOnClickListener(new click());
        b3.setOnClickListener(new click());
        b4.setOnClickListener(new click());
        b5.setOnClickListener(new click());
        b6.setOnClickListener(new click());
        b7.setOnClickListener(new click());
        b8.setOnClickListener(new click());
        b9.setOnClickListener(new click());
        btnA.setOnClickListener(new click());
        btnS.setOnClickListener(new click());
        btnM.setOnClickListener(new click());
        btnP.setOnClickListener(new click());
        btnE.setOnClickListener(new click());
        btnD.setOnClickListener(new click());
    }
        /*--判断使用加减乘除那种方法并计算,最后返回布尔类型参数--*/
    private boolean calculation(){
        if (Calstate==1)/*加法运算*/{
            num2 = Double.valueOf(text);
            num1 = num1 + num2;
            Calstate = 0;
            return true;
        }else if (Calstate==2)/*减法运算*/{
            num2 = Double.valueOf(text);
            num1 = num1 - num2;
            Calstate = 0;
            return true;
        }else if (Calstate==3)/*乘法运算*/{
            num2 = Double.valueOf(text);
            num1 = num1 * num2;
            Calstate = 0;
            return true;
        }else if (Calstate==4)/*除法运算*/{
            num2 = Double.valueOf(text);
            if (num2.doubleValue()==0){
                text = "错误!";
                state = 0;
                Calstate = 0;
                return false;
            }else{
                num1 = num1 / num2;
                Calstate = 0;
                return true;
            }
        }else {
            state = 0;
            Calstate = 0;
            text = "Calstate参数错误!";
            return false;
        }
    }
    class click implements View.OnClickListener{
        public void onClick(View v){
            //0和小数点
            if(v==b0){
                if(state==0){
                    text = "0";
                    state = 0;
                }else if (state==1){
                    text = text+"0";
                }
                txt.setText(text);
            }
            if(v==btnP){
                if(state==0){
                    text = "0.";
                    state = 1;
                }else if (state==1){
                    if (text.indexOf(".")==-1)
                        text = text+".";
                }
                txt.setText(text);
            }
            //数字键
            if(v==b1){
                if(state==0){
                    text = "1";
                    state = 1;
                }else if (state==1){
                    text = text+"1";
                }
                txt.setText(text);
            }
            if(v==b2){
                if(state==0){
                    text = "2";
                    state = 1;
                }else if (state==1){
                    text = text+"2";
                }
                txt.setText(text);
            }
            if(v==b3){
                if(state==0){
                    text = "3";
                    state = 1;
                }else if (state==1){
                    text = text+"3";
                }
                txt.setText(text);
            }
            if(v==b4){
                if(state==0){
                    text = "4";
                    state = 1;
                }else if (state==1){
                    text = text+"4";
                }
                txt.setText(text);
            }
            if(v==b5){
                if(state==0){
                    text = "5";
                    state = 1;
                }else if (state==1){
                    text = text+"5";
                }
                txt.setText(text);
            }
            if(v==b6){
                if(state==0){
                    text = "6";
                    state = 1;
                }else if (state==1){
                    text = text+"6";
                }
                txt.setText(text);
            }
            if(v==b7){
                if(state==0){
                    text = "7";
                    state = 1;
                }else if (state==1){
                    text = text+"7";
                }
                txt.setText(text);
            }
            if(v==b8){
                if(state==0){
                    text = "8";
                    state = 1;
                }else if (state==1){
                    text = text+"8";
                }
                txt.setText(text);
            }
            if(v==b9) {
                if(state==0){
                    text = "9";
                    state = 1;
                }else if (state==1){
                    text = text+"9";
                }
                txt.setText(text);
            }
            /*-----------清除-------------*/
            if(v==btnClean){
                state = 0;
                text = "0";
                txt.setText(text);
            }
            /*----------------加减乘除-----------------*/
            if(v==btnA){
                if(Calstate==0){
                    num1 = Double.valueOf(text);
                    Calstate = 1;
                    text = num1.toString();
                    state = 0;
                }else{
                    if(calculation()){
                        Calstate = 1;
                        text = num1.toString();
                        state = 0;
                    }
                }
                txt.setText(text);
            }
            if(v==btnS){
                if(Calstate==0){
                    num1 = Double.valueOf(text);
                    Calstate = 2;
                    text = num1.toString();
                    state = 0;
                }else{
                    if(calculation()){
                        Calstate = 2;
                        text = num1.toString();
                        state = 0;
                    }
                }
                txt.setText(text);
            }
            if(v==btnM){
                if(Calstate==0){
                    num1 = Double.valueOf(text);
                    Calstate = 3;
                    text = num1.toString();
                    state = 0;
                }else{
                    if(calculation()){
                        Calstate = 3;
                        text = num1.toString();
                        state = 0;
                    }
                }
                txt.setText(text);
            }
            if(v==btnD){
                if(Calstate==0){
                    num1 = Double.valueOf(text);
                    Calstate = 4;
                    text = num1.toString();
                    state = 0;
                }else{
                    if(calculation()){
                        Calstate = 4;
                        text = num1.toString();
                        state = 0;
                    }
                }
                txt.setText(text);
            }
            /*---------等于----------*/
            if(v==btnE){
                if(Calstate==0){
                    num = Double.valueOf(text);
                    text = num.toString();
                    num1 = zero;
                    num2 = zero;
                    Calstate = 0;
                    state = 0;
                }else{
                    if(calculation()){
                        num = num1;
                        text = num.toString();
                        num1 = zero;
                        num2 = zero;
                        Calstate = 0;
                        state = 0;
                    }
                }
                txt.setText(text);
            }
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ava实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),可运行高分资源 Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值