前言:开始学习安卓,费了好大劲,才完成今天的作业。

计算器的界面:

简单计算器实现

在UI那块采用的是网格布局,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:rowCount="6"
    android:columnCount="4"> 设置计算器网格为 6行 4列
 <TextView
     android:id="@+id/textview1"
     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"
     />
 <Button
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_columnSpan="4"
     android:text="清除"
     android:textSize="26sp"
     android:id="@+id/btn_clear"
     android:layout_weight="1"
     android:background="#ffffff"/>
 <Button android:text="1" android:textSize="26sp"
     android:id="@+id/btn_1"
     android:layout_weight="1"
     android:background="#FFFFCC"/>
 <Button android:text="2" android:textSize="26sp"
     android:id="@+id/btn_2"
     android:layout_weight="1"
     android:background="#FFFFCC"/>
 <Button android:text="3" android:textSize="26sp" android:id="@+id/btn_3"
     android:layout_weight="1"
     android:background="#FFFFCC"
     />
 <Button android:text="+" android:textSize="26sp"
          android:id="@+id/btn_plus"
          android:layout_weight="1"
          android:background="#ffffff"/>
 <Button android:text="4" android:textSize="26sp"
          android:id="@+id/btn_4"
         android:layout_weight="1"
         android:background="#ffffcc"/>
 <Button android:text="5" android:textSize="26sp"
     android:id="@+id/btn_5"
     android:layout_weight="1"
     android:background="#ffffcc"/>
 <Button android:text="6" android:textSize="26sp"
     android:id="@+id/btn_6"
     android:layout_weight="1"
     android:background="#ffffcc"/>
 <Button android:text="-" android:textSize="26sp"
     android:id="@+id/btn_minus"
     android:layout_weight="1"
     android:background="#ffffff"/>
 <Button android:text="7" android:textSize="26sp"
          android:id="@+id/btn_7"
          android:layout_weight="1"
          android:background="#ffffcc"/>
 <Button android:text="8" android:textSize="26sp"
          android:id="@+id/btn_8"
          android:layout_weight="1"
          android:background="#ffffcc"/>
 <Button android:text="9" android:textSize="26sp"
          android:id="@+id/btn_9"
          android:layout_weight="1"
          android:background="#ffffcc"/>
 <Button android:text="*" android:textSize="26sp"
          android:id="@+id/btn_multiply"
          android:layout_weight="1"
          android:background="#ffffff"/>
<Button android:text="."  android:textSize="26sp"
 android:id="@+id/btn_point"
 android:layout_weight="1"
 android:background="#FFFFFF"/>

 <Button android:text="0" android:textSize="26sp"
     android:id="@+id/btn_0"
     android:background="#FFFFCC"
     android:layout_weight="1"/>
 <Button android:text="=" android:textSize="26sp"
     android:id="@+id/btn_equal"
     android:background="#99CCFF"
     android:layout_weight="1"/>
 <Button android:text="/" android:textSize="26sp"
          android:id="@+id/btn_divide"
          android:layout_weight="1"
          android:background="#ffffff" />
</GridLayout>

逻辑代码

1.在主体部分 
思路:就是 按照按钮点击事件的套路,每当点击按钮时,就会执行onClick()的方法,我们在这个方法里加入个switch循环,每个case:是按钮的id,我们申请个private StringBuilder的字符串。每按一个按钮,只要满足要求,不是错误的输入,就调用append()方法。
下面是主体代码

