移动应用开发 实验1——设计三种计算器的UI

这是一个Android简易计算器的实现,包括布局设计和Java代码。XML布局文件中定义了计算器的各个按钮,包括数字、运算符和功能键,而Java部分则实现了按钮点击事件,进行相应的数学运算。代码中还提到存在未解决的问题,如特定情况下的精度问题,并提到了硬编码和Android开发的最佳实践。
摘要由CSDN通过智能技术生成

一、实验要求

 1.实现以下照片的布局

 

 2.附加题,实现普通计算机效果

二、代码展示

1.Activity部分

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent">

        <TextView
            android:id="@+id/textview_01"
            android:layout_width="match_parent"
            android:layout_height="105sp"
            android:background="@drawable/underline"
            android:gravity="right"
            android:text="0"
            android:textColor="@color/black"
            android:textSize="50sp" />

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

            <Button
                android:id="@+id/button_return"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:backgroundTint="#d3d3d3"
                android:text="退格"
                android:textColor="@color/black"
                android:textSize="23dp" />

            <Button
                android:id="@+id/button_clear"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:backgroundTint="#d3d3d3"
                android:text="清除"
                android:textColor="@color/black"
                android:textSize="23dp" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:backgroundTint="#d3d3d3"
                android:text="+-"
                android:textColor="@color/black"
                android:textSize="25dp" />

            <Button
                android:id="@+id/button_add"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:backgroundTint="#d3d3d3"
                android:text="+"
                android:textColor="@color/black"
                android:textSize="25dp" />
        </LinearLayout>

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

            <Button
                android:id="@+id/button_07"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:backgroundTint="#d3d3d3"
                android:text="7"
                android:textColor="@color/black"
                android:textSize="25dp" />

            <Button
                android:id="@+id/button_08"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:backgroundTint="#d3d3d3"
                android:text="8"
                android:textColor="@color/black"
                android:textSize="25dp" />

            <Button
                android:id="@+id/button_09"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:backgroundTint="#d3d3d3"
                android:text="9"
                android:textColor="@color/black"
                android:textSize="25dp" />

            <Button
                android:id="@+id/button_reduce"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:backgroundTint="#d3d3d3"
                android:text="-"
                android:textColor="@color/black"
                android:textSize="25dp" />
        </LinearLayout>

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

            <Button
                android:id="@+id/button_04"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:backgroundTint="#d3d3d3"
                android:text="4"
                android:textColor="@color/black"
                android:textSize="25dp" />

            <Button
                android:id="@+id/button_05"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:backgroundTint="#d3d3d3"
                android:text="5"
                android:textColor="@color/black"
                android:textSize="25dp" />

            <Button
                android:id="@+id/button_06"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:backgroundTint="#d3d3d3"
                android:text="6"
                android:textColor="@color/black"
                android:textSize="25dp" />

            <Button
                android:id="@+id/button_ride"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:backgroundTint="#d3d3d3"
                android:text="*"
                android:textColor="@color/black"
                android:textSize="25dp" />
        </LinearLayout>

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

            <Button
                android:id="@+id/button_01"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:backgroundTint="#d3d3d3"
                android:text="1"
                android:textColor="@color/black"
                android:textSize="25dp" />

            <Button
                android:id="@+id/button_02"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:backgroundTint="#d3d3d3"
                android:text="2"
                android:textColor="@color/black"
                android:textSize="25dp" />

            <Button
                android:id="@+id/button_03"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:backgroundTint="#d3d3d3"
                android:text="3"
                android:textColor="@color/black"
                android:textSize="25dp" />

            <Button
                android:id="@+id/button_except"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:backgroundTint="#d3d3d3"
                android:text="/"
                android:textColor="@color/black"
                android:textSize="25dp" />
        </LinearLayout>

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

            <Button
                android:id="@+id/button_00"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:backgroundTint="#d3d3d3"
                android:text="0"
                android:textColor="@color/black"
                android:textSize="25dp" />

            <Button
                android:id="@+id/button_spot"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:backgroundTint="#d3d3d3"
                android:text="."
                android:textColor="@color/black"
                android:textSize="25dp" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:backgroundTint="#d3d3d3"
                android:text="1/X"
                android:textColor="@color/black"
                android:textSize="25dp" />

            <Button
                android:id="@+id/button_equal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:backgroundTint="#d3d3d3"
                android:text="="
                android:textColor="@color/black"
                android:textSize="25dp" />
        </LinearLayout>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

 2.java部分

