android studio 简易计算器制作
Posted bertin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android studio 简易计算器制作相关的知识,希望对你有一定的参考价值。
只是记录一下代码,随意取用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal">
</LinearLayout>
<!--写一个计算器的layout-->
<TextView
android:id="@+id/tv_result"
android:layout_
android:layout_
android:layout_margin="10dp"
android:background="@color/white"
android:gravity="right"
android:padding="10dp"
android:text=" "
android:textColor="@color/black"
android:textSize="30sp"
tools:ignore="DuplicateIds" />
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal">
<Button
android:id="@+id/btn_clear"
android:layout_
android:layout_
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="C"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_del"
android:layout_
android:layout_
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@color/white"
android:text="Del"
android:textColor="@color/white"
android:textSize="20sp" />
<Button
android:id="@+id/btn_div"
android:layout_
android:layout_
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="/"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_mul"
android:layout_
android:layout_
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="*"
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
<!--计算器的数字按键-->
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal">
<Button
android:id="@+id/btn_7"
android:layout_
android:layout_
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="7"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_8"
android:layout_
android:layout_
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="8"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_9"
android:layout_
android:layout_
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="9"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_sub"
android:layout_
android:layout_
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="-"
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal">
<Button
android:id="@+id/btn_4"
android:layout_
android:layout_
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="4"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_5"
android:layout_
android:layout_
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="5"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_6"
android:layout_
android:layout_
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="6"
android:textColor="@color/white"
android:textSize="30sp" />
<!-- 补充符号-->
<Button
android:id="@+id/btn_add"
android:layout_
android:layout_
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="+"
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal">
<Button
android:id="@+id/btn_1"
android:layout_
android:layout_
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="1"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_2"
android:layout_
android:layout_
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="2"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_3"
android:layout_
android:layout_
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="3"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_equal"
android:layout_
android:layout_
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="="
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
<!-- 百分号,0,小数点按键-->
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal">
<Button
android:id="@+id/btn_percent"
android:layout_
android:layout_
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="%"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_0"
android:layout_
android:layout_
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="0"
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_point"
android:layout_
android:layout_
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="."
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btn_none"
android:layout_
android:layout_
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/white"
android:text="B"
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
然后是MainActivity.java
package com.zdb.hwfrist;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
//导入数据框
import android.widget.Button;
import android.widget.TextView;
import java.util.Stack;
public class MainActivity extends AppCompatActivity
//创建button对象
Button btn_none,btn_0, btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9, btn_percent, btn_del, btn_point, btn_add, btn_sub, btn_mul, btn_div, btn_equal, btn_clear;
//创建TextView对象
TextView tv_result;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 实例化对象
btn_0 = findViewById(R.id.btn_0);
btn_1 = findViewById(R.id.btn_1);
btn_2 = findViewById(R.id.btn_2);
btn_3 = findViewById(R.id.btn_3);
btn_4 = findViewById(R.id.btn_4);
btn_5 = findViewById(R.id.btn_5);
btn_6 = findViewById(R.id.btn_6);
btn_7 = findViewById(R.id.btn_7);
btn_8 = findViewById(R.id.btn_8);
btn_9 = findViewById(R.id.btn_9);
btn_percent = findViewById(R.id.btn_percent);
btn_add = findViewById(R.id.btn_add);
btn_sub = findViewById(R.id.btn_sub);
btn_mul = findViewById(R.id.btn_mul);
btn_div = findViewById(R.id.btn_div);
btn_del = findViewById(R.id.btn_del);
btn_equal = findViewById(R.id.btn_equal);
btn_point = findViewById(R.id.btn_point);
btn_clear = findViewById(R.id.btn_clear);
tv_result = findViewById(R.id.tv_result);
btn_none = findViewById(R.id.btn_none);
// 给按钮添加点击事件
btn_0.setOnClickListener(v ->
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "0");
);
btn_1.setOnClickListener(v ->
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "1");
);
btn_2.setOnClickListener(v ->
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "2");
);
btn_3.setOnClickListener(v ->
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "3");
);
btn_4.setOnClickListener(v ->
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "4");
);
btn_5.setOnClickListener(v ->
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "5");
);
btn_6.setOnClickListener(v ->
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "6");
);
btn_7.setOnClickListener(v ->
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "7");
);
btn_8.setOnClickListener(v ->
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "8");
);
btn_9.setOnClickListener(v ->
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "9");
);
btn_point.setOnClickListener(v ->
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + ".");
);
btn_clear.setOnClickListener(v ->
tv_result.setText(" ");
);
btn_none.setOnClickListener(v ->
tv_result.setText("叫你手贱,点我干什么");
);
btn_del.setOnClickListener(v ->
String str_out = tv_result.getText().toString();
if (str_out.length() == 1)
tv_result.setText(" ");
else
tv_result.setText(str_out.substring(0, str_out.length() - 1));
);
btn_add.setOnClickListener(v ->
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "+");
);
btn_sub.setOnClickListener(v ->
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "-");
);
btn_mul.setOnClickListener(v ->
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "*");
);
btn_div.setOnClickListener(v ->
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "/");
);
btn_percent.setOnClickListener(v ->
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "%");
);
btn_equal.setOnClickListener(v ->
String str = tv_result.getText().toString();
// 如果str是中文,就不执行
if (str.contains("叫"))
return;
// 如果str是空的,就不执行
if (str.equals(" "))
return;
// 如果str是以运算符结尾,就不执行
if (str.endsWith("+") || str.endsWith("-") || str.endsWith("*") || str.endsWith("/"))
tv_result.setText(str+"好好写");
return;
str="1*"+str;
// 如果str有%,则在%后面加一个1
if (str.contains("%"))
str = str.replace("%", "%0");
Object result = calculate(str);
System.out.println(result);
// 打印result
System.out.println(result);
// 如果小数点后面是0,就不显示
if (result.toString().endsWith(".0"))
tv_result.setText(result.toString().substring(0, result.toString().length() - 2));
else
tv_result.setText(result + "");
);
public static double calculate(String input)
String[] parts = input.split("(?<=[-+*/%])|(?=[-+*/%])");
Stack<Double> numbers = new Stack<>();
Stack<Character> operators = new Stack<>();
for (String part : parts)
part = part.trim();
if (part.isEmpty())
continue;
char firstChar = part.charAt(0);
if (Character.isDigit(firstChar))
double num = Double.parseDouble(part);
numbers.push(num);
else
while (!operators.isEmpty() && shouldPopOperator(operators.peek(), firstChar))
char op = operators.pop();
double num2 = numbers.pop();
double num1 = numbers.pop();
double result = calculateOneOperation(num1, num2, op);
numbers.push(result);
operators.push(firstChar);
while (!operators.isEmpty())
char op = operators.pop();
double num2 = numbers.pop();
double num1 = numbers.pop();
double result = calculateOneOperation(num1, num2, op);
numbers.push(result);
return numbers.pop();
private static boolean shouldPopOperator(char op1, char op2)
if (op1 == \'(\' || op1 == \')\')
return false;
int precedence1 = getOperatorPrecedence(op1);
int precedence2 = getOperatorPrecedence(op2);
return precedence1 >= precedence2;
private static int getOperatorPrecedence(char op)
switch (op)
case \'+\':
case \'-\':
return 1;
case \'*\':
case \'/\':
return 2;
case \'%\':
return 3;
default:
return 0;
private static double calculateOneOperation(double num1, double num2, char op)
switch (op)
case \'%\':
return num1 * 0.01;
case \'+\':
return num1 + num2;
case \'-\':
return num1 - num2;
case \'*\':
return num1 * num2;
case \'/\':
return num1 / num2;
default:
throw new IllegalArgumentException("Invalid operator: " + op);
本文来自博客园,作者:bertin,转载请注明原文链接:https://www.cnblogs.com/bertin/p/17320806.html
制作一个简易计算器——基于Android Studio实现
一个计算器Android程序的源码部分分为主干和细节两部分。
一、主干
1. 主干的构成
- 计算器的布局
- 事件(即计算器上的按钮、文本框)监听
- 实现计算
2. 详细解释
假设我们的项目名为Calculator,而布局名称(Layout Name)为默认的activity_main 。即设置如下图所示:
在这种前提下,有:
- 设置计算器布局的文件:Calculator/app/src/main/res/layout/activity_main.xml
- 事件监听和计算实现在同一个文件里:Calculator/app/src/main/java/下的一个子目录里的MainActivity.java
即如下图所示:
3. 主干代码部分
- 计算器布局代码(写在activity_main.xml文件里):
1 <?xml version="1.0" encoding="utf-8"?> 2 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="wrap_content" 5 > 6 <LinearLayout 7 android:layout_width="fill_parent" 8 android:layout_height="fill_parent" 9 android:orientation="vertical" > 10 <EditText 11 android:id="@+id/input" 12 android:layout_width="fill_parent" 13 android:layout_height="wrap_content" 14 android:layout_gravity="center" 15 android:editable="false" 16 android:hint="@string/shuru" /> 17 18 <EditText 19 android:id="@+id/output" 20 android:layout_width="fill_parent" 21 android:layout_height="wrap_content" 22 android:layout_gravity="center" 23 android:editable="true" 24 android:gravity="right" 25 android:hint="@string/shuchu" /> 26 27 <RelativeLayout 28 android:layout_width="fill_parent" 29 android:layout_height="wrap_content" > 30 31 <Button 32 android:id="@+id/seven" 33 android:layout_width="80dp" 34 android:layout_height="70dp" 35 android:layout_alignParentLeft="true" 36 android:text="@string/seven" 37 android:textSize="40sp" /> 38 39 <Button 40 android:id="@+id/eight" 41 android:layout_width="80dp" 42 android:layout_height="70dp" 43 android:layout_toRightOf="@id/seven" 44 android:text="@string/eight" 45 android:textSize="40sp" /> 46 47 <Button 48 android:id="@+id/nine" 49 android:layout_width="80dp" 50 android:layout_height="70dp" 51 android:layout_toRightOf="@id/eight" 52 android:text="@string/nine" 53 android:textSize="40sp" /> 54 55 <Button 56 android:id="@+id/add" 57 android:layout_width="80dp" 58 android:layout_height="70dp" 59 android:layout_alignParentRight="true" 60 android:layout_toRightOf="@id/nine" 61 android:text="@string/add" 62 android:textSize="40sp" /> 63 64 <Button 65 android:id="@+id/four" 66 android:layout_width="80dp" 67 android:layout_height="70dp" 68 android:layout_alignParentLeft="true" 69 android:layout_below="@id/seven" 70 android:text="@string/four" 71 android:textSize="40sp" /> 72 73 <Button 74 android:id="@+id/five" 75 android:layout_width="80dp" 76 android:layout_height="70dp" 77 android:layout_below="@id/eight" 78 android:layout_toRightOf="@id/four" 79 android:text="@string/five" 80 android:textSize="40sp" /> 81 82 <Button 83 android:id="@+id/six" 84 android:layout_width="80dp" 85 android:layout_height="70dp" 86 android:layout_below="@id/nine" 87 android:layout_toRightOf="@id/five" 88 android:text="@string/six" 89 android:textSize="40sp" /> 90 91 <Button 92 android:id="@+id/subtract" 93 android:layout_width="80dp" 94 android:layout_height="70dp" 95 android:layout_alignParentRight="true" 96 android:layout_below="@id/add" 97 android:layout_toRightOf="@id/six" 98 android:text="@string/subtract" 99 android:textSize="40sp" /> 100 101 <Button 102 android:id="@+id/one" 103 android:layout_width="80dp" 104 android:layout_height="70dp" 105 android:layout_alignParentLeft="true" 106 android:layout_below="@id/four" 107 android:text="@string/one" 108 android:textSize="40sp" /> 109 110 <Button 111 android:id="@+id/two" 112 android:layout_width="80dp" 113 android:layout_height="70dp" 114 android:layout_below="@id/five" 115 android:layout_toRightOf="@id/one" 116 android:text="@string/two" 117 android:textSize="40sp" /> 118 119 <Button 120 android:id="@+id/three" 121 android:layout_width="80dp" 122 android:layout_height="70dp" 123 android:layout_below="@id/six" 124 android:layout_toRightOf="@id/two" 125 android:text="@string/three" 126 android:textSize="40sp" /> 127 128 <Button 129 android:id="@+id/multiply" 130 android:layout_width="80dp" 131 android:layout_height="70dp" 132 android:layout_alignParentRight="true" 133 android:layout_below="@id/subtract" 134 android:layout_toRightOf="@id/three" 135 android:text="@string/multiply" 136 android:textSize="40sp" /> 137 138 <Button 139 android:id="@+id/zero" 140 android:layout_width="80dp" 141 android:layout_height="70dp" 142 android:layout_alignParentLeft="true" 143 android:layout_below="@id/one" 144 android:text="@string/zero" 145 android:textSize="40sp" /> 146 147 <Button 148 android:id="@+id/clear" 149 android:layout_width="80dp" 150 android:layout_height="70dp" 151 android:layout_below="@id/two" 152 android:layout_toRightOf="@id/zero" 153 android:text="@string/clear" 154 android:textSize="40sp" /> 155 156 <Button 157 android:id="@+id/result" 158 android:layout_width="80dp" 159 android:layout_height="70dp" 160 android:layout_below="@id/three" 161 android:layout_toRightOf="@id/clear" 162 android:text="@string/result" 163 android:textSize="40sp" /> 164 165 <Button 166 android:id="@+id/divide" 167 android:layout_width="80dp" 168 android:layout_height="70dp" 169 android:layout_alignParentRight="true" 170 android:layout_below="@id/multiply" 171 android:layout_toRightOf="@id/result" 172 android:text="@string/divide" 173 android:textSize="40sp" /> 174 175 <Button 176 android:id="@+id/dot" 177 android:layout_width="80dp" 178 android:layout_height="70dp" 179 android:layout_alignParentLeft="true" 180 android:layout_below="@id/zero" 181 android:text="@string/dot" 182 android:textSize="40sp" /> 183 <Button 184 android:id="@+id/writeButton" 185 android:layout_width="wrap_content" 186 android:layout_height="wrap_content" 187 android:layout_alignParentLeft="true" 188 android:layout_below="@id/dot" 189 android:text="@string/write" 190 android:textSize="40sp" /> 191 <Button 192 android:id="@+id/readButton" 193 android:layout_width="wrap_content" 194 android:layout_height="wrap_content" 195 android:layout_alignParentRight="true" 196 android:layout_below="@id/dot" 197 android:text="@string/read" 198 android:textSize="40sp" /> 199 200 <CheckBox 201 android:id="@+id/appendBox" 202 android:text="@string/appendBox" 203 android:layout_width="wrap_content" 204 android:layout_height="wrap_content" 205 android:layout_alignParentBottom="true" 206 android:layout_toLeftOf="@+id/divide" 207 android:layout_toStartOf="@+id/divide" 208 android:layout_marginBottom="12dp" 209 /> 210 211 </RelativeLayout> 212 213 <EditText 214 215 android:layout_width="match_parent" 216 android:layout_height="wrap_content" 217 android:id="@+id/textView" /> 218 219 <EditText 220 221 android:layout_width="match_parent" 222 android:layout_height="wrap_content" 223 android:id="@+id/displayView" /> 224 225 <EditText 226 android:id="@+id/errorzero" 227 android:layout_width="fill_parent" 228 android:layout_height="wrap_content" 229 android:layout_gravity="center" 230 android:editable="false" 231 android:gravity="center" 232 /> 233 <EditText 234 android:id="@+id/resultText" 235 android:layout_width="fill_parent" 236 android:layout_height="wrap_content" 237 android:layout_gravity="center" 238 android:editable="false" 239 android:gravity="left" 240 android:text="@string/resultText" 241 /> 242 </LinearLayout> 243 </ScrollView>
- 事件监听和实现计算代码(写在MainActivity.java文件里)
1 package com.example.lenovo.calculator; 2 3 4 5 import android.app.Activity; 6 import android.content.Context; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 import android.widget.CheckBox; 12 import android.widget.EditText; 13 14 import java.io.FileInputStream; 15 import java.io.FileNotFoundException; 16 import java.io.FileOutputStream; 17 import java.io.IOException; 18 19 public class MainActivity extends Activity { 20 /** 21 * Called when the activity is first created. 22 */ 23 private EditText output = null; 24 private EditText input = null; 25 private Button btn0 = null; 26 private Button btn1 = null; 27 private Button btn2 = null; 28 private Button btn3 = null; 29 private Button btn4 = null; 30 private Button btn5 = null; 31 private Button btn6 = null; 32 private Button btn7 = null; 33 private Button btn8 = null; 34 private Button btn9 = null; 35 private Button btnadd = null; 36 private Button btnsubtract = null; 37 private Button btnmultiply = null; 38 private Button btndivide = null; 39 private Button btnclear = null; 40 private Button btnresult = null; 41 private Button btndot = null; 42 43 private EditText errorzero = null; 44 45 private EditText resultText = null; 46 private Button writeButton = null; 47 private Button readButton = null; 48 private CheckBox appendBox = null; 49 private EditText textView = null; 50 private EditText displayView = null; 51 public String FILE_NAME = "fileDemo.txt"; 52 53 54 private String str = "";//保存数字 55 private String strold = "";//原数字 56 private char act = \' \';//记录“加减乘除等于”符号 57 private int count = 0;//判断要计算的次数,如果超过一个符号,先算出来一部分 58 private Float result = null;//计算的输出结果 59 private Boolean errBoolean = false;//有错误的时候为true,无错为false 60 private Boolean flagBoolean = false;//一个标志,如果为true,可以响应运算消息,如果为false,不响应运算消息,只有前面是数字才可以响应运算消息 61 private Boolean flagDot = false; //小数点标志位 62 63 64 65 @Override 66 public void onCreate(Bundle savedInstanceState) { 67 super.onCreate(savedInstanceState); 68 setContentView(R.layout.activity_main); 69 70 output = (EditText) findViewById(R.id.output); 71 input = (EditText) findViewById(R.id.input); 72 73 errorzero = (EditText) findViewById(R.id.errorzero); 74 resultText = (EditText) findViewById(R.id.resultText); 75 writeButton = (Button) findViewById(R.id.writeButton); 76 readButton = (Button) findViewById(R.id.readButton); 77 textView = (EditText) findViewById(R.id.textView); 78 displayView = (EditText) findViewById(R.id.displayView); 79 appendBox = (CheckBox) findViewById(R.id.appendBox); 80 81 btn0 = (Button) findViewById(R.id.zero); 82 btn1 = (Button) findViewById(R.id.one); 83 btn2 = (Button) findViewById(R.id.two); 84 btn3 = (Button) findViewById(R.id.three); 85 btn4 = (Button) findViewById(R.id.four); 86 btn5 = (Button) findViewById(R.id.five); 87 btn6 = (Button) findViewById(R.id.six); 88 btn7 = (Button) findViewById(R.id.seven); 89 btn8 = (Button) findViewById(R.id.eight); 90 btn9 = (Button) findViewById(R.id.nine); 91 btnadd = (Button) findViewById(R.id.add); 92 btnsubtract = (Button) findViewById(R.id.subtract); 93 btnmultiply = (Button) findViewById(R.id.multiply); 94 btndivide = (Button) findViewById(R.id.divide); 95 btnclear = (Button) findViewById(R.id.clear); 96 btnresult = (Button) findViewById(R.id.result); 97 btndot = (Button) findViewById(R.id.dot); 98 //设置按钮侦听事件 99 btn0.setOnClickListener(listener); 100 btn1.setOnClickListener(listener); 101 btn2.setOnClickListener(listener); 102 btn3.setOnClickListener(listener); 103 btn4.setOnClickListener(listener); 104 btn5.setOnClickListener(listener); 105 btn6.setOnClickListener(listener); 106 btn7.setOnClickListener(listener); 107 btn8.setOnClickListener(listen以上是关于android studio 简易计算器制作的主要内容,如果未能解决你的问题,请参考以下文章