package com.example.myapplication;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Button;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button btn_0;
    Button btn_1;
    Button btn_2;
    Button btn_3;
    Button btn_4;
    Button btn_5;
    Button btn_6;
    Button btn_7;
    Button btn_8;
    Button btn_9;
    Button btn_point; //小数点
    Button btn_clear; //清除
    Button btn_del;   //删除
    Button btn_plus;
    Button btn_minus;
    Button btn_multiply;
    Button btn_divide;
    Button btn_equal;
    Button btn_left;
    Button btn_right;
    private TextView et_input;
    private StringBuilder pending = new StringBuilder();

    private void initView() {

        btn_0 = (Button) findViewById(R.id.btn_0);
        btn_1 = (Button) findViewById(R.id.btn_1);
        btn_2 = (Button) findViewById(R.id.btn_2);
        btn_3 = (Button) findViewById(R.id.btn_3);
        btn_4 = (Button) findViewById(R.id.btn_4);
        btn_5 = (Button) findViewById(R.id.btn_5);
        btn_6 = (Button) findViewById(R.id.btn_6);
        btn_7 = (Button) findViewById(R.id.btn_7);
        btn_8 = (Button) findViewById(R.id.btn_8);
        btn_9 = (Button) findViewById(R.id.btn_9);
        btn_point = (Button) findViewById(R.id.btn_point);
        btn_clear = (Button) findViewById(R.id.btn_clear);
        btn_plus = (Button) findViewById(R.id.btn_plus);
        btn_minus = (Button) findViewById(R.id.btn_minus);
        btn_multiply = (Button) findViewById(R.id.btn_multiply);
        btn_divide = (Button) findViewById(R.id.btn_divide);
        btn_equal = (Button) findViewById(R.id.btn_equal);
        et_input=findViewById(R.id.textview1);

        btn_0.setOnClickListener(this);
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_4.setOnClickListener(this);
        btn_5.setOnClickListener(this);
        btn_6.setOnClickListener(this);
        btn_7.setOnClickListener(this);
        btn_8.setOnClickListener(this);
        btn_9.setOnClickListener(this);
        btn_point.setOnClickListener(this);
        btn_plus.setOnClickListener(this);
        btn_equal.setOnClickListener(this);
        btn_minus.setOnClickListener(this);
        btn_multiply.setOnClickListener(this);
        btn_divide.setOnClickListener(this);
        btn_clear.setOnClickListener(this);
        btn_divide.setOnClickListener(this);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }


    public void onClick(View v) {
        int last = 0;
        if(pending.length()!=0)
        {
            last = pending.codePointAt(pending.length()-1);

        }
        switch (v.getId()) {
            case R.id.btn_0:
                pending = pending.append("0");
                et_input.setText(pending);
                break;
            case R.id.btn_1:
                pending = pending.append("1");
                et_input.setText(pending);
                break;
            case R.id.btn_2:
                pending = pending.append("2");
                et_input.setText(pending);
                break;
            case R.id.btn_3:
                pending = pending.append("3");
                et_input.setText(pending);
                break;
            case R.id.btn_4:
                pending = pending.append("4");
                et_input.setText(pending);
                break;
            case R.id.btn_5:
                pending = pending.append("5");
                et_input.setText(pending.toString());
                break;
            case R.id.btn_6:
                pending = pending.append("6");
                et_input.setText(pending);
                break;
            case R.id.btn_7:
                pending = pending.append("7");
                et_input.setText(pending);
                break;
            case R.id.btn_8:
                pending = pending.append("8");
                et_input.setText(pending);
                break;
            case R.id.btn_9:
                pending = pending.append("9");
                et_input.setText(pending);
                break;
            case R.id.btn_plus:
                //if (last >= '0' && last <= '9' ) {
                pending = pending.append("+");
                // }
                et_input.setText(pending);
                break;
            case R.id.btn_minus:
                //if (last >= '0' && last <= '9') {
                pending = pending.append("-");
                //  }
                et_input.setText(pending);
                break;
            case R.id.btn_multiply:
                // if (last >= '0' && last <= '9' ) {
                pending = pending.append("*");
                // }
                et_input.setText(pending);
                break;
            case R.id.btn_divide:
                // if (last >= '0' && last <= '9' ) {
                pending = pending.append("/");
                // }
                et_input.setText(pending);
                break;
            case R.id.btn_point:
                if (judje1()) {
                    pending = pending.append(".");
                    et_input.setText(pending);
                }
                break;

            case R.id.btn_clear: //清空
                pending = pending.delete(0, pending.length());
                et_input.setText(pending);
                break;
            case R.id.btn_equal: // =等于
                if ((pending.length() > 1)) {
                    InfixInToDuffix inf = new InfixInToDuffix();
                    String jieguo;
                    try {
                        String a = inf.toSuffix(pending);
                        jieguo = inf.dealEquation(a);

                    } catch (Exception ex) {
                        jieguo = "出错";
                    }
                    et_input.setText(pending + "=" + jieguo);
                    pending = pending.delete(0, pending.length());
                    if (Character.isDigit(jieguo.charAt(0))) {
                        pending = pending.append(jieguo);
                    }
                }
                break;
            default:
                break;
        }
    }

    private boolean judje1() {
        String a = "+-*/.";
        int[] b = new int[a.length()];
        int max;
        for (int i = 0; i < a.length(); i++) {
            String c = "" + a.charAt(i);
            b[i] = pending.lastIndexOf(c);
        }
        Arrays.sort(b);
        if (b[a.length() - 1] == -1) {
            max = 0;
        } else {
            max = b[a.length() - 1];
        }
        if (pending.indexOf(".", max) == -1) {
            return true;
        } else {
            return false;
        }
    }
    private int judje2(){
        int a=0,b=0;
        for(int i = 0 ; i < pending.length() ;i++){
            if(pending.charAt(i)=='(' ) {
                a++;
            }
            if(pending.charAt(i)==')' ) {
                b++;
            }
        }
        if(a == b)
            return 0;
        if(a > b)
            return 1;
        return 2;
    }




}

 

总结:

自己很菜,有很大的进步空间。

 

相关文章:

  • 2021-12-14
  • 2021-05-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-08
  • 2021-12-02
  • 2021-12-02
  • 2021-12-02
  • 2021-12-04
  • 2021-12-30
相关资源
相似解决方案