一款简单的Android计算器源代码

一款简单的Android计算器源代码

声明:本文代码是在洁子博客http://blog.sina.com.cn/tiankong29源代码的基础上修改过来的。由于他的代码在我的机器上出现很多错误,所以略作修改。使之更加完善。在这里感谢洁子的无私奉献!

效果图:


源代码如下:

MainActivity.java:

public class MainActivity extends ActionBarActivity {


private Button[] btnNum = new Button[11];// 数值按钮  
    private Button[] btnCommand = new Button[5];// 符号按钮  
    private EditText editText = null;// 显示区域  
    private Button btnClear = null; // clear按钮  
    
private static String lastCommand; // 用于保存运算符  
private boolean clearFlag; // 用于判断是否清空显示区域的值,true需要,false不需要  
private boolean firstFlag; // 用于判断是否是首次输入,true首次,false不是首次  
private double result; // 计算结果  
    public MainActivity() { 
        // 初始化各项值  
        result = 0; // x的值  
        firstFlag = true; // 是首次运算  
        clearFlag = false; // 不需要清空  
        lastCommand = "="; // 运算符  
    } 
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // 获取运算符  
        btnCommand[0] = (Button) findViewById(R.id.add); 
        btnCommand[1] = (Button) findViewById(R.id.subtract); 
        btnCommand[2] = (Button) findViewById(R.id.multiply); 
        btnCommand[3] = (Button) findViewById(R.id.divide); 
        btnCommand[4] = (Button) findViewById(R.id.equal); 
        // 获取数字  
        btnNum[0] = (Button) findViewById(R.id.num0); 
        btnNum[1] = (Button) findViewById(R.id.num1); 
        btnNum[2] = (Button) findViewById(R.id.num2); 
        btnNum[3] = (Button) findViewById(R.id.num3); 
        btnNum[4] = (Button) findViewById(R.id.num4); 
        btnNum[5] = (Button) findViewById(R.id.num5); 
        btnNum[6] = (Button) findViewById(R.id.num6); 
        btnNum[7] = (Button) findViewById(R.id.num7); 
        btnNum[8] = (Button) findViewById(R.id.num8); 
        btnNum[9] = (Button) findViewById(R.id.num9); 
        btnNum[10] = (Button) findViewById(R.id.point); 
        
     // 初始化显示结果区域  
        editText=(EditText) findViewById(R.id.Result); 
        editText.setText("0.0"); 
        // 实例化监听器对象  
        NumberAction na = new NumberAction(); 
        CommandAction ca = new CommandAction(); 
        for (Button bc : btnCommand) { 
            bc.setOnClickListener((OnClickListener) ca); 
        } 
        for (Button bc : btnNum) { 
            bc.setOnClickListener((OnClickListener) na); 
        } 
        // clear按钮的动作  
        btnClear = (Button) findViewById(R.id.clear); 
        btnClear.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View view) {
editText.setText("0.0"); 
           // 初始化各项值  
           result = 0; // x的值  
           firstFlag = true; // 是首次运算  
           clearFlag = false; // 不需要清空  
           lastCommand = "="; // 运算符  

}           
        }); 
    }
    
 //数字按钮监听器  
private class NumberAction implements OnClickListener{
@Override
    public void onClick(View view) {
    Button btn = (Button) view; 
          String input = btn.getText().toString(); 
         
   
    if (firstFlag) { // 首次输入  
              // 一上就".",就什么也不做  
              if (input.equals(".")) { 
                  return; 
              } 
              // 如果是"0.0"的话,就清空  
              if (editText.getText().toString().equals("0.0")) { 
                  editText.setText(""); 
              } 
              firstFlag = false;// 改变是否首次输入的标记值  
          } else { 
              String editTextStr = editText.getText().toString(); 
              // 判断显示区域的值里面是否已经有".",如果有,输入的又是".",就什么都不做  
              if (editTextStr.indexOf(".") != -1 && input.equals(".")) { 
                  return; 
              } 
              // 判断显示区域的值里面只有"-",输入的又是".",就什么都不做  
              if (editTextStr.equals("-") && input.equals(".")) { 
                  return; 
              } 
              // 判断显示区域的值如果是"0",输入的不是".",就什么也不做  
              if (editTextStr.equals("0") && !input.equals(".")) { 
                  return; 
              } 
          } 
          
    // 如果我点击了运算符以后,再输入数字的话,就要清空显示区域的值  
          if (clearFlag) { 
              editText.setText(""); 
              clearFlag = false;// 还原初始值,不需要清空  
          } 
          editText.setText(editText.getText().toString() + input);// 设置显示区域的值  
      }
    }
   
  //符号按钮监听器  
