Android实现简单计算器
前言
在学习了Android第一行代码的前三章后开始了自己的第一个android小项目,由于接触android时间不长,再加上个人审美能力较差,因此界面做的不是很好,大致可以体现出来一个计算器的界面。。。实现简单的计算器并不难,只要掌握了活动,布局以及控件的基本使用就可以开发出一个简单的android版计算器了
界面部分
我外层用的是外层相对布局,然后内层有四个线性布局加一个TextView控件,由于刚开始学,理解还不是特别深,因此布局这块写的可能感觉有点乱。。因为内层的线性布局采用了垂直布局,所以才写成了这样。。其实可以整体采用百分比布局或者外层垂直线性布局,内层水平线性布局,这样会比较清楚一点,以下是我的布局代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/jiemian"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/symbol_ac"
android:layout_width="match_parent"
android:layout_height="85dp"
android:layout_weight="1"
android:background="#f5f5f5"
android:text="AC"
android:textColor="#007dfe"
android:textSize="24sp" />
<Button
android:id="@+id/number_7"
android:layout_width="match_parent"
android:layout_height="85dp"
android:layout_weight="1"
android:background="@android:color/white"
android:text="7"
android:textSize="24sp" />
<Button
android:id="@+id/number_4"
android:layout_width="match_parent"
android:layout_height="85dp"
android:layout_weight="1"
android:background="@android:color/white"
android:text="4"
android:textSize="24sp" />
<Button
android:id="@+id/number_1"
android:layout_width="match_parent"
android:layout_height="85dp"
android:layout_weight="1"
android:background="@android:color/white"
android:text="1"
android:textSize="24sp" />
<Button
android:id="@+id/symbol_left"
android:layout_width="match_parent"
android:layout_height="85dp"
android:layout_weight="1"
android:background="@android:color/white"
android:text="("
android:textSize="24sp" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/symbol_div"
android:layout_width="match_parent"
android:layout_height="85dp"
android:layout_weight="1"
android:background="#f5f5f5"
android:text="/"
android:textColor="#007dfe"
android:textSize="24sp" />
<Button
android:id="@+id/number_8"
android:layout_width="match_parent"
android:layout_height="85dp"
android:layout_weight="1"
android:background="@android:color/white"
android:text="8"
android:textSize="24sp" />
<Button
android:id="@+id/number_5"
android:layout_width="match_parent"
android:layout_height="85dp"
android:layout_weight="1"
android:background="@android:color/white"
android:text="5"
android:textSize="24sp" />
<Button
android:id="@+id/number_2"
android:layout_width="match_parent"
android:layout_height="85dp"
android:layout_weight="1"
android:background="@android:color/white"
android:text="2"
android:textSize="24sp" />
<Button
android:id="@+id/number_0"
android:layout_width="match_parent"