xml代码
<?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" > <EditText android:layout_columnSpan="4" android:layout_gravity="fill_horizontal" android:layout_columnWeight="1" android:layout_rowWeight="1" android:editable="false" android:gravity="right|center_vertical" android:id="@+id/et" android:textSize="30sp" /> <Button android:text="清除" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20sp" android:layout_row="1" android:layout_column="1" android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:background="#900" android:id="@+id/clear"/> <Button android:text="后退" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20sp" android:layout_row="1" android:layout_column="0" android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:background="#900" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:id="@+id/goback"/> <Button android:text="/" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20sp" android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:background="#900" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:id="@+id/chu"/> <Button android:text="x" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20sp" android:background="#900" android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:id="@+id/cheng"/> <Button android:text="7" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:textSize="20sp" android:background="#900" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:id="@+id/bt_7"/> <Button android:text="8" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20sp" android:background="#900" android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:id="@+id/bt_8"/> <Button android:text="9" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:textSize="20sp" android:background="#900" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:id="@+id/bt_9"/> <Button android:text="-" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:textSize="20sp" android:background="#900" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:id="@+id/jian"/> <Button android:text="4" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:textSize="20sp" android:background="#900" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:id="@+id/bt_4"/> <Button android:text="5" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:textSize="20sp" android:background="#900" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:id="@+id/bt_5"/> <Button android:text="6" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:textSize="20sp" android:background="#900" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:id="@+id/bt_6"/> <Button android:text="+" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:textSize="20sp" android:background="#900" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:id="@+id/jia"/> <Button android:text="1" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:textSize="20sp" android:background="#900" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:id="@+id/bt_1"/> <Button android:text="2" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:textSize="20sp" android:background="#900" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:id="@+id/bt_2"/> <Button android:text="3" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:textSize="20sp" android:background="#900" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:id="@+id/bt_3"/> <Button android:text="=" android:layout_rowSpan="2" android:layout_gravity="fill_vertical" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:textSize="20sp" android:background="#900" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:id="@+id/deng"/> <Button android:text="0" android:layout_columnSpan="2" android:layout_gravity="fill_horizontal" android:layout_columnWeight="1" android:layout_rowWeight="1" android:background="#900" android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:textSize="20sp" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:id="@+id/bt_0" /> <Button android:text="." android:layout_columnWeight="1" android:layout_rowWeight="1" android:background="#900" android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:textSize="20sp" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:onClick="onClick" android:id="@+id/dian"/> </GridLayout>
Java代码
package com.hanqi.test5; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; /** * Created by Administrator on 2016/3/27. */ public class jisuanqi extends Activity implements View.OnClickListener { EditText et; Button clear; Button goback; Button bt_7; Button bt_8; Button bt_9; Button bt_6; Button bt_1; Button bt_2; Button bt_3; Button bt_4; Button bt_5; Button jia; Button jian; Button cheng; Button chu; Button bt_0; Button dian; Button deng; //存储显示的内容 //操作字符串的工具类 //实例化 private StringBuilder str_show = new StringBuilder(); //当前运算符的 id private int id_yunsf = 0; //运算符前面的数值 包装类 private Double numner_1; //运算符后面的数值 private Double numner_2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gridlayout); et =(EditText)findViewById(R.id.et); clear =(Button)findViewById(R.id.clear); goback =(Button)findViewById(R.id.goback); bt_7 =(Button)findViewById(R.id.bt_7); bt_8 =(Button)findViewById(R.id.bt_8); bt_9 =(Button)findViewById(R.id.bt_9); bt_6 =(Button)findViewById(R.id.bt_6); bt_1 =(Button)findViewById(R.id.bt_1); bt_2 =(Button)findViewById(R.id.bt_2); bt_3 =(Button)findViewById(R.id.bt_3); bt_4 =(Button)findViewById(R.id.bt_4); bt_5 =(Button)findViewById(R.id.bt_5); jia =(Button)findViewById(R.id.jia); jian =(Button)findViewById(R.id.jian); cheng =(Button)findViewById(R.id.cheng); chu =(Button)findViewById(R.id.chu); bt_0 =(Button)findViewById(R.id.bt_0); dian =(Button)findViewById(R.id.dian); deng =(Button)findViewById(R.id.deng); clear.setOnClickListener(this); goback.setOnClickListener(this); bt_0.setOnClickListener(this); bt_1.setOnClickListener(this); bt_2.setOnClickListener(this); bt_3.setOnClickListener(this); bt_4.setOnClickListener(this); bt_5.setOnClickListener(this); bt_6.setOnClickListener(this); bt_7.setOnClickListener(this); bt_8.setOnClickListener(this); bt_9.setOnClickListener(this); jia.setOnClickListener(this); jian.setOnClickListener(this); cheng.setOnClickListener(this); chu.setOnClickListener(this); deng.setOnClickListener(this); dian.setOnClickListener(this); } //View 事件源组件 public void onClick(View v) { //强转 Button bt = (Button)v; int id = bt.getId(); switch (id) { case R.id.clear: //重新实例化(清除) str_show = new StringBuilder(); //设置显示内容 et.setText(str_show); numner_1 = null; numner_2 = null; id_yunsf = 0; break; case R.id.goback: if (str_show.length()>0) //删除最后一个字符 str_show.deleteCharAt(str_show.length() - 1); et.setText(str_show); break; case R.id.bt_0: case R.id.bt_1: case R.id.bt_2: case R.id.bt_3: case R.id.bt_4: case R.id.bt_5: case R.id.bt_6: case R.id.bt_7: case R.id.bt_8: case R.id.bt_9: if (id != R.id.bt_0 || (id == R.id.bt_0 && !str_show.toString().equals("0"))){ str_show.append(bt.getText()); } et.setText(str_show); break; case R.id.dian: et.setText(str_show); break; case R.id.deng: case R.id.jia: case R.id.jian: case R.id.cheng: case R.id.chu: if (numner_1 == null) { if (str_show.length()>0) { numner_1 = new Double(str_show.toString()); id_yunsf = bt.getId(); //重新实例化(清除) str_show = new StringBuilder(); //设置显示内容 et.setText(str_show); } } else //计算 { if (str_show.length()>0) { //把字符串转换成Double 赋值 numner_2 = new Double(str_show.toString()); } //判断运算符 switch (id_yunsf) { case R.id.jia: //运算 numner_1 = numner_1.doubleValue() + numner_2.byteValue(); break; case R.id.jian: //运算 numner_1 = numner_1.doubleValue() - numner_2.byteValue(); break; case R.id.cheng: //运算 numner_1 = numner_1.doubleValue() * numner_2.byteValue(); break; case R.id.chu: if (numner_2 != 0) { //运算 numner_1 = numner_1.doubleValue() / numner_2.byteValue(); } else { Toast.makeText(this,"不能除0",Toast.LENGTH_LONG).show(); } break; } //运算符 id_yunsf = bt.getId(); //显示当前运算结果 et.setText(numner_1.toString()); //清空 str_show = new StringBuilder(); } break; } } }