工具:android studio
纯手写,简单语法。
布局用网格布局:
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:rowCount="6" android:columnCount="4"> <TextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_columnSpan="4" android:layout_marginLeft="4dp" android:gravity="start" android:text="0" android:textSize="50sp" /> <Button android:id="@+id/clean" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_columnSpan="4" android:text="@string/clean" android:textSize="26sp" /> <Button android:text="1" android:id="@+id/one" android:textSize="26sp"/> <Button android:text="2" android:id="@+id/two" android:textSize="26sp"/> <Button android:text="3" android:id="@+id/three" android:textSize="26sp"/> <Button android:text="+" android:id="@+id/add" android:textSize="26sp"/> <Button android:text="4" android:id="@+id/four" android:textSize="26sp"/> <Button android:text="5" android:id="@+id/five" android:textSize="26sp"/> <Button android:text="6" android:id="@+id/six" android:textSize="26sp"/> <Button android:text="-" android:id="@+id/less" android:textSize="26sp"/> <Button android:text="7" android:id="@+id/seven" android:textSize="26sp"/> <Button android:text="8" android:id="@+id/eight" android:textSize="26sp"/> <Button android:text="9" android:id="@+id/nine" android:textSize="26sp"/> <Button android:text="*" android:id="@+id/cheng" android:textSize="26sp"/> <Button android:text="." android:id="@+id/dian" android:textSize="26sp"/> <Button android:text="0" android:id="@+id/zero" android:textSize="26sp"/> <Button android:text="=" android:id="@+id/dengyu" android:textSize="26sp"/> <Button android:text="/" android:id="@+id/chu" android:textSize="26sp"/> </GridLayout>
功能实现:
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); } } }