一、实验要求
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"