1 项目名称
计算器
2 开发环境
Android Studio
3 功能设定
进行简单的加减乘除运算
4 具体设计
在界面设计区域中设置一个6行4列的网格布局,第一行为显示数据的文本标签,第二行为清除数据的按钮,第三至六行均划为四列,共安排十六个按钮,分别代表数字0、1、2、······、9及加、减、乘、除、等号等符号。
5 设计步骤
(1)设计网格布局的布局文件acivity_main.xml并声明TextView和各个Button
(2)设计控制文件MainActivity.java,建立程序中组件与用户界面程序组件的关联,并编写按钮的事件处理代码
6 源代码
6.1 acivity_main.xml
<?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"
tools:context=".MainActivity">
<TextView
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:layout_marginLeft="4dp"
android:gravity="left"
android:text=" "
android:textSize="50dip"/>
<Button
android:id="@+id/btnclear"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:text="清除"
android:textSize="26sp"/>
<Button android:id="@+id/btn1" android:text="1" android:textSize="26sp"/>
<Button android:id="@+id/btn2" android:text="2" android:textSize="26sp"/>
<Button android:id="@+id/btn3" android:text="3" android:textSize="26sp"/>
<Button android:id="@+id/btnadd" android:text="+" android:textSize="26sp"/>
<Button android:id="@+id/btn4" android:text="4" android:textSize="26sp"/>
<Button android:id="@+id/btn5" android:text="5" android:textSize="26sp"/>
<Button android:id="@+id/btn6" android:text="6" android:textSize="26sp"/>
<Button android:id="@+id/btnsub" android:text="-" android:textSize="26sp"/>
<Button android:id="@+id/btn7" android:text="7"