private class CommandAction implements OnClickListener{
@Override
    public void onClick(View view) { 
           Button btn = (Button) view; 
           String inputCommand = (String) btn.getText(); 
   
    if (firstFlag) {// 首次输入"-"的情况  
               if (inputCommand.equals("-")) { 
               editText.setText("-");// 显示区域的内容设置为"-"  
                   firstFlag = false;// 改变首次输入的标记  
               } 
           } else { 
               
    if (!clearFlag) {// 如果flag=false不需要清空显示区的值,就调用方法计算  
   
    calculate(Double.parseDouble(editText.getText().toString()));// 保存显示区域的值,并计算  
               } 
               // 保存你点击的运算符  
    lastCommand = inputCommand; 
               clearFlag = true;// 因为我这里已经输入过运算符,  
           } 
       }
    }
   
    // 计算用的方法  
   
public void calculate(double x) { 
         
    // Object lastCommand = null;


    if (lastCommand.equals("+")) { 
                result += x; 
            } else if (lastCommand.equals("-")) { 
            result -= x; 
            } else if (lastCommand.equals("*")) { 
            result *= x; 
            } else if (lastCommand.equals("/")) { 
            result /= x; 
            } else if (lastCommand.equals("=")) { 
            result = x; 
            } 
            editText.setText("" + result); 
            
    }
   
}

activity_main.xml文件:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
   
    tools:context="it.heima.cal.MainActivity" 
    android:stretchColumns="1"
    android:weightSum="6"
    android:orientation="vertical"
   
    >


<TableRow 
    android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        >
    <EditText
android:id="@+id/Result"
        android:layout_width="0px"
        android:layout_height="fill_parent"
        android:layout_span="4"
       
        android:gravity="right|bottom"
        android:cursorVisible="false"
        android:inputType="none"
        android:singleLine="true"
        
       />
</TableRow>  
         
<TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" >


       <Button 
           android:id="@+id/num7" 
           android:layout_width="0px" 
           android:layout_height="fill_parent" 
           
           android:text="7" 
           android:layout_weight="1" 
           /> 
       <Button 
           android:id="@+id/num8" 
           android:layout_width="0px" 
           android:layout_height="fill_parent" 
           
           android:text="8" 
           android:layout_weight="1" 
           /> 
       <Button 
           android:id="@+id/num9" 
           android:layout_width="0px" 
           android:layout_height="fill_parent" 
          
           android:text="9" 
           android:layout_weight="1" 
           /> 
       <Button 
           android:id="@+id/divide" 
           android:layout_width="0px" 
           android:layout_height="fill_parent" 
           
           android:text="/" 
           android:layout_weight="1" 
           />  
 
</TableRow>  


<TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">



        <Button 
            android:id="@+id/num4" 
           android:layout_width="0px" 
            android:layout_height="fill_parent" 
          
            android:text="4" 
            android:layout_weight="1" 
            /> 
        <Button 
            android:id="@+id/num5" 
           android:layout_width="0px" 
            android:layout_height="fill_parent" 
           
            android:text="5" 
            android:layout_weight="1" 
            /> 
        <Button 
            android:id="@+id/num6" 
            android:layout_width="0px" 
            android:layout_height="fill_parent" 
         
            android:text="6" 
            android:layout_weight="1" 
            /> 
        <Button 
            android:id="@+id/multiply" 
           android:layout_width="0px" 
            android:layout_height="fill_parent" 
   
            android:text="*" 
            android:layout_weight="1" 
            /> 
   


