打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android计算器开发实例
 
Android计算器开发实例
 
 
 
main.xml代码
<?xml version="1.0"encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android
   android:layout_width="fill_parent"android:layout_height="wrap_content" 
   android:textSize="42sp"android:stretchColumns="1"> 
   <TableRow> 
       <EditText android:id="@+id/result"android:layout_width="fill_parent" 
           android:layout_height="wrap_content" 
           android:background="@android:drawable/editbox_background" 
           android:layout_span="4" android:textSize="48sp"android:gravity="right|center_vertical" 
           android:cursorVisible="false" android:editable="false"android:lines="1" /> 
   </TableRow> 
   <TableRow> 
       <LinearLayoutandroid:orientation="horizontal" 
           android:layout_width="fill_parent"android:layout_height="wrap_content" 
           android:textSize="42sp"android:layout_weight="1"> 
           <Button android:id="@+id/num7"android:layout_width="fill_parent" 
               android:layout_height="wrap_content"android:textSize="42sp" 
               android:text="7" android:layout_weight="1"/> 
           <Button android:id="@+id/num8"android:layout_width="fill_parent" 
               android:layout_height="wrap_content"android:textSize="42sp" 
               android:text="8" android:layout_weight="1"/> 
           <Button android:id="@+id/num9"android:layout_width="fill_parent" 
               android:layout_height="wrap_content"android:textSize="42sp" 
               android:text="9" android:layout_weight="1"/> 
           <Button android:id="@+id/divide"android:layout_width="fill_parent" 
               android:layout_height="wrap_content"android:textSize="42sp" 
               android:text="/" android:layout_weight="1"/> 
       </LinearLayout> 
   </TableRow> 
   <TableRow> 
       <LinearLayoutandroid:orientation="horizontal" 
           android:layout_width="fill_parent"android:layout_height="wrap_content" 
           android:textSize="42sp"android:layout_weight="1"> 
           <Button android:id="@+id/num4"android:layout_width="fill_parent" 
               android:layout_height="wrap_content"android:textSize="42sp" 
               android:text="4" android:layout_weight="1"/> 
           <Button android:id="@+id/num5"android:layout_width="fill_parent" 
               android:layout_height="wrap_content"android:textSize="42sp" 
               android:text="5" android:layout_weight="1"/> 
           <Button android:id="@+id/num6"android:layout_width="fill_parent" 
               android:layout_height="wrap_content"android:textSize="42sp" 
               android:text="6" android:layout_weight="1"/> 
           <Button android:id="@+id/multiply"android:layout_width="fill_parent" 
               android:layout_height="wrap_content"android:textSize="42sp" 
               android:text="*" android:layout_weight="1"/> 
       </LinearLayout> 
   </TableRow> 
   <TableRow> 
       <LinearLayoutandroid:orientation="horizontal" 
           android:layout_width="fill_parent"android:layout_height="wrap_content" 
           android:textSize="42sp"android:layout_weight="1"> 
           <Button android:id="@+id/num1"android:layout_width="fill_parent" 
               android:layout_height="wrap_content"android:textSize="42sp" 
               android:text="1" android:layout_weight="1"/> 
           <Button android:id="@+id/num2"android:layout_width="fill_parent" 
               android:layout_height="wrap_content"android:textSize="42sp" 
               android:text="2" android:layout_weight="1"/> 
           <Button android:id="@+id/num3"android:layout_width="fill_parent" 
               android:layout_height="wrap_content"android:textSize="42sp" 
               android:text="3" android:layout_weight="1"/> 
           <Button android:id="@+id/subtract"android:layout_width="fill_parent" 
               android:layout_height="wrap_content"android:textSize="42sp" 
               android:text="-" android:layout_weight="1"/> 
       </LinearLayout> 
   </TableRow> 
   <TableRow> 
       <LinearLayoutandroid:orientation="horizontal" 
           android:layout_width="fill_parent"android:layout_height="wrap_content" 
           android:textSize="42sp"android:layout_weight="1"> 
           <Button android:id="@+id/num0"android:layout_width="fill_parent" 
               android:layout_height="wrap_content"android:textSize="42sp" 
               android:text="0" android:layout_weight="1"/> 
           <Button android:id="@+id/point"android:layout_width="fill_parent" 
               android:layout_height="wrap_content"android:textSize="42sp" 
               android:text="." android:layout_weight="1"/> 
           <Button android:id="@+id/add"android:layout_width="fill_parent" 
               android:layout_height="wrap_content"android:textSize="42sp" 
               android:text="+" android:layout_weight="1"/> 
           <Button android:id="@+id/equal"android:layout_width="fill_parent" 
               android:layout_height="wrap_content"android:textSize="42sp" 
               android:text="=" android:layout_weight="1"/> 
       </LinearLayout> 
   </TableRow> 
   <TableRow> 
       <Button android:id="@+id/clear"android:layout_width="fill_parent" 
           android:layout_height="wrap_content"android:textSize="30sp" 
           android:text="clear" android:layout_span="4"android:gravity="center_vertical|center_horizontal"/> 
   </TableRow> 
</TableLayout> 

 
package com.hello.cn;
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
public class calculator extends Activity { 
    privateButton[] btnNum = new Button[11];//数值按钮  
    privateButton[] btnCommand = new Button[5];//符号按钮  
    privateEditText editText = null;//显示区域  
    privateButton btnClear = null; //clear按钮  
    privateString lastCommand; //用于保存运算符  
    privateboolean clearFlag; //用于判断是否清空显示区域的值,true需要,false不需要  
    privateboolean firstFlag; //用于判断是否是首次输入,true首次,false不是首次  
    privatedouble result; // 计算结果  
    publiccalculator() { 
       // 初始化各项值  
       result = 0; // x的值  
       firstFlag = true; //是首次运算  
       clearFlag = false; //不需要清空  
       lastCommand = "="; // 运算符  
   
   @Override 
    public voidonCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.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(ca); 
       
       for (Button bc : btnNum) { 
           bc.setOnClickListener(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 = "="; // 运算符  
           
       }); 
   
    //数字按钮监听器  
    privateclass 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);//设置显示区域的值  
       
   
    //符号按钮监听器  
    privateclass 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;//因为我这里已经输入过运算符,  
           
       
   
    //计算用的方法  
    private voidcalculate(double x) { 
        
         
       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); 
   


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
加法计算器
android的Intent返回值
安卓动态添加删除多个控件
Android Visual Studio 跨平台開發實戰 Xamarin
Android onClick 按钮单击事件 四种常用写法
关于Android 自定义Dialog按钮监听和数据传递到Acitivity的实现
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服