學下Android 練手。
用的是eclipse adt。<?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>
<?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>
<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" />
對於很多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>