package com.example.a11;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    TextView textView;
    Button button_return,button_add,button_clear,button_equal,button_except,button_reduce,
            button_ride,button_spot,button_00,button_01,button_02,button_03,button_04,
            button_05,button_06,button_07,button_08,button_09;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViewAndListener();
    }

    private void initViewAndListener() {
        button_return=findViewById(R.id.button_return);
        button_00=findViewById(R.id.button_00);
        button_01=findViewById(R.id.button_01);
        button_02=findViewById(R.id.button_02);
        button_03=findViewById(R.id.button_03);
        button_04=findViewById(R.id.button_04);
        button_05=findViewById(R.id.button_05);
        button_06=findViewById(R.id.button_06);
        button_07=findViewById(R.id.button_07);
        button_08=findViewById(R.id.button_08);
        button_09=findViewById(R.id.button_09);
        button_clear=findViewById(R.id.button_clear);
        button_add=findViewById(R.id.button_add);
        button_reduce=findViewById(R.id.button_reduce);
        button_ride=findViewById(R.id.button_ride);
        button_except=findViewById(R.id.button_except);
        button_spot=findViewById(R.id.button_spot);
        button_equal=findViewById(R.id.button_equal);

        button_return.setOnClickListener(this);
        button_00.setOnClickListener(this);
        button_01.setOnClickListener(this);
        button_02.setOnClickListener(this);
        button_03.setOnClickListener(this);
        button_04.setOnClickListener(this);
        button_05.setOnClickListener(this);
        button_06.setOnClickListener(this);
        button_07.setOnClickListener(this);
        button_08.setOnClickListener(this);
        button_09.setOnClickListener(this);
        button_spot.setOnClickListener(this);
        button_equal.setOnClickListener(this);
        button_except.setOnClickListener(this);
        button_add.setOnClickListener(this);
        button_reduce.setOnClickListener(this);
        button_ride.setOnClickListener(this);
        button_clear.setOnClickListener(this);

        textView = findViewById(R.id.textview_01);
    }

    @Override
    public void onClick(View view) {
        //获取文本框内容
        String currentText = textView.getText().toString();

        switch (view.getId()){
            case R.id.button_00:
                if ((textView.getText().toString().equals("0")))
                    return;
                isflag("0");
                break;
            case R.id.button_01:
                isflag("1");
                break;
            case R.id.button_02:
                isflag("2");
                break;
            case R.id.button_03:
                isflag("3");
                break;
            case R.id.button_04:
                isflag("4");
                break;
            case R.id.button_05:
                isflag("5");
                break;
            case R.id.button_06:
                isflag("6");
                break;
            case R.id.button_07:
                isflag("7");
                break;
            case R.id.button_08:
                isflag("8");
                break;
            case R.id.button_09:
                isflag("9");
                break;
            case R.id.button_add:
                now_num++;
                isflag("+");
                if (now_num > 1) {
                    clear();
                }
                break;
            case R.id.button_except:
                now_num++;
                isflag("/");
                if (now_num > 1) {
                    clear();
                }
                break;
            case R.id.button_spot:
                isflag(".");
                break;
            case R.id.button_reduce:

                isflag("-");
                break;
            case R.id.button_ride:
                now_num++;
                isflag("*");
                if (now_num > 1) {
                    clear();
                }
                break;
            case R.id.button_clear:
                clear();
                break;
            case R.id.button_return:
                if (TextUtils.isEmpty(textView.getText())) {
                    return;
                }
                if (currentText.substring(0, currentText.length() - 1).length() > 0) {
                    textView.setText(currentText.substring(0, currentText.length() - 1));
                } else {
                    textView.setText("0");
                    flag = false;
                    now_num = 0;
                }
                break;
            case R.id.button_equal:
                if (TextUtils.isEmpty(textView.getText())) {
                    return;
                }
                equal();
                flag = false;
                now_num = 0;
                break;
        }
    }

    private String num_in_now;//记录当前结果
    private boolean flag = false;//记录计算器状态
    int now_num = 0;

     String clear() {
         textView.setText("0");// 视觉上呈现 清除效果,全部直接置0
         flag = false;
         now_num = 0;
         return "error!!!";
    }

    //判断0要不要替换
    //看flag的状态,输入数字
    //flag最开始是假,所以优先执行假的操作
    private void isflag(String s) {
        if(flag)
        {
            textView.setText(textView.getText()+s);
        }
        else
        {
            //作用:主要针对第一次点击事件,如果是输入符号,则变成0和符号,如果输入的是数字,0就变成那个数字
            if (s.equals(".") || s.equals("+")  || s.equals("*") || s.equals("/")) {
                textView.setText(textView.getText() + s);
            }
            else
            {
                textView.setText(s);
            }
            flag = true;
        }
    }

    private void equal(){
        textView.setText(operation(textView.getText().toString()));
    }

    //运算方法
    private String operation(String string){
        int index;//索引,其实就是数组(我们运算的那串数字)的下标
        double num_1,num_2;
        //加法
        if(string.contains("+")){
            index = string.indexOf("+");
            num_1 = Double.parseDouble(string.substring(0,index));//加号(包括加号)前面的部分
            num_2 = Double.parseDouble(string.substring(index+1,string.length()));//加号后面的部分
            return num_1 + num_2 + "";
        }
        //减法
        if (string.contains("-")) {
            index = string.indexOf("-");
            if(index == 0){
                //这是应对负数的情况,此时减号相当于 负号
                String string_2 = string.substring(1,string.length());
                int index_2 = string_2.indexOf("-");
                num_1 = Double.parseDouble(string_2.substring(0,index_2));
                num_2 = Double.parseDouble(string_2.substring(index_2+1,string.length()));
                return (-(num_1 + num_2)) + "";
            }else{
                //这是正常减法的情况
                num_1 = Double.parseDouble(string.substring(0,index));
                num_2 = Double.parseDouble(string.substring(index + 1, string.length()));
                return num_1 - num_2 + "";
            }
        }
        //乘法
        if (string.contains("*")) {
            index = string.indexOf("*");
            num_1 = Double.parseDouble(string.substring(0,index));
            num_2 = Double.parseDouble(string.substring(index+1,string.length()));
            return num_1 * num_2 + "";
        }
        //除法
        if (string.contains("/")) {
            index = string.indexOf("/");
            num_1 = Double.parseDouble(string.substring(0,index));
            num_2 = Double.parseDouble(string.substring(index+1,string.length()));
            return num_1 / num_2 + "";
        }
        return "error!!!";
    }
}

