package com.hanqi.jisuanqi;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button a1;
Button a2;
Button a3;
Button a4;
Button a5;
Button a6;
Button a7;
Button a8;
Button a9;
Button a0;
Button xiao;
Button gui;
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button dian;
TextView et;
//str==null !=str.length()==0
//s6为何无效
//s1存储数据
double s1 = 0;
//判断运算
int s2 = 0;
//承载连续运算数字
double s3 = 0;
//显示结果
double s4;
//运算完成后使用结果直接下次运算
int s5=0;
//确认小数点输入一次
int s6=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jisuan);
et = (TextView) findViewById(R.id.wenjian);
a1 = (Button) findViewById(R.id.aa1);
a2 = (Button) findViewById(R.id.aa2);
a3 = (Button) findViewById(R.id.a3);
a4 = (Button) findViewById(R.id.a4);
a5 = (Button) findViewById(R.id.a5);
a6 = (Button) findViewById(R.id.a6);
a7 = (Button) findViewById(R.id.a7);
a8 = (Button) findViewById(R.id.a8);
a9 = (Button) findViewById(R.id.a9);
a0 = (Button) findViewById(R.id.a0);
b1 = (Button) findViewById(R.id.jia);
b2 = (Button) findViewById(R.id.jian);
b3 = (Button) findViewById(R.id.cheng);
b4 = (Button) findViewById(R.id.chu);
b5 = (Button) findViewById(R.id.deng);
xiao = (Button) findViewById(R.id.xiao);
gui = (Button) findViewById(R.id.gui);
dian = (Button) findViewById(R.id.dian);
a1.setOnClickListener(new shuzi());
a2.setOnClickListener(shu);
a3.setOnClickListener(shu);
a4.setOnClickListener(shu);
a5.setOnClickListener(shu);
a6.setOnClickListener(shu);
a7.setOnClickListener(shu);
a8.setOnClickListener(shu);
a9.setOnClickListener(shu);
a0.setOnClickListener(shu);
b1.setOnClickListener(jia1);
b2.setOnClickListener(jian1);
b3.setOnClickListener(cheng1);
b4.setOnClickListener(chu1);
b5.setOnClickListener(deng1);
dian.setOnClickListener(dian1);
xiao.setOnClickListener(xiao1);
gui.setOnClickListener(gui1);
}
shuzi shu = new shuzi();
jia jia1=new jia();
jian jian1=new jian();
cheng cheng1=new cheng();
chu chu1=new chu();
deng deng1=new deng();
xiao xiao1=new xiao();
gui gui1=new gui();
dian dian1=new dian();
class shuzi implements View.OnClickListener {
public void onClick(View v) {
if (s5 == 1) {
et.setText(null);
s1 = 0;
s2 = 0;
s3 = 0;
s4 = 0;
s5 = 0;
}
Button b = (Button) v;
String str = et.getText().toString();
String str2 = b.getText().toString();
str += str2;
et.setText(str);
}
}
class dian implements View.OnClickListener {
public void onClick(View v) {
if(s5==1){et.setText(null);
s1=0;
s2=0;
s3=0;
s4=0;
s5=0;}
if(s6==1){
Toast.makeText(MainActivity.this, "好好输入", Toast.LENGTH_SHORT).show();
return;}
Button b = (Button) v;
String str = et.getText().toString();
if(str.length()==0||str.equals("0.")){
et.setText("0.");
Toast.makeText(MainActivity.this, "111", Toast.LENGTH_SHORT).show();
return;
}
String str2 = b.getText().toString();
str += str2;
et.setText(str);
s6=1;
}
}
class jia implements View.OnClickListener {
public void onClick(View v) {
s5=0;
Button b = (Button) v;
String str = et.getText().toString();
if (str.length()==0) {
return;
}
if (s1 == 0) {
s1 = Double.parseDouble(str);
} else {
s3 = Double.parseDouble(str);
if (s2 == 0) {
s1 = s3;
}
if (s2 == 1) {
s1 += s3;
}
if (s2 == 2) {
s1 -= s3;
}
if (s2 == 3) {
s1 *= s3;
}
if (s2==4&&s3!=0) {
s1 /= s3;
}
if (s2==4&&s3==0) {
Toast.makeText(MainActivity.this, "全部清除重新输入", Toast.LENGTH_SHORT).show();
s1=0;
s2=0;
s3=0;
s4=0;
s5=0;
}
}
et.setText(null);
s2 = 1;
s6=0;
Toast.makeText(MainActivity.this, ("s1="+s1+" s2="+s2+" s3="+s3), Toast.LENGTH_SHORT).show();
}
}
class jian implements View.OnClickListener {
public void onClick(View v) {
s5=0;
Button b = (Button) v;
String str = et.getText().toString();
if (str.length()==0) {
return;
}
if (s1 == 0) {
s1 =Double.parseDouble(str);
} else {
s3 = Double.parseDouble(str);
if(s2==0){s1=s3;}
if(s2==1){s1+=s3;}
if(s2==2){s1-=s3;}
if(s2==3){s1*=s3;}
if(s2==4&&s3!=0){s1/=s3;}
if (s2==4&&s3==0) {
Toast.makeText(MainActivity.this, "全部清除重新输入", Toast.LENGTH_SHORT).show();
s1=0;
s2=0;
s3=0;
s4=0;
s5=0;
}
}
s2 = 2;
s6=0;
et.setText(null);
Toast.makeText(MainActivity.this, ("s1="+s1+" s2="+s2+" s3="+s3), Toast.LENGTH_SHORT).show();
}
}
class cheng implements View.OnClickListener {
public void onClick(View v) {
s5=0;
Button b = (Button) v;
String str = et.getText().toString();
if (str.length()==0) {
return;
}
if (s1 == 0) {
s1 = Double.parseDouble(str);
} else {
s3 = Double.parseDouble(str);
if(s2==0){s1=s3;}
if(s2==1){s1+=s3;}
if(s2==2){s1-=s3;}
if(s2==3){s1*=s3;}
if(s2==4&&s3!=0){s1/=s3;}
if (s2==4&&s3==0) {
Toast.makeText(MainActivity.this, "全部清除重新输入", Toast.LENGTH_SHORT).show();
s1=0;
s2=0;
s3=0;
s4=0;
s5=0;
}
}
s2 = 3;
s6=0;
et.setText(null);
Toast.makeText(MainActivity.this, ("s1="+s1+" s2="+s2+" s3="+s3), Toast.LENGTH_SHORT).show();
}
}
class chu implements View.OnClickListener {
public void onClick(View v) {
s5=0;
Button b = (Button) v;
String str = et.getText().toString();
if (str.length()==0) {
return;
}
if (s1 == 0) {
s1 = Double.parseDouble(str);
} else {
s3 = Double.parseDouble(str);
if(s2==0){s1=s3;}
if(s2==1){s1+=s3;}
if(s2==2){s1-=s3;}
if(s2==3){s1*=s3;}
if(s2==4&&s3!=0){s1/=s3;}
if (s2==4&&s3==0) {
Toast.makeText(MainActivity.this, "全部清除重新输入", Toast.LENGTH_SHORT).show();
s1=0;
s2=0;
s3=0;
s4=0;
s5=0;
}
}
s2 = 4;
s6=0;
et.setText(null);
Toast.makeText(MainActivity.this, ("s1="+s1+" s2="+s2+" s3="+s3), Toast.LENGTH_SHORT).show();
}
}
class deng implements View.OnClickListener {
public void onClick(View v) {
String str = et.getText().toString();
if(str.length()==0){return;}
s3 = Double.parseDouble(str);
if(s3==0&&s2==4){
Toast.makeText(MainActivity.this, "逗逼", Toast.LENGTH_SHORT).show();
return;
}
if(s2==0){s4=s3;}
if(s2==1){s4=s1+s3;}
if(s2==2){s4=s1-s3;}
if(s2==3){s4=s1*s3;}
if(s2==4&&s3!=0){s4=s1/s3;}
if (s2==4&&s3==0) {
Toast.makeText(MainActivity.this, "全部清除重新输入", Toast.LENGTH_SHORT).show();
s1=0;
s2=0;
s3=0;
s4=0;
s5=0;
}
String str2=s4+"";
et.setText(str2);
s5=1;
s2=0;
s6=0;
Toast.makeText(MainActivity.this, ("s1="+s1+" s2="+s2+" s3="+s3), Toast.LENGTH_SHORT).show();
}
}
class gui implements View.OnClickListener {
public void onClick(View v) {
s1=0;
s2=0;
s3=0;
s5=0;
s4=0;
s6=0;
et.setText(null);
}
}
class xiao implements View.OnClickListener {
public void onClick(View v) {
String str=et.getText().toString();
if(str.length()==0){
return;
}
String str2=str.substring(0,str.length()-1);
et.setText(str2);
if(s5==1){
s5=0;
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="6"
android:columnCount="4">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_columnSpan="4"
android:layout_rowWeight="15"
android:hint="0"
android:gravity="bottom|right"
android:textSize="30dp"
android:id="@+id/wenjian"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="C"
android:textSize="30dp"
android:id="@+id/xiao"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="◀"
android:textSize="30dp"
android:id="@+id/gui"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="÷"
android:textSize="30dp"
android:id="@+id/chu"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="×"
android:textSize="30dp"
android:id="@+id/cheng"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="1"
android:textSize="30dp"
android:id="@+id/aa1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="2"
android:textSize="30dp"
android:id="@+id/aa2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="3"
android:textSize="30dp"
android:id="@+id/a3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="-"
android:textSize="30dp"
android:id="@+id/jian"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="4"
android:textSize="30dp"
android:id="@+id/a4"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="5"
android:textSize="30dp"
android:id="@+id/a5"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="6"
android:textSize="30dp"
android:id="@+id/a6"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="+"
android:textSize="30dp"
android:id="@+id/jia"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="7"
android:textSize="30dp"
android:id="@+id/a7"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="8"
android:textSize="30dp"
android:id="@+id/a8"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="9"
android:textSize="30dp"
android:id="@+id/a9"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_rowSpan="2"
android:text="="
android:textSize="30dp"
android:id="@+id/deng"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_columnSpan="2"
android:text="0"
android:textSize="30dp"
android:id="@+id/a0"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="."
android:textSize="30dp"
android:id="@+id/dian"/>
</GridLayout>