android 計算器編寫(1)

學下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標籤而言,它們都支持如onClickonLongClick等屬性,這種屬性的屬性值就是一個形如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>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章