[慕课网作业-安卓]简易计算器

来源:互联网 发布:营销qq for mac 编辑:程序博客网 时间:2024/06/01 08:00

最近在慕课网上学习Android,小白上岗,智商感人,只能笨鸟先飞,在这里做个记录,方便后期查看。

使用AndroidStudio开发,跟随老师视频教学,手动敲代码,调试bug,最后好歹可以运行(汗颜!)

这里是我的github上的完整代码(https://github.com/xh-2016/CalculatorDemo.git),下面也会附上主要代码部分。

1.xml配置部分代码

最终界面如下图:


<?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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    app:layout_behavior="@string/appbar_scrolling_view_behavior"    tools:context=".MainActivity"    tools:showIn="@layout/activity_main"    android:background="#B0E0E6">    <EditText        android:id="@+id/et_showview"        android:layout_width="fill_parent"        android:layout_height="60dp"        android:editable="false"        android:gravity="bottom|right"        android:background="@drawable/white_bg"        android:textSize="20sp"        />    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:orientation="horizontal"        android:gravity="center_horizontal"        >        <Button            android:layout_width="60dp"            android:layout_height="60dp"            android:text="C"            android:textColor="@color/black"            android:paddingBottom="10dp"            android:paddingRight="10dp"            android:background="@drawable/white_btn_selector"            android:textSize="20sp"            android:gravity="right|bottom"            android:id="@+id/btn_clear"            />        <Button            android:layout_width="60dp"            android:layout_height="60dp"            android:text="DEL"            android:paddingBottom="10dp"            android:paddingRight="10dp"            android:layout_marginLeft="10dp"            android:textSize="20sp"            android:textColor="@color/black"            android:gravity="right|bottom"            android:background="@drawable/white_btn_selector"            android:id="@+id/btn_del"            />        <Button            android:layout_width="60dp"            android:layout_height="60dp"            android:text="÷"            android:layout_marginLeft="10dp"            android:textColor="@color/black"            android:paddingBottom="10dp"            android:paddingRight="10dp"            android:background="@drawable/white_btn_selector"            android:textSize="20sp"            android:gravity="right|bottom"            android:id="@+id/btn_divide"            />        <Button            android:layout_width="60dp"            android:layout_height="60dp"            android:text="×"            android:layout_marginLeft="10dp"            android:textColor="@color/black"            android:paddingBottom="10dp"            android:paddingRight="10dp"            android:background="@drawable/white_btn_selector"            android:textSize="20sp"            android:gravity="right|bottom"            android:id="@+id/btn_multiply"            />    </LinearLayout>    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:orientation="horizontal"        android:gravity="center_horizontal"        >        <Button            android:layout_width="60dp"            android:layout_height="60dp"            android:text="7"            android:textSize="20sp"            android:gravity="right|bottom"            android:textColor="@color/black"            android:paddingBottom="10dp"            android:paddingRight="10dp"            android:background="@drawable/white_btn_selector"            android:id="@+id/btn_7"            />        <Button            android:layout_width="60dp"            android:layout_height="60dp"            android:text="8"            android:layout_marginLeft="10dp"            android:textColor="@color/black"            android:paddingBottom="10dp"            android:paddingRight="10dp"            android:background="@drawable/white_btn_selector"            android:textSize="20sp"            android:gravity="right|bottom"            android:id="@+id/btn_8"            />        <Button            android:layout_width="60dp"            android:layout_height="60dp"            android:text="9"            android:layout_marginLeft="10dp"            android:textColor="@color/black"            android:paddingBottom="10dp"            android:paddingRight="10dp"            android:background="@drawable/white_btn_selector"            android:textSize="20sp"            android:gravity="right|bottom"            android:id="@+id/btn_9"            />        <Button            android:layout_width="60dp"            android:layout_height="60dp"            android:text="-"            android:layout_marginLeft="10dp"            android:textColor="@color/black"            android:paddingBottom="10dp"            android:paddingRight="10dp"            android:background="@drawable/white_btn_selector"            android:textSize="20sp"            android:gravity="right|bottom"            android:id="@+id/btn_minus"            />    </LinearLayout>    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:orientation="horizontal"        android:gravity="center_horizontal"        >        <Button            android:layout_width="60dp"            android:layout_height="60dp"            android:text="4"            android:textSize="20sp"            android:textColor="@color/black"            android:paddingBottom="10dp"            android:paddingRight="10dp"            android:background="@drawable/white_btn_selector"            android:gravity="right|bottom"            android:id="@+id/btn_4"            />        <Button            android:layout_width="60dp"            android:layout_height="60dp"            android:text="5"            android:layout_marginLeft="10dp"            android:textSize="20sp"            android:textColor="@color/black"            android:paddingBottom="10dp"            android:paddingRight="10dp"            android:background="@drawable/white_btn_selector"            android:gravity="right|bottom"            android:id="@+id/btn_5"            />        <Button            android:layout_width="60dp"            android:layout_height="60dp"            android:text="6"            android:layout_marginLeft="10dp"            android:textSize="20sp"            android:textColor="@color/black"            android:paddingBottom="10dp"            android:paddingRight="10dp"            android:background="@drawable/white_btn_selector"            android:gravity="right|bottom"            android:id="@+id/btn_6"            />        <Button            android:layout_width="60dp"            android:layout_height="60dp"            android:text="+"            android:layout_marginLeft="10dp"            android:textSize="20sp"            android:textColor="@color/black"            android:paddingBottom="10dp"            android:paddingRight="10dp"            android:background="@drawable/white_btn_selector"            android:gravity="right|bottom"            android:id="@+id/btn_plus"            />    </LinearLayout>    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:gravity="center_horizontal"        android:layout_marginTop="10dp"        >        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:orientation="vertical"            >            <LinearLayout                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:orientation="horizontal"                >                <Button                    android:layout_width="60dp"                    android:layout_height="60dp"                    android:text="1"                    android:layout_marginLeft="10dp"                    android:textSize="20sp"                    android:textColor="@color/black"                    android:paddingBottom="10dp"                    android:paddingRight="10dp"                    android:background="@drawable/white_btn_selector"                    android:gravity="right|bottom"                    android:id="@+id/btn_1"                    />                <Button                    android:layout_width="60dp"                    android:layout_height="60dp"                    android:text="2"                    android:layout_marginLeft="10dp"                    android:textSize="20sp"                    android:textColor="@color/black"                    android:paddingBottom="10dp"                    android:paddingRight="10dp"                    android:background="@drawable/white_btn_selector"                    android:gravity="right|bottom"                    android:id="@+id/btn_2"                    />                <Button                    android:layout_width="60dp"                    android:layout_height="60dp"                    android:text="3"                    android:layout_marginLeft="10dp"                    android:textSize="20sp"                    android:textColor="@color/black"                    android:paddingBottom="10dp"                    android:paddingRight="10dp"                    android:background="@drawable/white_btn_selector"                    android:gravity="right|bottom"                    android:id="@+id/btn_3"                    />            </LinearLayout>            <LinearLayout                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:orientation="horizontal"                >                <Button                    android:layout_width="130dp"                    android:layout_height="60dp"                    android:text="0"                    android:layout_marginLeft="10dp"                    android:layout_marginTop="10dp"                    android:textSize="20sp"                    android:textColor="@color/black"                    android:paddingBottom="10dp"                    android:paddingRight="10dp"                    android:background="@drawable/white_btn_selector"                    android:gravity="right|bottom"                    android:id="@+id/btn_0"                    />                <Button                    android:layout_width="60dp"                    android:layout_height="60dp"                    android:layout_marginTop="10dp"                    android:textColor="@color/black"                    android:paddingBottom="10dp"                    android:paddingRight="10dp"                    android:background="@drawable/white_btn_selector"                    android:text="."                    android:layout_marginLeft="10dp"                    android:textSize="20sp"                    android:gravity="right|bottom"                    android:id="@+id/btn_point"                    />            </LinearLayout>        </LinearLayout>        <Button            android:layout_width="60dp"            android:layout_height="130dp"            android:text="="            android:layout_marginLeft="10dp"            android:paddingBottom="10dp"            android:paddingRight="10dp"            android:background="@drawable/orange_btn_selector"            android:textSize="20sp"            android:gravity="right|bottom"            android:id="@+id/btn_equal"            />    </LinearLayout></LinearLayout>

2.MainActivity.java部分代码

package com.xh.calculatordemo;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {    Button btn_0;//数字0按钮    Button btn_1;//数字1按钮    Button btn_2;//数字2按钮    Button btn_3;//数字3按钮    Button btn_4;//数字4按钮    Button btn_5;//数字5按钮    Button  btn_6;//数字6按钮    Button btn_7;//数字7按钮    Button btn_8;//数字8按钮    Button btn_9;//数字9按钮    Button btn_clear;//清零按钮    Button btn_del;//删除按钮    Button btn_minus;//-    Button btn_plus;//+    Button btn_multiply;//×    Button btn_divide;//÷    Button btn_point;//.    Button btn_equal;//=    EditText et_showview;//显示输入内容的显示屏    Boolean clear_flag=true;//清除标识    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.content_main);//控制xml为content_main        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_clear= (Button) findViewById(R.id.btn_clear);        btn_del= (Button) findViewById(R.id.btn_del);        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_point=(Button) findViewById(R.id.btn_point);        btn_equal=(Button)findViewById(R.id.btn_equal);        //以上是实例化按钮        et_showview=(EditText)findViewById(R.id.et_showview);        //实例化输入框        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_del.setOnClickListener(this);        btn_clear.setOnClickListener(this);        btn_point.setOnClickListener(this);        btn_plus.setOnClickListener(this);        btn_minus.setOnClickListener(this);        btn_multiply.setOnClickListener(this);        btn_divide.setOnClickListener(this);        btn_equal.setOnClickListener(this);        //设置按钮的点击事件    }    @Override    public void onClick(View v) {        String str=et_showview.getText().toString();        switch (v.getId()){            case R.id.btn_0:            case R.id.btn_1:            case R.id.btn_2:            case R.id.btn_3:            case R.id.btn_4:            case R.id.btn_5:            case R.id.btn_6:            case R.id.btn_7:            case R.id.btn_8:            case R.id.btn_9:            case R.id.btn_point:                if(clear_flag){                    clear_flag=false;                    str="";//计算下一个的时候,应该将原来的设置为空                    et_showview.setText("");                }                et_showview.setText(str+((Button)v).getText());                break;            case R.id.btn_plus:            case R.id.btn_minus:            case R.id.btn_multiply:            case R.id.btn_divide:                if(clear_flag){                    clear_flag=false;                    str="";                    et_showview.setText("");                }                et_showview.setText(str + " " + ((Button) v).getText() + " ");//将点击的运算符添加到输入框前后有“ ”用于区别                break;            case R.id.btn_equal:                getResult();                break;            case R.id.btn_del:                if(clear_flag){                    clear_flag=false;                    str="";//计算下一个的时候,应该将原来的设置为空                    et_showview.setText("");                }                else if(str!=null&& !str.equals(""))                {                    et_showview.setText(str.substring(0,str.length()-1));                }                break;            case R.id.btn_clear:                clear_flag=false;                str="";//计算下一个的时候,应该将原来的设置为空                et_showview.setText("");                break;        }    }    /**     * 四则运算求计算结果     */    private void getResult() {        String exp = et_showview.getText().toString();        if(clear_flag){            clear_flag=false;            return;        }        if (exp == null || exp.equals("")) {            return;        }        //没有输入运算符(运算符前后都手动加入了空格)        if (!exp.contains(" ")) {//如果不包含空格(运算符前面有空格),直接返回(比如点了数字,没有运算符)            return;        }        clear_flag = true;        double result = 0;        String s1 = exp.substring(0, exp.indexOf(" "));//运算符前面的字符串        String op = exp.substring(exp.indexOf(" ") + 1, exp.indexOf(" ") + 2);//运算符        String s2 = exp.substring(exp.indexOf(" ") + 3);//运算符后面的字符串        //s1、s2非空        if (!s1.equals("") && !s2.equals("")) {            double d1 = Double.parseDouble(s1);            double d2 = Double.parseDouble(s2);            if (op.equals("+")) {                result = d1 + d2;            } else if (op.equals("-")) {                result = d1 - d2;            } else if (op.equals("×")) {                result = d1 * d2;            } else if (op.equals("÷")) {                if (d2 == 0) {                    Toast.makeText(MainActivity.this, "除数不能为0!!!", Toast.LENGTH_LONG).show();                    result = 0;                } else {                    result = d1 / d2;                }            }                if (!s1.contains(".") && !s2.contains(".")&&!op.equals("÷")) {                    int r = (int)result;                    et_showview.setText(r + "");                } else {                    et_showview.setText(result + "");                }        }else if(!s1.equals("") && s2.equals("")){            Toast.makeText(MainActivity.this, "不具备运算",Toast.LENGTH_LONG).show();            et_showview.setText(exp);        }     //s1为空,s2非空        else if(s1.equals("") && !s2.equals("")){            double d2 = Double.parseDouble(s2);            if (op.equals("+")) {                result = 0 + d2;            } else if (op.equals("-")) {                result = 0 - d2;            } else if (op.equals("×")) {                result = 0 ;            } else if (op.equals("÷")) {                result = 0;            }            if (!s1.contains(".") && !s2.contains(".")&&!op.equals("÷")) {                int r = (int)result;                et_showview.setText(r + "");            } else {                et_showview.setText(result + "");            }        }        //s1、s2都是空        else{            et_showview.setText("");        }    }}
3.说说调试过程吧,看了老师的视频之后完成了代码,没有报错,但是真机调试的时候,出现了一些bug:

3.1刚开始连界面都无法显示,最后发现是这一句的问题 

setContentView(R.layout.content_main);//控制xml为content_main

我的xml文件是content_main,而MainActivity.main默认xml是Activity_main,所以最初无法渲染出界面

3.2可以显示界面之后,又有问题,每次点击按钮就会使app闪退,最后发现是这一句的问题

<strong><span style="font-size:12px;"> Boolean clear_flag=true;//清除标识</span></strong>

clear_flag最初我没有赋初值,导致初始化没有完成

3.3解决app闪退后,又发现减法运算有问题,每次输出都是0,最后发现是这句的问题

          } else if (op.equals("-")) {

在xml中,界面上显示的减号我是输入的搜狗输入里面的标准数字符号,后面写代码的时候,我以为键盘上面的减号就可以匹配成功,事实上并不能,所以减法运算有问题;同理加法运算的符号也要避免这个问题。

4.还有一部分是drawable里对一些背景的设置,由于比较简单,我这里就不写了,如果需要,可以直接去上面的地址下载完整版代码。

0 1