avtivity_main.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"
>
<!--rowCount 行数
columnCount 列数
layout_columnSpan 跨列
layout_rowSpan 跨行
fill_horizontal 水平填充
fill_vertical 垂直填充
layout_columnWeight 权重列
layout_rowWeight 权重行
background @drawable/ 设置图片
layout_row 指定行号
layout_column 指定列好 从0开始
-->
<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"/> <Button
android:text="清除"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:id="@+id/clear"
/>
<Button
android:text="后退"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:id="@+id/goback"/> <Button
android:text="/"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"/>
<Button
android:text="X"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"/>
<Button
android:text="7"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:id="@+id/bt_7"/>
<Button
android:text="8"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:id="@+id/bt_8"/>
<Button
android:text="9"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:id="@+id/bt_9"/>
<Button
android:text="-"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"/>
<Button
android:text="4"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"/>
<Button
android:text="5"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"/>
<Button
android:text="6"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"/>
<Button
android:text="+"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"/>
<Button
android:text="1"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"/>
<Button
android:text="2"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"/>
<Button
android:text="3"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"/>
<Button
android:text="="
android:layout_rowSpan="2"
android:layout_gravity="fill_vertical"
android:layout_columnWeight="1"
android:layout_rowWeight="2"
android:textSize="25sp"
android:background="#24f"
/>
<Button
android:text="0"
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:layout_columnWeight="2"
android:layout_rowWeight="1"
android:textSize="25sp"
/>
<Button
android:text="."
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
/> </GridLayout>
CalculatorActivity
package com.hanqi.application3; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; /**
* Created by Administrator on 2016/3/28.
*/
public class CalculatorActivity extends Activity implements View.OnClickListener {
EditText et;
Button bt_clear;
Button bt_goback;
Button bt_7;
Button bt_8;
Button bt_9;
//构建字符串StringBuffer
//存储显示的内容
private StringBuffer str_show= new StringBuffer(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.avtivity_main); et=(EditText)findViewById(R.id.et);
bt_clear=(Button)findViewById(R.id.clear);
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_clear.setOnClickListener(this);
bt_goback.setOnClickListener(this);
bt_7.setOnClickListener(this);
bt_8.setOnClickListener(this);
bt_9.setOnClickListener(this); } public void onClick(View v)
{
Button bt = (Button)v; int id =bt.getId();
switch (id)
{
case R.id.clear:
str_show= new StringBuffer();
et.setText(str_show);
break;
case R.id.goback:
str_show.codePointAt(str_show.length()-1);
et.setText(str_show);
break;
case R.id.bt_7:
case R.id.bt_8:
case R.id.bt_9:
str_show.append(bt.getText());
et.setText(str_show);
break; }
}
}