zoukankan      html  css  js  c++  java
  • android版计算器

     

    1.界面的设计

    1.1 应用程序图标的设计

    每一个应用程序,我们都要设计一个图标,可以说图标是应用程序的名片,好的图标能够很快地吸引用户。

    关于如何设计图标,在网上可以查询相关文件进行学习,在这不多做讲述,本人在网上寻找了一个图标作为计算器应用的图标,图标如下所示。

    1.1 按钮放置以及界面编排

    界面设计应该尽量简洁而美观 ,计算器界面情况如下所示:

     

     

    整个界面的布局为LinearLayout,按钮的布局为TableLayout。

    在这直接给出main.xml中的代码,代码如下所示:

      1 <?xml version="1.0" encoding="utf-8"?>
      2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      3     android:layout_width="fill_parent"
      4     android:layout_height="fill_parent"
      5     android:background="@drawable/background01"
      6     android:orientation="vertical"
      7     android:paddingTop="50dip" >
      8 
      9     <!-- paddingTop 离父控件顶部边缘的距离 -->
     10 
     11 
     12     <EditText
     13         android:id="@+id/etResult"
     14         android:layout_width="314dp"
     15         android:layout_height="60dip"
     16         android:layout_marginLeft="1dip"
     17         android:layout_marginRight="1dip"
     18         android:cursorVisible="false"
     19         android:editable="false"
     20         android:textSize="30dip" />   <!-- 顶部显示 输入框    marginLeft 离某元素左边缘的距离 -->
     21 
     22     <TableLayout
     23         android:layout_width="fill_parent"
     24         android:layout_height="fill_parent"
     25         android:stretchColumns="0,1,2,3,4" >
     26 
     27         <!-- stretchColumns="0,1,2,3,4" 它的意思就是自动拉伸0,1,2,3,4列。 -->
     28 
     29         <TableRow>
     30 
     31             <Button
     32                 android:id="@+id/bt_7"
     33                 android:layout_width="60dip"
     34                 android:layout_height="60dip"
     35                 android:layout_margin="1dip"
     36                 android:focusable="false"
     37                 android:text="7"
     38                 android:textSize="20sp" />
     39 
     40             <Button
     41                 android:id="@+id/bt_8"
     42                 android:layout_width="60dip"
     43                 android:layout_height="60dip"
     44                 android:layout_margin="1dip"
     45                 android:focusable="false"
     46                 android:text="8"
     47                 android:textSize="20sp" />
     48 
     49             <Button
     50                 android:id="@+id/bt_9"
     51                 android:layout_width="60dip"
     52                 android:layout_height="60dip"
     53                 android:layout_margin="1dip"
     54                 android:focusable="false"
     55                 android:text="9"
     56                 android:textSize="20sp" />
     57 
     58             <Button
     59                 android:id="@+id/bt_Devide"
     60                 android:layout_width="60dip"
     61                 android:layout_height="60dip"
     62                 android:layout_margin="1dip"
     63                 android:focusable="false"
     64                 android:text="÷"
     65                 android:textSize="20sp" />  <!-- 除号 -->
     66 
     67             <Button
     68                 android:id="@+id/bt_backout"
     69                 android:layout_width="60dip"
     70                 android:layout_height="60dip"
     71                 android:layout_margin="1dip"
     72                 android:focusable="false"
     73                 android:text="←"
     74                 android:textSize="20sp" /> <!-- 撤销← -->
     75 
     76         </TableRow>
     77 
     78         <TableRow>
     79 
     80             <Button
     81                 android:id="@+id/bt_4"
     82                 android:layout_width="60dip"
     83                 android:layout_height="60dip"
     84                 android:layout_margin="1dip"
     85                 android:focusable="false"
     86                 android:text="4"
     87                 android:textSize="20sp" />
     88 
     89             <Button
     90                 android:id="@+id/bt_5"
     91                 android:layout_width="60dip"
     92                 android:layout_height="60dip"
     93                 android:layout_margin="1dip"
     94                 android:focusable="false"
     95                 android:text="5"
     96                 android:textSize="20sp" />
     97 
     98             <Button
     99                 android:id="@+id/bt_6"
    100                 android:layout_width="60dip"
    101                 android:layout_height="60dip"
    102                 android:layout_margin="1dip"
    103                 android:focusable="false"
    104                 android:text="6"
    105                 android:textSize="20sp" />
    106 
    107             <Button
    108                 android:id="@+id/bt_Multiply"
    109                 android:layout_width="60dip"
    110                 android:layout_height="60dip"
    111                 android:layout_margin="1dip"
    112                 android:focusable="false"
    113                 android:text="*"
    114                 android:textSize="20sp" /> <!-- 乘号 -->
    115 
    116             <Button
    117                 android:id="@+id/bt_C"
    118                 android:layout_width="60dip"
    119                 android:layout_height="60dip"
    120                 android:layout_margin="1dip"
    121                 android:focusable="false"
    122                 android:text="C"
    123                 android:textSize="20sp" /> <!-- C键 就是Clear -->
    124         </TableRow>
    125 
    126         <TableRow>
    127 
    128             <Button
    129                 android:id="@+id/bt_1"
    130                 android:layout_width="60dip"
    131                 android:layout_height="60dip"
    132                 android:layout_margin="1dip"
    133                 android:focusable="false"
    134                 android:text="1"
    135                 android:textSize="20sp" />
    136 
    137             <Button
    138                 android:id="@+id/bt_2"
    139                 android:layout_width="60dip"
    140                 android:layout_height="60dip"
    141                 android:layout_margin="1dip"
    142                 android:focusable="false"
    143                 android:text="2"
    144                 android:textSize="20sp" />
    145 
    146             <Button
    147                 android:id="@+id/bt_3"
    148                 android:layout_width="60dip"
    149                 android:layout_height="60dip"
    150                 android:layout_margin="1dip"
    151                 android:focusable="false"
    152                 android:text="3"
    153                 android:textSize="20sp" />
    154 
    155             <Button
    156                 android:id="@+id/bt_Subtract"
    157                 android:layout_width="60dip"
    158                 android:layout_height="60dip"
    159                 android:layout_margin="1dip"
    160                 android:focusable="false"
    161                 android:text="-"
    162                 android:textSize="20sp" /> <!-- 减-号 -->
    163 
    164             <Button
    165                 android:id="@+id/bt_ON"
    166                 android:layout_width="60dip"
    167                 android:layout_height="60dip"
    168                 android:layout_margin="1dip"
    169                 android:focusable="false"
    170                 android:text="ON"
    171                 android:textSize="20sp" />
    172         </TableRow>
    173 
    174         <TableRow>
    175 
    176             <Button
    177                 android:id="@+id/bt_0"
    178                 android:layout_width="60dip"
    179                 android:layout_height="60dip"
    180                 android:layout_margin="1dip"
    181                 android:focusable="false"
    182                 android:text="0"
    183                 android:textSize="20sp" />
    184 
    185             <Button
    186                 android:id="@+id/bt_Point"
    187                 android:layout_width="60dip"
    188                 android:layout_height="60dip"
    189                 android:layout_margin="1dip"
    190                 android:focusable="false"
    191                 android:text="."
    192                 android:textSize="20sp" />
    193             <!-- 点号 -->
    194 
    195             <Button
    196                 android:id="@+id/bt_equals"
    197                 android:layout_width="60dip"
    198                 android:layout_height="60dip"
    199                 android:layout_margin="1dip"
    200                 android:focusable="false"
    201                 android:text="="
    202                 android:textSize="20sp" />  <!-- 等于 -->
    203 
    204             <Button
    205                 android:id="@+id/bt_Plus"
    206                 android:layout_width="60dip"
    207                 android:layout_height="60dip"
    208                 android:layout_margin="1dip"
    209                 android:focusable="false"
    210                 android:text="+"
    211                 android:textSize="20sp" /> <!-- 加 +号 -->
    212 
    213             <Button
    214                 android:id="@+id/bt_OFF"
    215                 android:layout_width="60dip"
    216                 android:layout_height="60dip"
    217                 android:layout_margin="1dip"
    218                 android:focusable="false"
    219                 android:text="OFF"
    220                 android:textSize="20sp" />
    221         </TableRow>
    222     </TableLayout>
    223 
    224 </LinearLayout>

    Activity_Calculator.java
      1 package lysine.calculator;
      2 
      3 import android.app.Activity;
      4 import android.os.Bundle;
      5 import android.text.InputType;
      6 import android.view.View;
      7 import android.view.View.OnClickListener;
      8 import android.widget.Button;
      9 import android.widget.EditText;
     10 import android.widget.Toast;
     11 
     12 public class Activity_Calculator extends Activity {
     13     // Plus 加 +号 Subtract 减-号 Multiply乘号 Devide除号 Point点号 equals等于 backout撤销←
     14     private EditText etResult;
     15     private Button bt7;
     16     private Button bt8;
     17     private Button bt9;
     18     private Button btDevide;
     19     private Button btBackout;
     20 
     21     private Button bt4;
     22     private Button bt5;
     23     private Button bt6;
     24     private Button btPlus;
     25     private Button btSubtract;
     26 
     27     private Button bt1;
     28     private Button bt2;
     29     private Button bt3;
     30     private Button btMultiply;
     31 
     32     private Button bt0;
     33     private Button btPoint;
     34     private Button btEquals;
     35     private Button btC;
     36 
     37     public void onCreate(Bundle savedInstanceState) {
     38         super.onCreate(savedInstanceState);
     39         setContentView(R.layout.main);
     40         etResult = (EditText) findViewById(R.id.etResult);
     41         bt7 = (Button) findViewById(R.id.bt_7);
     42         bt8 = (Button) findViewById(R.id.bt_8);
     43         bt9 = (Button) findViewById(R.id.bt_9);
     44         btDevide = (Button) findViewById(R.id.bt_Devide); // Devide除号
     45         btBackout = (Button) findViewById(R.id.bt_backout);
     46 
     47         bt4 = (Button) findViewById(R.id.bt_4);
     48         bt5 = (Button) findViewById(R.id.bt_5);
     49         bt6 = (Button) findViewById(R.id.bt_6);
     50         btMultiply = (Button) findViewById(R.id.bt_Multiply); // Multiply乘号
     51         btC = (Button) findViewById(R.id.bt_C);
     52 
     53         bt1 = (Button) findViewById(R.id.bt_1);
     54         bt2 = (Button) findViewById(R.id.bt_2);
     55         bt3 = (Button) findViewById(R.id.bt_3);
     56         btSubtract = (Button) findViewById(R.id.bt_Subtract); // Subtract 减-号
     57 
     58         bt0 = (Button) findViewById(R.id.bt_0);
     59         btPoint = (Button) findViewById(R.id.bt_Point);
     60         btEquals = (Button) findViewById(R.id.bt_equals);
     61         btPlus = (Button) findViewById(R.id.bt_Plus); // Plus 加 +号
     62 
     63         etResult.setInputType(InputType.TYPE_NULL); // 输出框,默认为空
     64 
     65         /*
     66          * .按钮
     67          */
     68         btPoint.setOnClickListener(new OnClickListener() {
     69 
     70             public void onClick(View arg0) {
     71                 String str1 = etResult.getText().toString(); // str1为输出框的字符
     72                 if (str1.equals("")) {
     73                     etResult.setText(""); // 如果等于空字符串。默认显示 ""
     74                 } else {
     75                     etResult.setText(str1 + btPoint.getText().toString()); // 如果不为空,st1
     76                                                                             // 输出框字符串
     77                                                                             // +
     78                 }
     79             }
     80         });
     81 
     82         /*
     83          * 0~9 的数字按钮
     84          */
     85         bt0.setOnClickListener(new OnClickListener() {
     86 
     87             public void onClick(View v) {
     88                 String str1 = etResult.getText().toString(); // str1为输出框的字符
     89                 if (str1.equals("")) {
     90                     etResult.setText(""); // 如果等于空字符串。默认显示""
     91                 } else {
     92                     etResult.setText(str1 + bt0.getText().toString()); // 如果不为空,st1
     93                                                                         // 输出框字符串
     94                                                                         // + bt0
     95                 }
     96             }
     97         });
     98         bt1.setOnClickListener(new OnClickListener() {
     99 
    100             public void onClick(View v) {
    101                 String str1 = etResult.getText().toString(); // str1为输出框的字符
    102                 etResult.setText(str1 + bt1.getText().toString()); // 如果不为空,st1
    103                                                                     // 输出框字符串 +
    104                                                                     // bt1
    105             }
    106         });
    107         bt2.setOnClickListener(new OnClickListener() {
    108 
    109             public void onClick(View v) {
    110                 String str1 = etResult.getText().toString(); // str1为输出框的字符
    111                 etResult.setText(str1 + bt2.getText().toString()); // 如果不为空,st1
    112                                                                     // 输出框字符串 +
    113                                                                     // bt2
    114             }
    115         });
    116         bt3.setOnClickListener(new OnClickListener() {
    117 
    118             public void onClick(View v) {
    119                 String str1 = etResult.getText().toString(); // str1为输出框的字符
    120                 etResult.setText(str1 + bt3.getText().toString()); // 如果不为空,st1
    121                                                                     // 输出框字符串 +
    122                                                                     // bt3
    123 
    124             }
    125         });
    126         bt4.setOnClickListener(new OnClickListener() {
    127 
    128             public void onClick(View v) {
    129                 String str1 = etResult.getText().toString(); // str1为输出框的字符
    130                 etResult.setText(str1 + bt4.getText().toString()); // 如果不为空,st1
    131                                                                     // 输出框字符串 +
    132                                                                     // bt4
    133             }
    134         });
    135         bt5.setOnClickListener(new OnClickListener() {
    136 
    137             public void onClick(View v) {
    138                 String str1 = etResult.getText().toString(); // str1为输出框的字符
    139                 etResult.setText(str1 + bt5.getText().toString()); // 如果不为空,st1
    140                                                                     // 输出框字符串 +
    141                                                                     // bt5
    142             }
    143         });
    144         bt6.setOnClickListener(new OnClickListener() {
    145 
    146             public void onClick(View v) {
    147                 String str1 = etResult.getText().toString(); // str1为输出框的字符
    148                 etResult.setText(str1 + bt6.getText().toString()); // 如果不为空,st1
    149                                                                     // 输出框字符串 +
    150                                                                     // bt6
    151             }
    152         });
    153         bt7.setOnClickListener(new OnClickListener() {
    154 
    155             public void onClick(View v) {
    156                 String str1 = etResult.getText().toString(); // str1为输出框的字符
    157                 etResult.setText(str1 + bt7.getText().toString()); // 如果不为空,st1
    158                                                                     // 输出框字符串 +
    159                                                                     // bt7
    160 
    161             }
    162         });
    163         bt8.setOnClickListener(new OnClickListener() {
    164 
    165             public void onClick(View v) {
    166                 String str1 = etResult.getText().toString(); // str1为输出框的字符
    167                 etResult.setText(str1 + bt8.getText().toString()); // 如果不为空,st1
    168                                                                     // 输出框字符串 +
    169                                                                     // bt8
    170             }
    171         });
    172         bt9.setOnClickListener(new OnClickListener() {
    173 
    174             public void onClick(View v) {
    175                 String str1 = etResult.getText().toString(); // str1为输出框的字符
    176                 etResult.setText(str1 + bt9.getText().toString()); // 如果不为空,st1
    177                                                                     // 输出框字符串 +
    178                                                                     // bt9
    179             }
    180         });
    181 
    182         /*
    183          * C键 清空clear
    184          */
    185         btC.setOnClickListener(new OnClickListener() {
    186             public void onClick(View arg0) {
    187                 String str1 = etResult.getText().toString(); // 这条一定要加,不然影响下面的
    188                 etResult.setText("");
    189             }
    190         });
    191 
    192         /*
    193          * 撤销键 ←
    194          */
    195         btBackout.setOnClickListener(new OnClickListener() {
    196 
    197             public void onClick(View arg0) {
    198                 String str = etResult.getText().toString();
    199                 if (str.length() == 0) {
    200                     etResult.setText("");
    201                 } else {
    202                     etResult.setText(str.subSequence(0, str.length() - 1));
    203                 }
    204             }
    205         });
    206 
    207         /*
    208          * + 、-、 * 、/四个按钮
    209          */
    210         btPlus.setOnClickListener(new OnClickListener() {
    211 
    212             public void onClick(View arg0) {
    213                 final String str = etResult.getText().toString(); // str为输出框的字符
    214                 if (str.equals("")) {
    215                     etResult.setText("");
    216                 } else {
    217                     etResult.setText(str + btPlus.getText().toString());
    218                 }
    219             }
    220         });
    221         btSubtract.setOnClickListener(new OnClickListener() {
    222 
    223             public void onClick(View v) {
    224                 String str = etResult.getText().toString(); // str为输出框的字符
    225                 if (str.equals("")) {
    226                     etResult.setText("");
    227                 } else {
    228                     etResult.setText(str + btSubtract.getText().toString());
    229                 }
    230             }
    231         });
    232         btMultiply.setOnClickListener(new OnClickListener() {
    233 
    234             public void onClick(View v) {
    235                 String str = etResult.getText().toString(); // str为输出框的字符
    236                 if (str.equals("")) {
    237                     etResult.setText("");
    238                 } else {
    239                     etResult.setText(str + btMultiply.getText().toString());
    240                 }
    241 
    242             }
    243         });
    244 
    245         btDevide.setOnClickListener(new OnClickListener() {
    246             public void onClick(View arg0) {
    247                 String str = etResult.getText().toString();
    248                 if (str.equals("")) {
    249                     etResult.setText("");
    250                 } else {
    251                     etResult.setText(str + btDevide.getText().toString());
    252                 }
    253             }
    254         });
    255 
    256         /*
    257          * = 等于按钮
    258          */
    259         btEquals.setOnClickListener(new OnClickListener() {
    260 
    261             public void onClick(View arg0) {
    262                 String str = etResult.getText().toString();
    263                 if (str.indexOf('+') > 0) {
    264                     Double num1 = Double.parseDouble(str.substring(0,
    265                             str.indexOf('+')));
    266                     Double num2 = Double.parseDouble(str.substring(str
    267                             .indexOf('+') + 1));
    268                     Double result = num1 + num2;
    269                     etResult.setText(result.toString());
    270                 } else if (str.indexOf('-') > 0) {
    271                     Double num1 = Double.parseDouble(str.substring(0,
    272                             str.indexOf('-')));
    273                     Double num2 = Double.parseDouble(str.substring(str
    274                             .indexOf('-') + 1));
    275                     Double result = num1 - num2;
    276                     etResult.setText(result.toString());
    277                 } else if (str.indexOf('*') > 0) {
    278                     Double num1 = Double.parseDouble(str.substring(0,
    279                             str.indexOf('*')));
    280                     Double num2 = Double.parseDouble(str.substring(str
    281                             .indexOf('*') + 1));
    282                     Double result = num1 * num2;
    283                     etResult.setText(result.toString());
    284                 } else if (str.indexOf('÷') > 0) {
    285                     Double num1 = Double.parseDouble(str.substring(0,
    286                             str.indexOf('÷')));
    287                     Double num2 = Double.parseDouble(str.substring(str
    288                             .indexOf('÷') + 1));
    289                     if (num2 == 0) {
    290                         Toast.makeText(Activity_Calculator.this, "除数不能为0",
    291                                 Toast.LENGTH_LONG).show();
    292                     } else {
    293                         Double result = num1 / num2;
    294                         etResult.setText(result.toString());
    295                     }
    296                 }
    297             }
    298         });
    299 
    300     }
    301 }

      

  • 相关阅读:
    【Python学习之路】——Day20(Django 上)
    【Python学习之路】——WEB本质
    【Python学习之路】——Day16(JavaScript)
    【Python学习之路】——Day14(HTML)
    【Python学习之路】——Day13(Redis and Memcached)
    【Python学习之路】——Day12(python mysql)
    【Python学习之路】——Day11(I/O多路复用)
    【Python学习之路】——Day10(线程、进程)
    【Python学习之路】——Day9(网络编程socket)
    哲学家就餐-同步问题解析-python
  • 原文地址:https://www.cnblogs.com/firecode/p/2639026.html
Copyright © 2011-2022 走看看