频道栏目
首页 > 资讯 > Android > 正文

Android之一个简单计算器源代码

13-09-05        来源:[db:作者]  
收藏   我要投稿
通过Android4.0 网格布局GridLayout来实现一个简单的计算器界面布局   源码如下(欢迎大家指导 批评 )
[java]  
package com.android.xiong.gridlayoutTest;  
  
  
import java.math.BigDecimal;  
import java.util.regex.Pattern;  
  
  
import com.android.xiong.gridlayoutTest.R.id;  
  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.GridLayout;  
import android.widget.TextView;  
  
  
public class MainActivity extends Activity {  
  
  
<span style="white-space:pre">  </span>private EditText print;  
  
  
<span style="white-space:pre">  </span>private static String fistNumber = "0";// 第一次输入的值  
<span style="white-space:pre">  </span>private static String secondNumber = "0";// 第二次输入的值  
<span style="white-space:pre">  </span>private static String num = "0";// 显示的结果  
<span style="white-space:pre">  </span>private static int flg = 0;// 结果累加一次  
<span style="white-space:pre">  </span>public Counts take = null;  
  
  
<span style="white-space:pre">  </span>private int[] btidTake = { R.id.txtdivide, R.id.txtx, R.id.txtmin,  
<span style="white-space:pre">          </span>R.id.txttakesum };  
  
  
<span style="white-space:pre">  </span>private Button[] buttonTake = new Button[btidTake.length];  
  
  
<span style="white-space:pre">  </span>private int[] btidNum = { R.id.txt0, R.id.txt1, R.id.txt2, R.id.txt3,  
<span style="white-space:pre">          </span>R.id.txt4, R.id.txt5, R.id.txt6, R.id.txt7, R.id.txt8, R.id.txt9,  
<span style="white-space:pre">          </span>R.id.txtspl };  
<span style="white-space:pre">  </span>private Button[] buttons = new Button[btidNum.length];  
  
  
<span style="white-space:pre">  </span>private int[] btcl = { R.id.chars, R.id.charx, R.id.txtb, R.id.txtv };  
<span style="white-space:pre">  </span>private Button[] btcls = new Button[btcl.length];  
<span style="white-space:pre">  </span>private GridLayout gly;  
  
  
<span style="white-space:pre">  </span>@Override  
<span style="white-space:pre">  </span>protected void onCreate(Bundle savedInstanceState) {  
<span style="white-space:pre">      </span>super.onCreate(savedInstanceState);  
<span style="white-space:pre">      </span>setContentView(R.layout.activity_main);  
<span style="white-space:pre">      </span>gly=(GridLayout)findViewById(R.id.gly);  
<span style="white-space:pre">      </span>print = (EditText) findViewById(R.id.print);  
<span style="white-space:pre">      </span>print.setText("0");  
<span style="white-space:pre">      </span>print.setEnabled(false);  
<span style="white-space:pre">      </span>GetNumber get = new GetNumber();  
<span style="white-space:pre">      </span>for (int i = 0; i < btidNum.length; i++) {  
<span style="white-space:pre">          </span>buttons[i] = (Button) findViewById(btidNum[i]);  
<span style="white-space:pre">          </span>buttons[i].setOnClickListener(get);  
<span style="white-space:pre">      </span>}  
<span style="white-space:pre">      </span>Compute cm = new Compute();  
<span style="white-space:pre">      </span>for (int i = 0; i < btidTake.length; i++) {  
<span style="white-space:pre">          </span>buttonTake[i] = (Button) findViewById(btidTake[i]);  
<span style="white-space:pre">          </span>buttonTake[i].setOnClickListener(cm);  
<span style="white-space:pre">      </span>}  
  
  
<span style="white-space:pre">      </span>Button eq = (Button) findViewById(R.id.txteq);  
  
  
<span style="white-space:pre">      </span>eq.setOnClickListener(new OnClickListener() {  
<span style="white-space:pre">          </span>@Override  
<span style="white-space:pre">          </span>public void onClick(View v) {  
<span style="white-space:pre">              </span>if (flg == 0) {  
<span style="white-space:pre">                  </span>secondNumber = print.getText().toString();  
<span style="white-space:pre">                  </span>if (take == Counts.DIVIDE && secondNumber.equals("0")) {  
<span style="white-space:pre">                      </span>print.setText("0不能为被除数");  
<span style="white-space:pre">                  </span>} else {  
<span style="white-space:pre">                      </span>num = take.Values(fistNumber, secondNumber);  
<span style="white-space:pre">                      </span>fistNumber = num;  
<span style="white-space:pre">                      </span>secondNumber = "0";  
<span style="white-space:pre">                      </span>print.setText(num);  
<span style="white-space:pre">                      </span>flg = 1;  
<span style="white-space:pre">                      </span>gly.setBackgroundResource(R.drawable.jz);  
<span style="white-space:pre">                  </span>}  
<span style="white-space:pre">              </span>}  
<span style="white-space:pre">          </span>}  
<span style="white-space:pre">      </span>});  
<span style="white-space:pre">      </span>Button cleargo = (Button) findViewById(R.id.cleargo);  
<span style="white-space:pre">      </span>cleargo.setOnClickListener(new OnClickListener() {  
<span style="white-space:pre">          </span>@Override  
<span style="white-space:pre">          </span>public void onClick(View v) {  
<span style="white-space:pre">              </span>// TODO Auto-generated method stub  
<span style="white-space:pre">              </span>if (num.length() > 1) {  
<span style="white-space:pre">                  </span>num = num.substring(0, num.length() - 1);  
<span style="white-space:pre">              </span>} else {  
<span style="white-space:pre">                  </span>num = "0";  
<span style="white-space:pre">              </span>}  
<span style="white-space:pre">              </span>print.setText(num);  
<span style="white-space:pre">          </span>}  
<span style="white-space:pre">      </span>});  
<span style="white-space:pre">      </span>Button clear = (Button) findViewById(R.id.clear);  
<span style="white-space:pre">      </span>clear.setOnClickListener(new OnClickListener() {  
  
  
<span style="white-space:pre">          </span>@Override  
<span style="white-space:pre">          </span>public void onClick(View v) {  
<span style="white-space:pre">              </span>// TODO Auto-generated method stub  
<span style="white-space:pre">              </span>num = "0";  
<span style="white-space:pre">              </span>fistNumber = secondNumber = num;  
<span style="white-space:pre">              </span>print.setText(num);  
<span style="white-space:pre">              </span>flg = 0;  
<span style="white-space:pre">          </span>}  
<span style="white-space:pre">      </span>});  
<span style="white-space:pre">      </span>for (int i = 0; i < btcl.length; i++) {  
<span style="white-space:pre">          </span>btcls[i] = (Button) findViewById(btcl[i]);  
<span style="white-space:pre">          </span>btcls[i].setOnClickListener(new OnTake());  
<span style="white-space:pre">      </span>}  
<span style="white-space:pre">  </span>}  
  
  
<span style="white-space:pre">  </span>// 给 EditText赋值  
<span style="white-space:pre">  </span>class GetNumber implements OnClickListener {  
  
  
<span style="white-space:pre">      </span>@Override  
<span style="white-space:pre">      </span>public void onClick(View v) {  
<span style="white-space:pre">          </span>// TODO Auto-generated method stub  
<span style="white-space:pre">          </span>if (flg == 1)  
<span style="white-space:pre">              </span>num = "0";  
<span style="white-space:pre">          </span>if (num.equals("0")) {  
<span style="white-space:pre">              </span>print.setText("");  
<span style="white-space:pre">              </span>num = v.getId() == R.id.txtspl ? "0" : "";  
<span style="white-space:pre">          </span>}  
<span style="white-space:pre">          </span>String txt = ((Button) v).getText().toString();  
<span style="white-space:pre">          </span>boolean s = Pattern.matches("(\\d+).?(\\d)*", num + txt);  
<span style="white-space:pre">          </span>num = s ? (num + txt) : num;  
<span style="white-space:pre">          </span>gly.setBackgroundResource(R.drawable.js);  
<span style="white-space:pre">          </span>print.setText(num);  
<span style="white-space:pre">      </span>}  
<span style="white-space:pre">  </span>}  
  
  
<span style="white-space:pre">  </span>// 根据条件计算  
<span style="white-space:pre">  </span>class Compute implements OnClickListener {  
  
  
<span style="white-space:pre">      </span>@Override  
<span style="white-space:pre">      </span>public void onClick(View arg0) {  
  
  
<span style="white-space:pre">          </span>fistNumber = print.getText().toString();  
<span style="white-space:pre">          </span>// TODO Auto-generated method stub  
<span style="white-space:pre">          </span>switch (arg0.getId()) {  
<span style="white-space:pre">          </span>case R.id.txttakesum:  
<span style="white-space:pre">              </span>take = Counts.ADD;  
<span style="white-space:pre">              </span>break;  
<span style="white-space:pre">          </span>case R.id.txtmin:  
<span style="white-space:pre">              </span>take = Counts.MINUS;  
<span style="white-space:pre">              </span>break;  
<span style="white-space:pre">          </span>case R.id.txtx:  
<span style="white-space:pre">              </span>take = Counts.MULTIPLY;  
<span style="white-space:pre">              </span>break;  
<span style="white-space:pre">          </span>case R.id.txtdivide:  
<span style="white-space:pre">              </span>take = Counts.DIVIDE;  
<span style="white-space:pre">              </span>break;  
<span style="white-space:pre">          </span>}  
<span style="white-space:pre">          </span>num = "0";  
<span style="white-space:pre">          </span>flg = 0;  
<span style="white-space:pre">          </span>gly.setBackgroundResource(R.drawable.js);  
<span style="white-space:pre">      </span>}  
  
  
<span style="white-space:pre">  </span>}  
  
  
<span style="white-space:pre">  </span>class OnTake implements OnClickListener {  
  
  
<span style="white-space:pre">      </span>@Override  
<span style="white-space:pre">      </span>public void onClick(View v) {  
<span style="white-space:pre">          </span>// TODO Auto-generated method stub  
<span style="white-space:pre">          </span>switch (v.getId()) {  
<span style="white-space:pre">          </span>case R.id.chars:  
<span style="white-space:pre">              </span>num = "-" + num;  
<span style="white-space:pre">              </span>break;  
<span style="white-space:pre">          </span>case R.id.charx:  
<span style="white-space:pre">              </span>num = BigDecimal.valueOf(1).divide(new BigDecimal(num),12,BigDecimal.ROUND_UP).stripTrailingZeros()  
<span style="white-space:pre">                      </span>.toString();  
<span style="white-space:pre">              </span>break;  
<span style="white-space:pre">          </span>case R.id.txtb:  
<span style="white-space:pre">              </span>num = new BigDecimal(num).divide(BigDecimal.valueOf(100),12,BigDecimal.ROUND_UP).stripTrailingZeros()  
<span style="white-space:pre">                      </span>.toString();  
<span style="white-space:pre">              </span>break;  
<span style="white-space:pre">          </span>case R.id.txtv:  
<span style="white-space:pre">          </span><span style="white-space:pre">   </span>Double numss = Math.sqrt(new BigDecimal(num).doubleValue());  
<span style="white-space:pre">              </span>int stratindex=numss.toString().contains(".")?numss.toString().indexOf("."):0;  
<span style="white-space:pre">              </span>num = numss.toString().length()>13?numss.toString().substring(0, 12+stratindex):numss.toString();  
<span style="white-space:pre">          </span>}  
<span style="white-space:pre">          </span>print.setText(num);  
<span style="white-space:pre">          </span>flg=0;  
  
  
<span style="white-space:pre">      </span>}  
  
  
<span style="white-space:pre">  </span>}  
  
  
<span style="white-space:pre">  </span>@Override  
<span style="white-space:pre">  </span>public boolean onCreateOptionsMenu(Menu menu) {  
<span style="white-space:pre">      </span>// Inflate the menu; this adds items to the action bar if it is present.  
<span style="white-space:pre">      </span>getMenuInflater().inflate(R.menu.main, menu);  
<span style="white-space:pre">      </span>return true;  
<span style="white-space:pre">  </span>}  
  
  
}  
 
[java] 
package com.android.xiong.gridlayoutTest;  
  
import java.math.BigDecimal;  
  
public enum Counts {  
    ADD, MINUS, MULTIPLY, DIVIDE, MARK, ROOT;  
    public String Values(String num1, String num2) {  
        BigDecimal number1 = new BigDecimal(num1);  
        BigDecimal number2 = new BigDecimal(num2);  
        BigDecimal number = BigDecimal.valueOf(0);  
        switch (this) {  
        case ADD:  
            number = number1.add(number2);  
            break;  
        case MINUS:  
            number = number1.subtract(number2);  
            break;  
        case MULTIPLY:  
            number = number1.multiply(number2);  
            break;  
        case DIVIDE:  
            number = number1.divide(number2,20,BigDecimal.ROUND_UP);  
            break;  
  
        }  
        return number.stripTrailingZeros().toString();  
  
    }  
  
}  
 
[html] 
<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>  
 
相关TAG标签
上一篇:将表中一个字段内容放在一起显示
下一篇:oracle检查点队列与增量检查点
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站