• APP-计算器


    在网课上学习了计算器的制作方法,并自己做了小常识,看完一便后,感觉自己会了,思想也明白了,但是当关掉视频真正开始做的时候会发向许多的问题,自己在前的好多语法用的并不是很熟练,但是反复的查阅资料,观看教程,还是可以完成完成这个项目的开发。

    activity_main.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"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/tvScreen"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:gravity="right"
            android:textAppearance="?android:attr/textAppearanceLarge"/>
    
        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
    
            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
    
                <Button
                    android:id="@+id/btn1"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="1" />
                <Button
                    android:id="@+id/btn2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2"
                    android:layout_weight="1"/>
                <Button
                    android:id="@+id/btn3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="3"
                    android:layout_weight="1"/>
                <Button
                    android:id="@+id/btnAdd"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="+"
                    android:layout_weight="1"/>
    
            </TableRow>
            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
    
                <Button
                    android:id="@+id/btn4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="4"
                    android:layout_weight="1"/>
                <Button
                    android:id="@+id/btn5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="5"
                    android:layout_weight="1"/>
                <Button
                    android:id="@+id/btn6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="6"
                    android:layout_weight="1"/>
                <Button
                    android:id="@+id/btnSub"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="-"
                    android:layout_weight="1"/>
    
            </TableRow>
            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
    
                <Button
                    android:id="@+id/btn7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="7"
                    android:layout_weight="1"/>
                <Button
                    android:id="@+id/btn8"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="8"
                    android:layout_weight="1"/>
                <Button
                    android:id="@+id/btn9"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="9"
                    android:layout_weight="1"/>
                <Button
                    android:id="@+id/btnX"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="*"
                    android:layout_weight="1"/>
    
            </TableRow>
            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
    
                <Button
                    android:id="@+id/btnClear"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="C"
                    android:layout_weight="1"/>
                <Button
                    android:id="@+id/btn0"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0"
                    android:layout_weight="1"/>
                <Button
                    android:id="@+id/btnResult"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="="
                    android:layout_weight="1"/>
                <Button
                    android:id="@+id/btnDiv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="/"
                    android:layout_weight="1"/>
    
            </TableRow>
        </TableLayout>
    
    </LinearLayout>

    MainActivity.java:

    package com.example.counte;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
    
        private TextView tvScrean;
        private List<item> items=new ArrayList<item>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            tvScrean=(TextView)findViewById((R.id.tvScreen));
            findViewById(R.id.btn0).setOnClickListener((View.OnClickListener) this);
            findViewById(R.id.btn1).setOnClickListener((View.OnClickListener) this);
            findViewById(R.id.btn2).setOnClickListener((View.OnClickListener) this);
            findViewById(R.id.btn3).setOnClickListener((View.OnClickListener) this);
            findViewById(R.id.btn4).setOnClickListener((View.OnClickListener) this);
            findViewById(R.id.btn5).setOnClickListener((View.OnClickListener) this);
            findViewById(R.id.btn6).setOnClickListener((View.OnClickListener) this);
            findViewById(R.id.btn7).setOnClickListener((View.OnClickListener) this);
            findViewById(R.id.btn8).setOnClickListener((View.OnClickListener) this);
            findViewById(R.id.btn9).setOnClickListener((View.OnClickListener) this);
            findViewById(R.id.btnAdd).setOnClickListener((View.OnClickListener) this);
            findViewById(R.id.btnSub).setOnClickListener((View.OnClickListener) this);
            findViewById(R.id.btnX).setOnClickListener((View.OnClickListener) this);
            findViewById(R.id.btnDiv).setOnClickListener((View.OnClickListener) this);
            findViewById(R.id.btnClear).setOnClickListener((View.OnClickListener) this);
            findViewById(R.id.btnResult).setOnClickListener((View.OnClickListener) this);
    
    
        }
    
    
        public void onClick(View v){
            switch (v.getId()){
                case R.id.btn0:
                    tvScrean.append("0");
                    break;
                case R.id.btn1:
                    tvScrean.append("1");
                    break;
                case R.id.btn2:
                    tvScrean.append("2");
                    break;
                case R.id.btn3:
                    tvScrean.append("3");
                    break;
                case R.id.btn4:
                    tvScrean.append("4");
                    break;
                case R.id.btn5:
                    tvScrean.append("5");
                    break;
                case R.id.btn6:
                    tvScrean.append("6");
                    break;
                case R.id.btn7:
                    tvScrean.append("7");
                    break;
                case R.id.btn8:
                    tvScrean.append("8");
                    break;
                case R.id.btn9:
                    tvScrean.append("9");
                    break;
                case R.id.btnAdd:
                    items.add(new item(Double.parseDouble(tvScrean.getText().toString()),Types.Num));
                    check();
                    items.add(new item(0,Types.Add));
                    tvScrean.setText("");//清空文本
                    break;
                case R.id.btnSub:
                    items.add(new item(Double.parseDouble(tvScrean.getText().toString()),Types.Num));
                    check();
                    items.add(new item(0,Types.Sub));
                    tvScrean.setText("");//清空文本
                    break;
                case R.id.btnX:
                    items.add(new item(Double.parseDouble(tvScrean.getText().toString()),Types.Num));
                    check();
                    items.add(new item(0,Types.X));
                    tvScrean.setText("");//清空文本
                    break;
                case R.id.btnDiv:
                    items.add(new item(Double.parseDouble(tvScrean.getText().toString()),Types.Num));
                    check();
                    items.add(new item(0,Types.Div));
                    tvScrean.setText("");//清空文本
                    break;
                case R.id.btnClear:
                    tvScrean.setText("");
                    items.clear();//数组清空
                    break;
                case R.id.btnResult:
                    items.add(new item(Double.parseDouble(tvScrean.getText().toString()),Types.Num));
                    check();
                    tvScrean.setText(items.get(0).value+"");
                    items.clear();//清空数组
                    break;
    
            }
        }
        public void check(){
            if(items.size()>=3){
    
                double a=items.get(0).value;
                double b=items.get(2).value;
                int opt=items.get(1).type;
    
                items.clear();
    
                switch (opt){
                    case Types.Add:
                        items.add(new item(a+b,Types.Num));
                        break;
                    case Types.Sub:
                        items.add(new item(a-b,Types.Num));
                        break;
                    case Types.X:
                        items.add(new item(a*b,Types.Num));
                        break;
                    case Types.Div:
                        items.add(new item(a/b,Types.Num));
                        break;
                }
            }
        }
    }

    item.java:

    package com.example.counte;
    
    public class item {
    
        public item(double value,int type){
            this.value=value;
            this.type=type;
        }
        public double value=0;
        public int type=0;
    }

    Types.java:

    package com.example.counte;
    
    public class Types {
        public static final int Add=1;
        public static final int Sub=2;
        public static final int X=3;
        public static final int Div=4;
        public static final int Num=5;
    }

    但其中出现的最为明显的问题:

  • 相关阅读:
    (待续)【转载】 Deep Reinforcement Learning Doesn't Work Yet(这里有一篇深度强化学习劝退文)
    【转载】 深度强化学习走入「死胡同」,继续死磕电子游戏还是另辟蹊径?
    【转载】 Docker-关于docker cpu的限制后,实际效果的研究
    个人常用的 matplotlib 绘图模板
    【转载】共轭梯度法(视频讲解) 数值分析6(3共轭梯度法) ——苏州大学
    【转载】 向量,标量对向量求导数
    【转载】 Linux Hang Task 简介
    AOC U2790PC对比上一代的AOC U2790PQU怎么样?
    屏幕ppi
    ubuntu杀死进程
  • 原文地址:https://www.cnblogs.com/hhjing/p/12236876.html
Copyright © 2020-2023  润新知