前端界面(布局文件) :线性布局+组件:Button,TextView,使用权重来控制每个按钮的大小。
java代码(加载界面,执行组件事件):实例化组件,绑定监听事件,处理事件。button的setOnClickListener(按钮的监听事件)
<LinearLayout 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:orientation="vertical">
<TextView
android:id="@+id/txtResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示结果"
android:textSize="30sp"
android:gravity="right"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnClean"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C" />
<Button
android:id="@+id/btnDel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="DEL" />
<Button
android:id="@+id/btnAdd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<Button
android:id="@+id/btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
<Button
android:id="@+id/btnSub"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="-"/>
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4" />
<Button
android:id="@+id/btn5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" />
<Button
android:id="@+id/btn6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6" />
<Button
android:id="@+id/btnMul"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="*"/>
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
<Button
android:id="@+id/btn8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8" />
<Button
android:id="@+id/btn9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"
android:visibility="visible" />
<Button
android:id="@+id/btnDivide"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="/"/>
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0" />
<Button
android:id="@+id/btnDoc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="." />
<Button
android:id="@+id/btnEqual"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:text="="/>
LinearLayout>
LinearLayout>
package com.example.qq.mycalculatorapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//实例化按钮
private Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9,btnDoc;
private Button btnAdd, btnSub, btnDivide, btnMul, btnEqual,btnDel,btnClean;
private StringBuffer digitalA = new StringBuffer();//第一个数
private StringBuffer digitalB = new StringBuffer();//第二个数
private boolean isFirstNum = true;//判断是第一个数字还是第二个数字
private int operate;//做标记,判断+,-,*,/
private TextView txtResult;//结果
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calc);
//找到相应的按钮,用于绑定
btn0 = (Button) this.findViewById(R.id.btn0);
btn1 = (Button) this.findViewById(R.id.btn1);
btn2 = (Button) this.findViewById(R.id.btn2);
btn3 = (Button) this.findViewById(R.id.btn3);
btn4 = (Button) this.findViewById(R.id.btn4);
btn5 = (Button) this.findViewById(R.id.btn5);
btn6 = (Button) this.findViewById(R.id.btn6);
btn7 = (Button) this.findViewById(R.id.btn7);
btn8 = (Button) this.findViewById(R.id.btn8);
btn9 = (Button) this.findViewById(R.id.btn9);
btnDoc=(Button)this.findViewById(R.id.btnDoc);
txtResult = (TextView) this.findViewById(R.id.txtResult);
btnAdd = (Button) this.findViewById(R.id.btnAdd);
btnSub = (Button) this.findViewById(R.id.btnSub);
btnMul = (Button) this.findViewById(R.id.btnMul);
btnDivide = (Button) this.findViewById(R.id.btnDivide);
btnDel = (Button) this.findViewById(R.id.btnDel);
btnClean = (Button) this.findViewById(R.id.btnClean);
btnEqual=(Button)this.findViewById(R.id.btnEqual);
//绑定数字按钮,事件监听器
btn0.setOnClickListener(this);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
btnDoc.setOnClickListener(this);
//绑定符号按钮,事件监听器(+,-,*,/,=,C,Del)
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(digitalA.length()==0){
//没有数字输入,就不需要做加法
return;
}
operate = 0;
txtResult.setText("+");
isFirstNum = false;
}
});
btnSub.setOnClickListener((v) -> {
if(digitalA.length()==0){
return;
}
operate = 1;
txtResult.setText("-");
isFirstNum = false;
});
btnMul.setOnClickListener((v) -> {
if(digitalA.length()==0){
return;
}
operate = 2;
txtResult.setText("*");
isFirstNum = false;
});
btnDivide.setOnClickListener((v) -> {
if(digitalA.length()==0){
return;
}
operate = 3;
txtResult.setText("/");
isFirstNum = false;
});
btnEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(digitalA.length()==0){
return;
}
float a = Float.parseFloat(digitalA.toString());
float b = Float.parseFloat(digitalB.toString());
float c = 0;
switch (operate) {
case 0:
c = a + b;
txtResult.setText(c + "");
break;
case 1:
c = a - b;
txtResult.setText(c + "");
break;
case 2:
c = a * b;
txtResult.setText(c + "");
break;
case 3:
c = a / b;
txtResult.setText(c + "");
break;
}
isFirstNum = true;
digitalA = new StringBuffer();
digitalB = new StringBuffer();
}
});
btnClean.setOnClickListener((v) -> {
if(digitalA.length()>=0&&isFirstNum){
digitalA=new StringBuffer();
txtResult.setText("0");
}
else if(digitalB.length()>=0&&!isFirstNum){
digitalA=new StringBuffer();
digitalB=new StringBuffer();
txtResult.setText("0");
}
isFirstNum = true;
digitalA = new StringBuffer();
digitalB = new StringBuffer();
});
btnDel.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(digitalA.length()>0&&isFirstNum){
String temp=digitalA.substring(0,digitalA.length()-1);//取子串,即删除最后一位
digitalA=new StringBuffer();
digitalA.append(temp);
txtResult.setText(digitalA.toString());
}
else if(digitalA.length()==0&&isFirstNum){
//没有输入按删除,返回0
digitalA=new StringBuffer();
txtResult.setText("0");
}
else if(digitalB.length()>0&&!isFirstNum){
String temp=digitalB.substring(0,digitalB.length()-1);//取子串,即删除最后一位
digitalB=new StringBuffer();
digitalB.append(temp);
txtResult.setText(digitalB.toString());
}
else if(digitalB.length()==0&&!isFirstNum){
//没有输入按删除,返回0
digitalB=new StringBuffer();
txtResult.setText("0");
}
}
});
}
//数字的显示
public void onClick(View v) {
//String str = txtResult.toString();
switch (v.getId()) {
case R.id.btn0:
if(isFirstNum) {
digitalA.append("0");
txtResult.setText(digitalA);
}else {
digitalB.append("0");
txtResult.setText(digitalB);
}
break;
case R.id.btn1:
if(isFirstNum) {
digitalA.append("1");
txtResult.setText(digitalA);
}else {
digitalB.append("1");
txtResult.setText(digitalB);
}
break;
case R.id.btn2:
if(isFirstNum) {
digitalA.append("2");
txtResult.setText(digitalA);
}else {
digitalB.append("2");
txtResult.setText(digitalB);
}
break;
case R.id.btn3:
if(isFirstNum) {
digitalA.append("3");
txtResult.setText(digitalA);
}else {
digitalB.append("3");
txtResult.setText(digitalB);
}
break;
case R.id.btn4:
if(isFirstNum) {
digitalA.append("4");
txtResult.setText(digitalA);
}else {
digitalB.append("4");
txtResult.setText(digitalB);
}
break;
case R.id.btn5:
if(isFirstNum) {
digitalA.append("5");
txtResult.setText(digitalA);
}else {
digitalB.append("5");
txtResult.setText(digitalB);
}
break;
case R.id.btn6:
if(isFirstNum) {
digitalA.append("6");
txtResult.setText(digitalA);
}else {
digitalB.append("6");
txtResult.setText(digitalB);
}
break;
case R.id.btn7:
if(isFirstNum) {
digitalA.append("7");
txtResult.setText(digitalA);
}else {
digitalB.append("7");
txtResult.setText(digitalB);
}
break;
case R.id.btn8:
if(isFirstNum) {
digitalA.append("8");
txtResult.setText(digitalA);
}else {
digitalB.append("8");
txtResult.setText(digitalB);
}
break;
case R.id.btn9:
if(isFirstNum) {
digitalA.append("9");
txtResult.setText(digitalA);
}else {
digitalB.append("9");
txtResult.setText(digitalB);
}
break;
case R.id.btnDoc:
if(isFirstNum) {
digitalA.append(".");
txtResult.setText(digitalA);
}else {
digitalB.append(".");
txtResult.setText(digitalB);
}
break;
}
}
}