您好,欢迎光临本网站![请登录][注册会员]  
文件名称: android简单计算器
  所属分类: Java
  开发工具:
  文件大小: 987kb
  下载次数: 0
  上传时间: 2015-06-20
  提 供 者: qq_29******
 详细说明: 基于安卓java开发简单计算器小程序。 package com.he; import android.app.Activity; import android.os.Bundle; import android.text.InputType; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class CalculatorActivity extends Activity { //加plus 减subtract 乘multiply 除divide private EditText etResult; private Button btPlus; private Button btSub; private Button btMultiply; private Button btDivide; private Button btDe lete; private Button btCe; private Button btPoint; private Button btDeng; private Button bt0; private Button bt1; private Button bt2; private Button bt3; private Button bt4; private Button bt5; private Button bt6; private Button bt7; private Button bt8; private Button bt9; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); etResult=(EditText)findViewById(R.id.etResult); btPlus=(Button)findViewById(R.id.btPlus); btSub=(Button)findViewById(R.id.btSubtract); btMultiply=(Button)findViewById(R.id.btMultiply); btDivide=(Button)findViewById(R.id.btDivide); btDelete=(Button)findViewById(R.id.btDelete); btDeng=(Button)findViewById(R.id.btDeng); btPoint=(Button)findViewById(R.id.btPoint); btCe=(Button)findViewById(R.id.btCe); bt0=(Button)findViewById(R.id.bt0); bt1=(Button)findViewById(R.id.bt1); bt2=(Button)findViewById(R.id.bt2); bt3=(Button)findViewById(R.id.bt3); bt4=(Button)findViewById(R.id.bt4); bt5=(Button)findViewById(R.id.bt5); bt6=(Button)findViewById(R.id.bt6); bt7=(Button)findViewById(R.id.bt7); bt8=(Button)findViewById(R.id.bt8); bt9=(Button)findViewById(R.id.bt9); etResult.setInputType(InputType.TYPE_NULL); /* * .按钮 */ btPoint.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { String str1=etResult.getText().toString(); if(str1.equals("")){ etResult.setText(""); }else{ etResult.setText(str1+btPoint.getText().toString()); } } }); /* * 0---------9的数字按钮 */ bt0.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { String str1=etResult.getText().toString(); if(str1.equals("")){ etResult.setText(""); }else{ etResult.setText(str1+bt0.getText().toString()); } } }); bt1.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { String str1=etResult.getText().toString(); etResult.setText(str1+bt1.getText().toString()); } }); bt2.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { String str1=etResult.getText().toString(); etResult.setText(str1+bt2.getText().toString()); } }); bt3.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { String str1=etResult.getText().toString(); etResult.setText(str1+bt3.getText().toString()); } }); bt4.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { String str1=etResult.getText().toString(); etResult.setText(str1+bt4.getText().toString()); } }); bt5.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { String str1=etResult.getText().toString(); etResult.setText(str1+bt5.getText().toString()); } }); bt6.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { String str1=etResult.getText().toString(); etResult.setText(str1+bt6.getText().toString()); } }); bt7.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { String str1=etResult.getText().toString(); etResult.setText(str1+bt7.getText().toString()); } }); bt8.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { String str1=etResult.getText().toString(); etResult.setText(str1+bt8.getText().toString()); } }); bt9.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { String str1=etResult.getText().toString(); etResult.setText(str1+bt9.getText().toString()); } }); /* * ce键 清空 */ btCe.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { etResult.setText(""); } }); /* * Delete键 */ btDelete.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { String str=etResult.getText().toString(); if(str.length()==0){ etResult.setText(""); }else{ etResult.setText(str.subSequence(0, str.length()-1)); } } }); /* * + 、-、 * 、/四个按钮 */ btPlus.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { final String str=etResult.getText().toString(); if(str.equals("")){ etResult.setText(""); }else{ etResult.setText(str+btPlus.getText().toString()); } } }); btSub.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { String str=etResult.getText().toString(); if(str.equals("")){ etResult.setText(""); }else{ etResult.setText(str+btSub.getText().toString()); } } }); btMultiply.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { String str=etResult.getText().toString(); if(str.equals("")){ etResult.setText(""); }else{ etResult.setText(str+btMultiply.getText().toString()); } } }); btDivide.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { String str=etResult.getText().toString(); if(str.equals("")){ etResult.setText(""); }else{ etResult.setText(str+btDivide.getText().toString()); } } }); /* * = 按钮 */ btDeng.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { String str=etResult.getText().toString(); /*if(str.charAt(str.length()-1)=='0' && str.charAt(str.length()-2)=='/'){ Toast.makeText(CalculatorActivity.this, "除数不能为0!", Toast.LENGTH_LONG).show(); }else{ }*/ if(str.indexOf('+')>0){ Double num1=Double.parseDouble(str.substring(0, str.indexOf('+'))); Double num2=Double.parseDouble(str.substring(str.indexOf('+')+1)); Double result=num1+num2; etResult.setText(result.toString()); } else if(str.indexOf('-')>0){ Double num1=Double.parseDouble(str.substring(0, str.indexOf('-'))); Double num2=Double.parseDouble(str.substring(str.indexOf('-')+1)); Double result=num1-num2; etResult.setText(result.toString()); } else if(str.indexOf('*')>0){ Double num1=Double.parseDouble(str.substring(0, str.indexOf('*'))); Double num2=Double.parseDouble(str.substring(str.indexOf('*')+1)); Double result=num1*num2; etResult.setText(result.toString()); }else if(str.indexOf('/')>0){ Double num1=Double.parseDouble(str.substring(0, str.indexOf('/'))); Double num2=Double.parseDouble(str.substring(str.indexOf('/')+1)); if(num2==0){ Toast.makeText(CalculatorActivity.this, "除数不能为0!", Toast.LENGTH_LONG).show(); }else{ Double result=num1/num2; etResult.setText(result.toString()); } } } }); } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: 计算器
 输入关键字,在本站1000多万海量源码库中尽情搜索: