每天2个android小例子----简单计算器源代码

通过Android4.0 网格布局GridLayout来实现一个简单的计算器界面布局

  1 package com.android.xiong.gridlayoutTest;
  2 
  3 import java.math.BigDecimal;
  4 import java.util.regex.Pattern;
  5 
  6 import com.android.xiong.gridlayoutTest.R.id;
  7 
  8 import android.os.Bundle;
  9 import android.app.Activity;
 10 import android.view.Menu;
 11 import android.view.View;
 12 import android.view.View.OnClickListener;
 13 import android.widget.Button;
 14 import android.widget.EditText;
 15 import android.widget.GridLayout;
 16 import android.widget.TextView;
 17 
 18 public class MainActivity extends Activity {
 19 
 20     private EditText print;
 21 
 22     private static String fistNumber = "0";// 第一次输入的值
 23     private static String secondNumber = "0";// 第二次输入的值
 24     private static String num = "0";// 显示的结果
 25     private static int flg = 0;// 结果累加一次
 26     public Counts take = null;
 27 
 28     private int[] btidTake = { R.id.txtdivide, R.id.txtx, R.id.txtmin,
 29             R.id.txttakesum };
 30 
 31     private Button[] buttonTake = new Button[btidTake.length];
 32 
 33     private int[] btidNum = { R.id.txt0, R.id.txt1, R.id.txt2, R.id.txt3,
 34             R.id.txt4, R.id.txt5, R.id.txt6, R.id.txt7, R.id.txt8, R.id.txt9,
 35             R.id.txtspl };
 36     private Button[] buttons = new Button[btidNum.length];
 37 
 38     private int[] btcl = { R.id.chars, R.id.charx, R.id.txtb, R.id.txtv };
 39     private Button[] btcls = new Button[btcl.length];
 40     private GridLayout gly;
 41 
 42     @Override
 43     protected void onCreate(Bundle savedInstanceState) {
 44         super.onCreate(savedInstanceState);
 45         setContentView(R.layout.activity_main);
 46         gly=(GridLayout)findViewById(R.id.gly);
 47         print = (EditText) findViewById(R.id.print);
 48         print.setText("0");
 49         print.setEnabled(false);
 50         GetNumber get = new GetNumber();
 51         for (int i = 0; i < btidNum.length; i++) {
 52             buttons[i] = (Button) findViewById(btidNum[i]);
 53             buttons[i].setOnClickListener(get);
 54         }
 55         Compute cm = new Compute();
 56         for (int i = 0; i < btidTake.length; i++) {
 57             buttonTake[i] = (Button) findViewById(btidTake[i]);
 58             buttonTake[i].setOnClickListener(cm);
 59         }
 60 
 61         Button eq = (Button) findViewById(R.id.txteq);
 62 
 63         eq.setOnClickListener(new OnClickListener() {
 64             @Override
 65             public void onClick(View v) {
 66                 if (flg == 0) {
 67                     secondNumber = print.getText().toString();
 68                     if (take == Counts.DIVIDE && secondNumber.equals("0")) {
 69                         print.setText("0不能为被除数");
 70                     } else {
 71                         num = take.Values(fistNumber, secondNumber);
 72                         fistNumber = num;
 73                         secondNumber = "0";
 74                         print.setText(num);
 75                         flg = 1;
 76                         gly.setBackgroundResource(R.drawable.jz);
 77                     }
 78                 }
 79             }
 80         });
 81         Button cleargo = (Button) findViewById(R.id.cleargo);
 82         cleargo.setOnClickListener(new OnClickListener() {
 83             @Override
 84             public void onClick(View v) {
 85                 // TODO Auto-generated method stub
 86                 if (num.length() > 1) {
 87                     num = num.substring(0, num.length() - 1);
 88                 } else {
 89                     num = "0";
 90                 }
 91                 print.setText(num);
 92             }
 93         });
 94         Button clear = (Button) findViewById(R.id.clear);
 95         clear.setOnClickListener(new OnClickListener() {
 96 
 97             @Override
 98             public void onClick(View v) {
 99                 // TODO Auto-generated method stub
100                 num = "0";
101                 fistNumber = secondNumber = num;
102                 print.setText(num);
103                 flg = 0;
104             }
105         });
106         for (int i = 0; i < btcl.length; i++) {
107             btcls[i] = (Button) findViewById(btcl[i]);
108             btcls[i].setOnClickListener(new OnTake());
109         }
110     }
111 
112     // 给 EditText赋值
113     class GetNumber implements OnClickListener {
114 
115         @Override
116         public void onClick(View v) {
117             // TODO Auto-generated method stub
118             if (flg == 1)
119                 num = "0";
120             if (num.equals("0")) {
121                 print.setText("");
122                 num = v.getId() == R.id.txtspl ? "0" : "";
123             }
124             String txt = ((Button) v).getText().toString();
125             boolean s = Pattern.matches("-*(\\d+).?(\\d)*", num + txt);
126             num = s ? (num + txt) : num;
127             gly.setBackgroundResource(R.drawable.js);
128             print.setText(num);
129         }
130     }
131 
132     // 根据条件计算
133     class Compute implements OnClickListener {
134 
135         @Override
136         public void onClick(View arg0) {
137 
138             fistNumber = print.getText().toString();
139             // TODO Auto-generated method stub
140             switch (arg0.getId()) {
141             case R.id.txttakesum:
142                 take = Counts.ADD;
143                 break;
144             case R.id.txtmin:
145                 take = Counts.MINUS;
146                 break;
147             case R.id.txtx:
148                 take = Counts.MULTIPLY;
149                 break;
150             case R.id.txtdivide:
151                 take = Counts.DIVIDE;
152                 break;
153             }
154             num = "0";
155             flg = 0;
156             gly.setBackgroundResource(R.drawable.js);
157         }
158 
159     }
160 
161     class OnTake implements OnClickListener {
162 
163         @Override
164         public void onClick(View v) {
165             // TODO Auto-generated method stub
166             switch (v.getId()) {
167             case R.id.chars:
168                 num = "-" + num;
169                 break;
170             case R.id.charx:
171                 if(!num.equals("0"))
172                 num = BigDecimal.valueOf(1).divide(new BigDecimal(num),12,BigDecimal.ROUND_UP).stripTrailingZeros()
173                         .toString();        
174                 break;
175             case R.id.txtb:
176                 num = new BigDecimal(num).divide(BigDecimal.valueOf(100),12,BigDecimal.ROUND_UP).stripTrailingZeros()
177                         .toString();
178                 break;
179             case R.id.txtv:
180                 Double numss = Math.sqrt(new BigDecimal(num).doubleValue());
181                 int stratindex=numss.toString().contains(".")?numss.toString().indexOf("."):0;
182                 num = numss.toString().length()>13?numss.toString().substring(0, 12+stratindex):numss.toString();
183             }
184             print.setText(num);
185             flg=0;
186             num = "0";
187 
188         }
189 
190     }
191 
192     @Override
193     public boolean onCreateOptionsMenu(Menu menu) {
194         // Inflate the menu; this adds items to the action bar if it is present.
195         getMenuInflater().inflate(R.menu.main, menu);
196         return true;
197     }
198 
199 }
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="40px"
    android:columnCount="5"
    android:rowCount="6"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/print"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="5"
        android:layout_marginLeft="2px"
        android:layout_marginRight="2px"
        android:layout_row="0"
        android:background="#eee" />

    <Button
        android:id="@+id/cleargo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="1"
        android:text="《--" />

    <Button
        android:id="@+id/clear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="3"
        android:layout_columnSpan="2"
        android:layout_row="1"
        android:layout_gravity="fill_horizontal"
        android:text="清屏" />

    <Button
        android:id="@+id/chars"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="1"
        android:text="-/+" />

    <Button
        android:id="@+id/charx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_row="1"
        android:text="1/x" />

    <Button
        android:id="@+id/txt7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="2"
        android:text="7" />

    <Button
        android:id="@+id/txt8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="2"
        android:text="8" />

    <Button
        android:id="@+id/txt9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_row="2"
        android:text="9" />

    <Button
        android:id="@+id/txtdivide"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="3"
        android:layout_row="2"
        android:text="÷" />

    <Button
        android:id="@+id/txtb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="4"
        android:layout_row="2"
        android:text="%" />

    <Button
        android:id="@+id/txt4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="3"
        android:text="4" />

    <Button
        android:id="@+id/txt5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="3"
        android:text="5" />

    <Button
        android:id="@+id/txt6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_row="3"
        android:text="6" />

    <Button
        android:id="@+id/txtx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="3"
        android:layout_row="3"
        android:text="X" />

    <Button
        android:id="@+id/txtv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="4"
        android:layout_row="3"
        android:text="√" />

    <Button
        android:id="@+id/txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="4"
        android:text="1" />

    <Button
        android:id="@+id/txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="4"
        android:text="2" />

    <Button
        android:id="@+id/txt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_row="4"
        android:text="3" />

    <Button
        android:id="@+id/txtmin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="3"
        android:layout_row="4"
        android:text="-" />

    <Button
        android:id="@+id/txteq"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="4"
        android:layout_gravity="fill_vertical"
        android:layout_row="4"
        android:layout_rowSpan="2"
        android:text="=" />

    <Button
        android:id="@+id/txt0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal"
        android:layout_row="5"
        android:text="0" />

    <Button
        android:id="@+id/txtspl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_row="5"
        android:text="." />

    <Button
        android:id="@+id/txttakesum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="3"
        android:layout_row="5"
        android:text="+" />

</GridLayout></pre><br>
<br>
<p></p>
<pre></pre>

 

posted @ 2013-10-18 10:07  kin2321  阅读(785)  评论(0编辑  收藏  举报