工具:android studio
纯手写,简单语法。
布局用网格布局:
功能实现:
package com.example.jisuanji;
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 {
Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, badd, bless, bcheng, bchu, bdian, bdengyu,bclean;
TextView txt;
String str="",str1="";
double num1,num2;
double result;
int op;
String opd="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.one);
b2 = (Button) findViewById(R.id.two);
b3 = (Button) findViewById(R.id.three);
b4 = (Button) findViewById(R.id.four);
b5 = (Button) findViewById(R.id.five);
b6 = (Button) findViewById(R.id.six);
b7 = (Button) findViewById(R.id.seven);
b8 = (Button) findViewById(R.id.eight);
b9 = (Button) findViewById(R.id.nine);
b0 = (Button) findViewById(R.id.zero);
badd = (Button) findViewById(R.id.add);
bless = (Button) findViewById(R.id.less);
bcheng = (Button) findViewById(R.id.cheng);
bchu = (Button) findViewById(R.id.chu);
bdian=(Button)findViewById(R.id.dian);
bdengyu=(Button)findViewById(R.id.dengyu);
bclean=(Button)findViewById(R.id.clean);
txt=(TextView)findViewById(R.id.txt);
b1.setOnClickListener(new click1());
b2.setOnClickListener(new click2());
b3.setOnClickListener(new click3());
b4.setOnClickListener(new click4());
b5.setOnClickListener(new click5());
b6.setOnClickListener(new click6());
b7.setOnClickListener(new click7());
b8.setOnClickListener(new click8());
b9.setOnClickListener(new click9());
b0.setOnClickListener(new click0());
badd.setOnClickListener(new clickadd());
bless.setOnClickListener(new clickless());
bcheng.setOnClickListener(new clickcheng());
bchu.setOnClickListener(new clickchu());
bdengyu.setOnClickListener(new clickdengyu());
bdian.setOnClickListener(new clickdian());
bclean.setOnClickListener(new clickclean());
}
class clickclean implements View.OnClickListener{
public void onClick(View v)
{
txt.setText("0");
str="";
}
}
class click1 implements View.OnClickListener
{
public void onClick(View v)
{
str += "1";
str1=str;
txt.setText(str1);
}
}
class click2 implements View.OnClickListener
{
public void onClick(View v)
{
str += "2";str1=str;txt.setText(str1);
}
}
class click3 implements View.OnClickListener
{
public void onClick(View v)
{
str += "3";str1=str;txt.setText(str1);
}
}
class click4 implements View.OnClickListener
{
public void onClick(View v)
{
str=str+"4";str1=str;txt.setText(str1);
}
}
class click5 implements View.OnClickListener
{
public void onClick(View v)
{
str=str+"5";str1=str;txt.setText(str1);
}
}
class click6 implements View.OnClickListener
{
public void onClick(View v)
{
str=str+"6";str1=str;txt.setText(str1);
}
}
class click7 implements View.OnClickListener
{
public void onClick(View v)
{
str=str+"7";str1=str;txt.setText(str1);
}
}
class click8 implements View.OnClickListener
{
public void onClick(View v)
{
str=str+"8";str1=str;txt.setText(str1);
}
}
class click9 implements View.OnClickListener
{
public void onClick(View v)
{
str=str+"9";str1=str;txt.setText(str1);
}
}
class click0 implements View.OnClickListener
{
public void onClick(View v)
{
str=str+"0";str1=str;txt.setText(str1);
}
}
class clickadd implements View.OnClickListener
{
public void onClick(View v)
{
op=1;
num1=Double.parseDouble(str);
str1=str+"+";
str="";
txt.setText(str1);
}
}
class clickless implements View.OnClickListener
{
public void onClick(View v)
{
op=2;
num1=Double.parseDouble(str);
str1=str+"-";
str="";txt.setText(str1);
}
}
class clickcheng implements View.OnClickListener
{
public void onClick(View v)
{
op=3;
num1=Double.parseDouble(str);
str1=str+"*";
str="";txt.setText(str1);
}
}
class clickchu implements View.OnClickListener
{
public void onClick(View v)
{
op=4;
num1=Double.parseDouble(str);
str1=str+"/";
str="";
txt.setText(str1);
}
}
class clickdengyu implements View.OnClickListener
{
public void onClick(View v)
{
if(op==1){
num2=Double.parseDouble(str);
result=num1+num2;
opd=Double.toString(result);
txt.setText(opd);
}
if (op==2){
num2=Double.parseDouble(str);
result=num1-num2;
opd=Double.toString(result);
txt.setText(opd);
}
if (op==3){
num2=Double.parseDouble(str);
result=num1*num2;
opd=Double.toString(result);
txt.setText(opd);
}
if (op==4){
num2=Double.parseDouble(str);
result=num1/num2;
opd=Double.toString(result);
txt.setText(opd);
}
}
}
class clickdian implements View.OnClickListener
{
public void onClick(View v)
{
str=str+".";str1=str;txt.setText(str1);
}
}
}