</TableRow>    
 
<TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" >
    
        <Button android:id="@+id/num1" 
           android:layout_width="0px" 
            android:layout_height="fill_parent" 
     
            android:text="1" 
            android:layout_weight="1" 
            /> 
        <Button 
            android:id="@+id/num2" 
            android:layout_width="0px" 
            android:layout_height="fill_parent" 
            
            android:text="2" 
            android:layout_weight="1" 
            /> 
        <Button 
            android:id="@+id/num3" 
            android:layout_width="0px" 
            android:layout_height="fill_parent" 
          
            android:text="3" 
            android:layout_weight="1" 
            /> 
        <Button 
            android:id="@+id/subtract" 
            android:layout_width="0px" 
            android:layout_height="fill_parent" 
     
            android:text="-" 
            android:layout_weight="1" 
            /> 
  
    
</TableRow>


<TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"> 
        
        <Button 
            android:id="@+id/num0" 
           android:layout_width="0px" 
                android:layout_height="fill_parent" 
     
                android:text="0" 
                android:layout_weight="1" 
                /> 
        <Button 
            android:id="@+id/point" 
           android:layout_width="0px" 
            android:layout_height="fill_parent" 
       
            android:text="."
            android:layout_weight="1" 
            /> 
        <Button 
            android:id="@+id/add" 
           android:layout_width="0px" 
            android:layout_height="fill_parent" 
  
            android:text="+" 
            android:layout_weight="1" 
            /> 
        <Button 
            android:id="@+id/equal" 
            android:layout_width="0px" 
            android:layout_height="fill_parent" 
      
            android:text="=" 
            android:layout_weight="1" 
            /> 
     
</TableRow> 
    
<TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"> 
        <Button 
            android:id="@+id/clear" 
           android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
          
            android:text="clear" 
            android:layout_span="4" 
            android:gravity="center_vertical|center_horizontal"
            /> 
</TableRow> 
</TableLayout>


由于初学,通过这个小例子的学习我掌握了很多小技巧,特别是对布局有了更加清晰的认识!感谢网上无私奉献的大牛们!

package ymq.demo03; import android.app.Activity; import android.os.Bundle; import android.view.*; import android.widget.*; public class demo03 extends Activity { /** Called when the activity is first created. */ String str=""; EditText et; int c=0,flag=0; double b=0.0,g=0.0,f=0.0; View vi; public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub menu.add(0, 1, 1, "退出"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub if(item.getItemId()==1){finish();} return super.onOptionsItemSelected(item); } //计算方法 public double calculater(){ switch(c){ case 0:f=g;break; case 1:f=b+g;break; case 2:f=b-g;break; case 3:f=b*g;break; case 4:f=b/g;break; } b=f; c=0; return f; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获得按键 final Button number[]=new Button[10]; final Button fuhao[]=new Button[11]; fuhao[0]=(Button)findViewById(R.id.button01); fuhao[1]=(Button)findViewById(R.id.button02); fuhao[2]=(Button)findViewById(R.id.button03); fuhao[3]=(Button)findViewById(R.id.button04); fuhao[4]=(Button)findViewById(R.id.button05); fuhao[5]=(Button)findViewById(R.id.button06); fuhao[6]=(Button)findViewById(R.id.buttonce); fuhao[7]=(Button)findViewById(R.id.buttonc); fuhao[8]=(Button)findViewById(R.id.zheng); fuhao[9]=(Button)findViewById(R.id.kaifang); fuhao[10]=(Button)findViewById(R.id.pingfang); number[0]=(Button)findViewById(R.id.button0); number[1]=(Button)findViewById(R.id.button1); number[2]=(Button)findViewById(R.id.button2); number[3]=(Button)findViewById(R.id.button3); number[4]=(Button)findViewById(R.id.button4); number[5]=(Button)findViewById(R.id.button5); number[6]=(Button)findViewById(R.id.button6); number[7]=(Butto
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值