备注:

1. “加减”  “分数” 部分并未实现

2.实现8.9*9的时候会出现亿分位多数的情况,,除此之外还有8.6*6等等,无法解决,望解决的同学发在评论区,让我学习一下

 三、实验总结

(一)Activity部分

总体的代码量重复度很高,复制粘贴即可

(二)Java部分

   1.  不要使用硬编码语言(hardcoded )

 Android Studio在xml布局文件中,有时会显示“Hardcoded String XXX,should use @string resource”的警告信息。其中Hardcoded String指的是字符串硬编码,该警告信息的意思是最好不要使用字符串硬编码,而是要使用@string资源。

   硬编码即直接指定内容时,带来的不便有:

(1)当程序中存在多个相同的硬编码内容并需要修改时,要将程序代码中每个硬编码内容都要修改一遍,         这样的过程比较繁琐

(2)硬编码不利于将程序代码转变为其他语言

(3)每个硬编码内容都要占用相应的内存空间,因此程序占用的内存更大。

2. android:contentDescription的作用是什么

色盲或者使用时,她所使用的软件如果带朗读功能会读这里面的文字内容,本质是文本框文字部分,只不过给盲人等困难人群设置的

3.onclick直接用已经过时了,因为缺少安全性,现在都用setOnClickListen,但是这种方法非常麻烦,学了一天都没搞懂怎么用。

4.怎么在创建一个窗口(即activity)

   新建里面找到activity,创建即可

5.声明可以在onCreate里面声明,也可以在外部声明,外部声明就是全局变量

   onCreate只运行一次

   必须在onCreate里面初始化要用的控件

6.夸activity使用控件ID会报错(报错:not found),使用数据回传

7.substring(begin,end): 此字符串的一个子字符串。该子字符串始于指定索引处的字符,一直到此字符串索引末尾

   例:"hamburger".substring(3,8) returns "burge"

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
运用HTML、CSS、JavaScript、jQuery等相关技术和DreamweaverCS6工具设计一个爱旅行旅游网站,要求网站具有用户登录验证、用户登陆后显示用户名,分为管理员、注册用户、游客三种访问权限,管理员可以修改后台信息、用户可以浏览和提交相关信息、游客可以浏览网站信息。网站具有图片轮播效果、鼠标对图片的进入和移出特效、点击小图放大图片特效、文字超链接、图片超链接、锚链接、菜单栏、景点预定须知、景点简介、交通指南、用户点评功能、div分块、CSS样式、下拉菜单、景点门票预订、酒店预订,表单输入验证功能,提交后提示预定信息,导航栏功能、AJAX城市天气预报查询功能,网站界面美观,布局配色合理,方便和吸引用户的使用。包含10个以上自己设计的超链接页面。 具体要求为: (1) 网站主题为爱旅行旅游网站,网站标志语、界面主色调与网站主题相匹配,界面布局设计美观、大方、合理,要求用到HTML、CSS、JavaScript和jQuery网页特效、采用JavaScript和jQuery技术编程。 (2) 用户登陆后显示用户名,分为管理员、注册用户、游客三种访问权限,管理员可以修改后台信息、用户可以浏览和提交相关信息、游客可以浏览网站信息,网站具有图片轮播效果、鼠标对图片的进入和移出特效、点击小图放大图片特效、淡入淡出特效、滑动特效、文字超链接、图片超链接、锚链接、菜单栏、景点预定须知、景点简介、交通指南、用户点评功能、div分块、CSS样式、下拉菜单、景点门票预订、酒店预订,表单输入验证功能,提交后提示预定信息,导航栏功能、AJAX城市天气预报查询功能,,10个以上自己设计的超链接页面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值