1. 程式人生 > >用android studio寫一個簡單並且bug奇多的計算器

用android studio寫一個簡單並且bug奇多的計算器

思路:先佈局,在將相應的按鍵例項化,建立監聽器,計算。可以說是非常簡單了。

貼一下程式碼:

佈局程式碼:

<?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:background="@android:color/darker_gray"
    tools:context="com.example.fxh98.cal.MainActivity">


    <EditText
        android:id="@+id/input"
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:ems="10"
        android:enabled="false"
        android:textSize="30sp"
        android:gravity="right|bottom"

        android:inputType="textPersonName" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:orientation="horizontal">

        <Button
            android:layout_width="290dp"
            android:layout_height="match_parent"
            android:text="C"
            android:id="@+id/btn_C"

            android:textSize="30sp" />

        <Button
            android:layout_width="90dp"
            android:layout_height="match_parent"
            android:text="DEL"
            android:id="@+id/btn_Del"

            android:textSize="30sp"/>
    </LinearLayout>

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

        <Button
            android:layout_width="95dp"
            android:layout_height="80dp"
            android:text="7"
            android:id="@+id/btn_7"

            android:textSize="40sp"/>

        <Button
            android:layout_width="95dp"
            android:layout_height="80dp"
            android:text="8"
            android:id="@+id/btn_8"
            android:textSize="40sp"/>

        <Button
            android:layout_width="95dp"
            android:layout_height="80dp"
            android:text="9"
            android:id="@+id/btn_9"
            android:textSize="40sp"/>

        <Button
            android:layout_width="95dp"
            android:layout_height="80dp"
            android:text="/"
            android:id="@+id/btn_div"
            android:textSize="40sp"/>
    </LinearLayout>

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

        <Button
            android:layout_width="95dp"
            android:layout_height="80dp"
            android:text="4"
            android:id="@+id/btn_4"
            android:textSize="40sp"/>

        <Button
            android:layout_width="95dp"
            android:layout_height="80dp"
            android:text="5"
            android:id="@+id/btn_5"
            android:textSize="40sp"/>

        <Button
            android:layout_width="95dp"
            android:layout_height="80dp"
            android:text="6"
            android:id="@+id/btn_6"
            android:textSize="40sp"/>

        <Button
            android:layout_width="95dp"
            android:layout_height="80dp"
            android:text="*"
            android:id="@+id/btn_mul"
            android:textSize="40sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="95dp"
            android:layout_height="80dp"
            android:text="1"
            android:id="@+id/btn_1"
            android:textSize="40sp"/>

        <Button
            android:layout_width="95dp"
            android:layout_height="80dp"
            android:text="2"
            android:id="@+id/btn_2"
            android:textSize="40sp"/>

        <Button
            android:layout_width="95dp"
            android:layout_height="80dp"
            android:text="3"
            android:id="@+id/btn_3"
            android:textSize="40sp"/>

        <Button
            android:layout_width="95dp"
            android:layout_height="80dp"
            android:text="-"
            android:id="@+id/btn_sub"
            android:textSize="40sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="95dp"
            android:layout_height="80dp"
            android:text="."
            android:id="@+id/btn_de"
            android:textSize="40sp"/>

        <Button
            android:layout_width="95dp"
            android:layout_height="80dp"
            android:text="0"
            android:id="@+id/btn_0"
            android:textSize="40sp"/>

        <Button
            android:layout_width="95dp"
            android:layout_height="80dp"
            android:text="="
            android:id="@+id/btn_eql"
            android:textSize="40sp"/>

        <Button
            android:layout_width="95dp"
            android:layout_height="80dp"
            android:text="+"
            android:id="@+id/btn_add"
            android:textSize="40sp"/>
    </LinearLayout>

</LinearLayout>


簡單的佈局,建議不要拖動佈局,用手打的佈局更方便。

