开发工具
Android Studio
开发过程
1.使用Android Studio新建一个空项目。
2.创建布局
长下面这样(不太好看😁)

布局.png
布局代码
spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="assembly" cid="n27" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: "Fira Code", Consolas, "Lucida Console", Courier, monospace, "Helvetica Neue", Helvetica, "Arial sans-serif"; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(40, 44, 52); position: relative !important; padding: 6px 10px; box-shadow: rgba(0, 0, 0, 0.16) 0px 2px 5px 0px, rgba(0, 0, 0, 0.12) 0px 2px 10px 0px; margin-bottom: 2.10526rem; border: none; border-radius: 6px; width: inherit; color: rgb(40, 44, 52); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<TextView
android:id="@+id/tv_equation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textAlignment="center"
android:layout_marginTop="20dp"
android:text="@string/equation"
android:textColor="#000"
android:textSize="40sp" />
<LinearLayout
android:id="@+id/linearlayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_below="@+id/tv_equation"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="#ffffff">
<TextView
android:id="@+id/tv_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="a:"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:textColor="#000"
android:textSize="40sp"/>
<EditText
android:id="@+id/ev_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入参数a的值"
android:inputType="numberSigned"
android:background="@null"
android:padding="10dp"
android:textSize="25sp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/linearlayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_below="@+id/linearlayout1"
android:layout_marginLeft="10dp"
android:layout_marginTop="1dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="#ffffff">
<TextView
android:id="@+id/tv_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="b:"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:textColor="#000"
android:textSize="40sp"/>
<EditText
android:id="@+id/ev_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入参数b的值"
android:inputType="numberSigned"
android:background="@null"
android:padding="10dp"
android:textSize="25sp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/linearlayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_below="@+id/linearlayout2"
android:layout_marginLeft="10dp"
android:layout_marginTop="1dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="#ffffff">
<TextView
android:id="@+id/tv_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="c:"
android:layout_marginTop="1dp"
android:textColor="#000"
android:textSize="40sp"/>
<EditText
android:id="@+id/ev_c"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入参数c的值"
android:inputType="numberSigned"
android:background="@null"
android:padding="10dp"
android:textSize="25sp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/linearlayout4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_below="@+id/linearlayout3"
android:layout_marginLeft="10dp"
android:layout_marginTop="1dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="#ffffff">
<TextView
android:id="@+id/tv_answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="答案:"
android:textColor="#000"
android:textSize="25sp"/>
<TextView
android:id="@+id/ev_answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:padding="10dp"
android:textSize="25sp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/linearlayout5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_below="@+id/linearlayout4"
android:layout_marginLeft="10dp"
android:layout_marginTop="1dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="#ffffff">
<Button
android:id="@+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Clear"
android:textSize="35dp"
android:layout_marginTop="10dp"
tools:ignore="InvalidId"
android:layout_weight="1"/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="求解"
android:textSize="40dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
tools:ignore="InvalidId"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
3.Java代码的编写
MainActivity.java 代码:
mdtype="fences" cid="n38" lang="java" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: "Fira Code", Consolas, "Lucida Console", Courier, monospace, "Helvetica Neue", Helvetica, "Arial sans-serif"; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(40, 44, 52); position: relative !important; padding: 6px 10px; box-shadow: rgba(0, 0, 0, 0.16) 0px 2px 5px 0px, rgba(0, 0, 0, 0.12) 0px 2px 10px 0px; margin-bottom: 2.10526rem; border: none; border-radius: 6px; width: inherit; color: rgb(40, 44, 52); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.calculator.utils.CalculateUtils;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
EditText aValue=(EditText)findViewById(R.id.ev_a);
EditText bValue=(EditText)findViewById(R.id.ev_b);
EditText cValue=(EditText)findViewById(R.id.ev_c);
TextView answer_view=(TextView)findViewById(R.id.ev_answer);
Button btn1=(Button)findViewById(R.id.btn1);
Button btn_clear=(Button)findViewById(R.id.btn_clear);
//点击求解
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CalculateUtils.calculate(aValue, bValue, cValue, answer_view);
}
});
//点击Clear
btn_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
aValue.setText("");
bValue.setText("");
cValue.setText("");
answer_view.setText("");
}
});
}
}
CalculateUtils.java 代码:
<pre mdtype="fences" cid="n51" lang="java" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: "Fira Code", Consolas, "Lucida Console", Courier, monospace, "Helvetica Neue", Helvetica, "Arial sans-serif"; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(40, 44, 52); position: relative !important; padding: 6px 10px; box-shadow: rgba(0, 0, 0, 0.16) 0px 2px 5px 0px, rgba(0, 0, 0, 0.12) 0px 2px 10px 0px; margin-bottom: 2.10526rem; border: none; border-radius: 6px; width: inherit; color: rgb(40, 44, 52); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">package com.example.calculator.utils;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
public class CalculateUtils {
private static final String TAG = "KING";
public static void calculate(EditText aValue, EditText bValue, EditText cValue, TextView answer_view) {
double a, b, c;
a = b = c = 0;
if(aValue.getText().length() > 0)
a=Double.parseDouble(aValue.getText().toString());
if(bValue.getText().length() > 0)
b=Double.parseDouble(bValue.getText().toString());
if(cValue.getText().length() > 0)
c=Double.parseDouble(cValue.getText().toString());
if(a == 0){
if (b != 0){
answer_view.setText("x="+(-c/b));
} else answer_view.setText("嘿嘿,爷不会\uD83E\uDD23\uD83E\uDD23");
} else {
double x1, x2;
double delta = b * b - 4 * a * c;
if (delta < 0) {
answer_view.setText("该方程无实数根");
} else if (delta == 0) {
x1 = (-b) / (2 * a);
answer_view.setText("该方程有一个实数根:x=" + x1);
} else {
x1 = ((-b + Math.sqrt(delta)) / (2 * a));
x2 = ((-b - Math.sqrt(delta)) / (2 * a));
answer_view.setText("该方程有两个实数根:x1=" + x1 + "\t\t" + "x2=" + x2);
}
}
}
}
4.给APP换一个图标
找一张图片,放进资源文件夹。
配置 AndroidManifest.xml 文件,在 android:icon 后添加图片名称。
android:icon="@mipmap/calculator_icon"
成果
做完后大概就是这样的
图标:
图标.jpg

界面.jpg