Android實現簡單的計算器

android的佈局和配置文件

1.下面就是android實現的計算器的佈局配置,可以看見基本是線性佈局,這樣的好處是界面簡潔明瞭

[html]  view plain  copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context=".MainActivity" >  
  11.       
  12. <!-- 任務:    要求datapick,監聽用戶選擇的日期 -->  
  13.     <!--         android:inputType="text" -->  
  14.       
  15.         <EditText  
  16.         android:layout_width="280dp"  
  17.         android:layout_height="80dp"  
  18.         android:background="@android:drawable/edit_text"  
  19.         android:inputType="text"  
  20.         android:id="@+id/result"   
  21.           
  22.             />   
  23.       
  24.     <LinearLayout   
  25.         android:layout_width="fill_parent"  
  26.         android:layout_height="wrap_content"  
  27.         android:orientation="horizontal"  
  28.         >  
  29.         <Button   
  30.             android:layout_width="70dp"  
  31.             android:layout_height="60dp"  
  32.             android:text="@string/c_ce"  
  33.             android:id="@+id/c_ce"  
  34.             />  
  35.         <Button   
  36.             android:layout_width="70dp"  
  37.             android:layout_height="60dp"  
  38.             android:text="@string/c_c"  
  39.             android:id="@+id/c_c"  
  40.             />  
  41.         <Button   
  42.             android:layout_width="70dp"  
  43.             android:layout_height="60dp"  
  44.             android:text="@string/c_xx"  
  45.             android:id="@+id/c_xx"  
  46.             />  
  47.         <Button   
  48.             android:layout_width="70dp"  
  49.             android:layout_height="60dp"  
  50.             android:text="@string/c_div"  
  51.             android:id="@+id/c_div"  
  52.             />  
  53.     </LinearLayout>  
  54.       
  55.     <LinearLayout   
  56.         android:layout_width="fill_parent"  
  57.         android:layout_height="wrap_content"  
  58.         android:orientation="horizontal"  
  59.         >  
  60.         <Button   
  61.             android:layout_width="70dp"  
  62.             android:layout_height="60dp"  
  63.             android:text="@string/c_7"  
  64.             android:id="@+id/c_7"  
  65.             />  
  66.         <Button   
  67.             android:layout_width="70dp"  
  68.             android:layout_height="60dp"  
  69.             android:text="@string/c_8"  
  70.             android:id="@+id/c_8"  
  71.             />  
  72.         <Button   
  73.             android:layout_width="70dp"  
  74.             android:layout_height="60dp"  
  75.             android:text="@string/c_9"  
  76.             android:id="@+id/c_9"  
  77.             />  
  78.         <Button   
  79.             android:layout_width="70dp"  
  80.             android:layout_height="60dp"  
  81.             android:text="@string/c_X"  
  82.             android:id="@+id/c_X"  
  83.             />  
  84.     </LinearLayout>  
  85.       
  86.     <LinearLayout   
  87.         android:layout_width="fill_parent"  
  88.         android:layout_height="wrap_content"  
  89.         android:orientation="horizontal"  
  90.         >  
  91.         <Button   
  92.             android:layout_width="70dp"  
  93.             android:layout_height="60dp"  
  94.             android:text="@string/c_4"  
  95.             android:id="@+id/c_4"  
  96.             />  
  97.         <Button   
  98.             android:layout_width="70dp"  
  99.             android:layout_height="60dp"  
  100.             android:text="@string/c_5"  
  101.             android:id="@+id/c_5"  
  102.             />  
  103.         <Button   
  104.             android:layout_width="70dp"  
  105.             android:layout_height="60dp"  
  106.             android:text="@string/c_6"  
  107.             android:id="@+id/c_6"  
  108.             />  
  109.         <Button   
  110.             android:layout_width="70dp"  
  111.             android:layout_height="60dp"  
  112.             android:text="@string/c_delete"  
  113.             android:id="@+id/c_delete"  
  114.             />  
  115.     </LinearLayout>  
  116.       
  117.     <LinearLayout   
  118.         android:layout_width="fill_parent"  
  119.         android:layout_height="wrap_content"  
  120.         android:orientation="horizontal"  
  121.         >  
  122.         <Button   
  123.             android:layout_width="70dp"  
  124.             android:layout_height="60dp"  
  125.             android:text="@string/c_1"  
  126.             android:id="@+id/c_1"  
  127.             />  
  128.         <Button   
  129.             android:layout_width="70dp"  
  130.             android:layout_height="60dp"  
  131.             android:text="@string/c_2"  
  132.             android:id="@+id/c_2"  
  133.             />  
  134.         <Button   
  135.             android:layout_width="70dp"  
  136.             android:layout_height="60dp"  
  137.             android:text="@string/c_3"  
  138.             android:id="@+id/c_3"  
  139.             />  
  140.         <Button   
  141.             android:layout_width="70dp"  
  142.             android:layout_height="60dp"  
  143.             android:text="@string/c_add"  
  144.             android:id="@+id/c_add"  
  145.             />  
  146.     </LinearLayout>  
  147.       
  148.     <LinearLayout   
  149.         android:layout_width="fill_parent"  
  150.         android:layout_height="wrap_content"  
  151.         android:orientation="horizontal"  
  152.         >  
  153.         <Button   
  154.             android:layout_width="70dp"  
  155.             android:layout_height="60dp"  
  156.             android:text="@string/c_aord"  
  157.             android:id="@+id/c_aord"  
  158.             />  
  159.         <Button   
  160.             android:layout_width="70dp"  
  161.             android:layout_height="60dp"  
  162.             android:text="@string/c_0"  
  163.             android:id="@+id/c_0"  
  164.             />  
  165.         <Button   
  166.             android:layout_width="70dp"  
  167.             android:layout_height="60dp"  
  168.             android:text="@string/c_point"  
  169.             android:id="@+id/c_point"  
  170.             />  
  171.         <Button   
  172.             android:layout_width="70dp"  
  173.             android:layout_height="60dp"  
  174.             android:text="@string/c_equal"  
  175.             android:id="@+id/c_equal"  
  176.             />  
  177.     </LinearLayout>  
  178.       
  179.       
  180.   
  181.       
  182.   
  183. </LinearLayout>  

2.按鈕對應的字符串配置:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string name="app_name">計算器</string>  
  5.     <string name="action_settings">Settings</string>  
  6.     <string name="c_ce">CE</string>  
  7.     <string name="c_c">C</string>  
  8.     <string name="c_xx">Xx</string>  
  9.     <string name="c_div">÷</string>  
  10.       
  11.     <string name="c_7">7</string>  
  12.     <string name="c_8">8</string>  
  13.     <string name="c_9">9</string>  
  14.     <string name="c_X">X</string>  
  15.       
  16.     <string name="c_4">4</string>  
  17.     <string name="c_5">5</string>  
  18.     <string name="c_6">6</string>  
  19.     <string name="c_delete"></string>  
  20.       
  21.     <string name="c_1">1</string>  
  22.     <string name="c_2">2</string>  
  23.     <string name="c_3">3</string>  
  24.     <string name="c_add"></string>  
  25.       
  26.     <string name="c_aord">±</string>  
  27.     <string name="c_3">3</string>  
  28.     <string name="c_add"></string>  
  29.       
  30.     <string name="c_aord">±</string>  
  31.     <string name="c_0">0</string>  
  32.     <string name="c_point">·</string>
相關文章
相關標籤/搜索