main_activity檔案:
package com.example.fxh98.cal;

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 bt_0,bt_1,bt_2,bt_3,bt_4,bt_5,bt_6,bt_7,bt_8,bt_9,bt_de;
    Button bt_mul,bt_div,bt_add,bt_sub;
    Button bt_clr,bt_del,bt_eql;
    EditText et_input;
    boolean clear_flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //例項化物件
        bt_0 = findViewById(R.id.btn_0);
        bt_1 = findViewById(R.id.btn_1);
        bt_2 = findViewById(R.id.btn_2);
        bt_3 = findViewById(R.id.btn_3);
        bt_4 = findViewById(R.id.btn_4);
        bt_5 = findViewById(R.id.btn_5);
        bt_6 = findViewById(R.id.btn_6);
        bt_7 = findViewById(R.id.btn_7);
        bt_8 = findViewById(R.id.btn_8);
        bt_9 = findViewById(R.id.btn_9);
        bt_add = findViewById(R.id.btn_add);
        bt_sub = findViewById(R.id.btn_sub);
        bt_mul = findViewById(R.id.btn_mul);
        bt_div = findViewById(R.id.btn_div);
        bt_de = findViewById(R.id.btn_de);
        bt_eql = findViewById(R.id.btn_eql);
        bt_clr = findViewById(R.id.btn_C);
        bt_del = findViewById(R.id.btn_Del);
        et_input = findViewById(R.id.input);

        //設定監聽器

        bt_0.setOnClickListener(this);
        bt_1.setOnClickListener(this);
        bt_2.setOnClickListener(this);
        bt_3.setOnClickListener(this);
        bt_4.setOnClickListener(this);
        bt_5.setOnClickListener(this);
        bt_6.setOnClickListener(this);
        bt_7.setOnClickListener(this);
        bt_8.setOnClickListener(this);
        bt_9.setOnClickListener(this);
        bt_eql.setOnClickListener(this);
        bt_del.setOnClickListener(this);
        bt_de.setOnClickListener(this);
        bt_div.setOnClickListener(this);
        bt_mul.setOnClickListener(this);
        bt_add.setOnClickListener(this);
        bt_sub.setOnClickListener(this);
        bt_clr.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        String str = et_input.getText().toString();
        switch (view.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_de:
                if (clear_flag){
                    clear_flag = false;
                    str = "";
                    et_input.setText("");
                }
                et_input.setText(str + ((Button)view).getText());
                break;
            case R.id.btn_add:
            case R.id.btn_mul:
            case R.id.btn_div:
            case R.id.btn_sub:
                if (clear_flag){
                    clear_flag = false;
                    str = "";
                    et_input.setText("");
                }
                if(str.contains("+")||str.contains("-")||str.contains("*")||str.contains("/")){
                    et_input.setText(str.substring(0,str.length()-3) + " " + ((Button)view).getText() + " ");
                }
                else et_input.setText(str + " " + ((Button)view).getText() + " ");
                break;
            case R.id.btn_Del:
                if (clear_flag){
                    clear_flag = false;
                    str = "";
                    et_input.setText("");
                }
                else if (str != null && !str.equals("")){
                    et_input.setText(str.substring(0,str.length()-1));
                }
                break;
            case R.id.btn_C:
                clear_flag = false;
                str = "";
                et_input.setText("");
                break;
            case R.id.btn_eql:
                getResult();
                break;

        }

    }

    public void getResult() {
        String exp = et_input.getText().toString();
        if(exp == null||exp.equals("")) return;
        if(!exp.contains(" ")){//dont hava + - * /
            return ;
        }
        if(clear_flag){
            clear_flag = false;
            return;
        }
        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(d1 != 0&&d2 != 0)
                    cnt = d1 / d2;
                if(d2 == 0)
                    cnt = 0;
            }
            if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")) {
                int res = (int) cnt;
                et_input.setText(res+"");
            }else {
                et_input.setText(cnt+"");}
        }
        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;
                et_input.setText(res+"");
            }else {
                et_input.setText(cnt+"");}
        }
        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;
                et_input.setText(res+"");
            }else {
                et_input.setText(cnt+"");}
        }
        else {
            et_input.setText("");
        }
    }
}


bug有好幾個,但是由於是練手的程式,所以不想再做修改。