学下Android 练手。
用的是eclipse adt。
先写布局文件。选择新建工程之后不断enter,建立工程之后,找到自己打算编写的布局文件,路径应该是res/layout/文件名.xml,打开后开始编写。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:gravity="center_vertical|center_horizontal"
android:hint="@string/num1" />
<EditText
android:id="@+id/num2"
android:layout_weight="0.2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:hint="@string/num2" />
<TextView
android:id="@+id/result1"
android:layout_weight="0.2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="@string/result" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1"
android:layout_margin="2dip"
>
<TableRow>
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Add"
android:text="@string/add"
android:textSize="100sp"
android:layout_margin="1dip" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dip"
android:onClick="Sub"
android:text="@string/sub"
android:textSize="100sp" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1"
android:layout_margin="2dip"
>
<TableRow>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dip"
android:onClick="Mul"
android:text="@string/mul"
android:textSize="100sp" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dip"
android:onClick="Div"
android:text="@string/div"
android:textSize="100sp" />
</TableRow>
</TableLayout>
</LinearLayout>
代码分段介绍:
1,线性布局
布局的宽度长度都是match_parent,这样就可以覆盖整个手机屏幕,各个widget布置方向是垂直vertical。各个widget的情况在
android:orientation="vertical" > 与 </LinearLayout>之间编写
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
2.数据输入和计算结果输出部分的widget编写
用了editText用于输入数据,Text View显示输出结果。
layout_weight 是权重的意思,控制显示比例 layout_gravity 为显示重心,hint如字面所示,为默认字符串,其实可以直接输入,我在/src/values/strings.xml中添加相关变量后在在widget中使用
<EditText
android:id="@+id/num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:gravity="center_vertical|center_horizontal"
android:hint="@string/num1" />
<EditText
android:id="@+id/num2"
android:layout_weight="0.2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:hint="@string/num2" />
<TextView
android:id="@+id/result1"
android:layout_weight="0.2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="@string/result" />
3 用于计算的widget
做的比较简单,计算器只有加减乘除四个功能。因为最开始只写加减法,后来才有乘除,所以用了两个tablelayout布局,其实只写一个就可以了。每个layout中间有两个按钮button。其中android:onClick是一种简单的绑定事件监听器的方法,也是Google官方教程里面推荐的。
对于很多Android标签而言,它们都支持如onClick、onLongClick等属性,这种属性的属性值就是一个形如xxx
(View source)的方法的方法名。 通过直接在界面布局文件中为指定标签绑定事件处理方法。
在该界面布局对应的Activity中定义一个void XXX(View source)方法,处理事件。比如这次,我在button上android:onClick=add,就在之后的activity中添加一个void add(View view)方法来处理加法运算。所以最后在activity中添加了void add(View view),void sub(View view),void mul(View view),void div(View view)4个方法用来进行加减乘除运算。
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1"
android:layout_margin="2dip"
>
<TableRow>
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Add"
android:text="@string/add"
android:textSize="100sp"
android:layout_margin="1dip" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dip"
android:onClick="Sub"
android:text="@string/sub"
android:textSize="100sp" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1"
android:layout_margin="2dip"
>
<TableRow>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dip"
android:onClick="Mul"
android:text="@string/mul"
android:textSize="100sp" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dip"
android:onClick="Div"
android:text="@string/div"
android:textSize="100sp" />
</TableRow>
</TableLayout>