使用GridLayout对计算器按键进行布局,大致代码如下:
<?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:padding="5dp"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="计算器"
android:gravity="center"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"/>
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFF"
android:text="0"
android:textSize="25sp"
android:lines="7"
android:gravity="bottom|right"/>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="4"
android:rowCount="5">
<Button
android:id="@+id/btn_cancel"
android:width="0dp"
android:height="@dimen/button_height_size"
android:layout_columnWeight="1"
android:gravity="center"
android:text="C"
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/button_font_size"/>
<Button
android:id="@+id/btn_percentage"
android:width="0dp"
android:height="@dimen/button_height_size"
android:layout_columnWeight="1"
android:gravity="center"
android:text="%"
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/button_font_size"/>
<ImageButton
android:id="@+id/btn_backspace"
android:width="0dp"
android:layout_height="75dp"
android:height="@dimen/button_height_size"
android:layout_columnWeight="1"
android:scaleType="centerInside"
android:src="@drawable/backspace"
/>
<Button
android:id="@+id/btn_divider"
android:width="0dp"
android:height="@dimen/button_height_size"
android:layout_columnWeight="1"
android:gravity="center"
android:text="/"
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/button_font_size"/>
<Button
android:id="@+id/btn_seven"
android:width="0dp"
android:height="@dimen/button_height_size"
android:layout_columnWeight="1"
android:gravity="center"
android:text="7"
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/button_font_size"/>
<Button
android:id="@+id/btn_eight"
android:width="0dp"
android:height="@dimen/button_height_size"
android:layout_columnWeight="1"
android:gravity="center"
android:text="8"
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/button_font_size"/>
<Button
android:id="@+id/btn_nine"
android:width="0dp"
android:height="@dimen/button_height_size"
android:layout_columnWeight="1"
android:gravity="center"
android:text="9"
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/button_font_size"/>
<Button
android:id="@+id/btn_multiply"
android:width="0dp"
android:height="@dimen/button_height_size"
android:layout_columnWeight="1"
android:gravity="center"
android:text="*"
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/button_font_size"/>
<Button
android:id="@+id/btn_four"
android:width="0dp"
android:height="@dimen/button_height_size"
android:layout_columnWeight="1"
android:gravity="center"
android:text="4"
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/button_font_size"/>
<Button
android:id="@+id/btn_five"
android:width="0dp"
android:height="@dimen/button_height_size"
android:layout_columnWeight="1"
android:gravity="center"
android:text="5"
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/button_font_size"/>
<Button
android:id="@+id/btn_six"
android:width="0dp"
android:height="@dimen/button_height_size"
android:layout_columnWeight="1"
android:gravity="center"
android:text="6"
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/button_font_size"/>
<Button
android:id="@+id/btn_minus"
android:width="0dp"
android:height="@dimen/button_height_size"
android:layout_columnWeight="1"
android:gravity="center"
android:text="-"
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/button_font_size"/>
<Button
android:id="@+id/btn_one"
android:width="0dp"
android:height="@dimen/button_height_size"
android:layout_columnWeight="1"
android:gravity="center"
android:text="1"
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/button_font_size"/>
<Button
android:id="@+id/btn_two"
android:width="0dp"
android:height="@dimen/button_height_size"
android:layout_columnWeight="1"
android:gravity="center"
android:text="2"
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/button_font_size"/>
<Button
android:id="@+id/btn_three"
android:width="0dp"
android:height="@dimen/button_height_size"
android:layout_columnWeight="1"
android:gravity="center"
android:text="3"
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/button_font_size"/>
<Button
android:id="@+id/btn_plus"
android:width="0dp"
android:height="@dimen/button_height_size"
android:layout_columnWeight="1"
android:gravity="center"
android:text="+"
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/button_font_size"/>
<Button
android:id="@+id/btn_doublezero"
android:width="0dp"
android:height="@dimen/button_height_size"
android:layout_columnWeight="1"
android:gravity="center"
android:text="00"
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/button_font_size"/>
<Button
android:id="@+id/btn_zero"
android:width="0dp"
android:height="@dimen/button_height_size"
android:layout_columnWeight="1"
android:gravity="center"
android:text="0"
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/button_font_size"/>
<Button
android:id="@+id/btn_point"
android:width="0dp"
android:height="@dimen/button_height_size"
android:layout_columnWeight="1"
android:gravity="center"
android:text="."
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/button_font_size"/>
<Button
android:id="@+id/btn_equal"
android:width="0dp"
android:height="@dimen/button_height_size"
android:layout_columnWeight="1"
android:gravity="center"
android:text="="
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/button_font_size"/>
</GridLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
主要逻辑代码如下:
package com.njyd.calculatoractivity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv_result;
//第一个操作数
private String firstNum = "";
//运算符
private String operator = "";
//第二个操作数
private String secondNum = "";
//当前的计算结果
private String result = "";
//显示的文本内容
private String showText = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
//获取布局文件中的组件
tv_result = findViewById(R.id.tv_result);
findViewById(R.id.btn_cancel).setOnClickListener(this);
findViewById(R.id.btn_percentage).setOnClickListener(this);
findViewById(R.id.btn_backspace).setOnClickListener(this);
findViewById(R.id.btn_divider).setOnClickListener(this);
findViewById(R.id.btn_seven).setOnClickListener(this);
findViewById(R.id.btn_eight).setOnClickListener(this);
findViewById(R.id.btn_nine).setOnClickListener(this);
findViewById(R.id.btn_multiply).setOnClickListener(this);
findViewById(R.id.btn_four).setOnClickListener(this);
findViewById(R.id.btn_five).setOnClickListener(this);
findViewById(R.id.btn_six).setOnClickListener(this);
findViewById(R.id.btn_minus).setOnClickListener(this);
findViewById(R.id.btn_one).setOnClickListener(this);
findViewById(R.id.btn_two).setOnClickListener(this);
findViewById(R.id.btn_three).setOnClickListener(this);
findViewById(R.id.btn_plus).setOnClickListener(this);
findViewById(R.id.btn_doublezero).setOnClickListener(this);
findViewById(R.id.btn_zero).setOnClickListener(this);
findViewById(R.id.btn_point).setOnClickListener(this);
findViewById(R.id.btn_equal).setOnClickListener(this);
}
@Override
public void onClick(View v) {
String inputText;
if (v.getId() == R.id.btn_backspace){
inputText = "X";
}else{
inputText = ((TextView) v).getText().toString();
}
switch (v.getId()) {
//点击了退格按钮
case R.id.btn_backspace:
if (operator.equals("")) {
firstNum = String.valueOf((int)(Double.parseDouble(firstNum) / 10));
updateText(firstNum);
}else if(secondNum ==""){
operator = "";
updateText(firstNum + operator + secondNum);
}else {
secondNum = String.valueOf((int)(Double.parseDouble(firstNum) / 10));
updateText(firstNum + operator + secondNum);
}
break;
//点击了清除按钮
case R.id.btn_cancel:
clear();
break;
//点击了加减乘除
case R.id.btn_plus:
case R.id.btn_minus:
case R.id.btn_multiply:
case R.id.btn_divider:
operator = inputText;//运算符
updateText(showText + operator);
break;
//点击了等号按钮
case R.id.btn_equal:
//加减乘除四则运算
double calculate_result = calculate();
updateOperate(String.valueOf(calculate_result));
updateText(showText + "=" + result);
break;
//点击了百分号按钮
case R.id.btn_percentage:
if (operator.equals("")) {
firstNum = String.valueOf(Double.parseDouble(firstNum) * 0.01);
updateText(firstNum);
} else {
//有运算符,则继续拼接第二个操作数
secondNum = String.valueOf(Double.parseDouble(secondNum) * 0.01);
updateText(firstNum + operator + secondNum);
}
break;
//点击了其他按钮
default:
//上次的运算结果已经出来了
if (result.length() > 0 && operator.equals("")){
clear();
}
//无运算符,则继续拼接第一个操作数
if (operator.equals("")) {
firstNum = firstNum + inputText;
} else {
//有运算符,则继续拼接第二个操作数
secondNum = secondNum + inputText;
}
//整数前不需要0
if (showText.equals("0") && !inputText.equals(".")){
updateText(inputText);
}else {
updateText(showText + inputText);
}
break;
}
}
private double calculate() {
switch (operator){
case "+":
return Double.parseDouble(firstNum) + Double.parseDouble(secondNum);
case "-":
return Double.parseDouble(firstNum) - Double.parseDouble(secondNum);
case "*":
return Double.parseDouble(firstNum) * Double.parseDouble(secondNum);
case "/":
return Double.parseDouble(firstNum) / Double.parseDouble(secondNum);
}
return 0;
}
private void clear() {
updateOperate("");
updateText("");
}
//刷新运算结果
private void updateOperate(String new_result){
result = new_result;
firstNum = result;
secondNum = "";
operator = "";
}
//刷新文本显示
private void updateText(String text){
showText = text;
tv_result.setText(showText);
}
}