文件名称:android简单计算器
文件大小:987KB
文件格式:RAR
更新时间:2018-07-10 14:17:27
计算器
基于安卓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 btDelete; 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()); } } } }); } }
【文件预览】:
cwh
----.project(846B)
----project.properties(563B)
----proguard.cfg(1KB)
----src()
--------com()
----AndroidManifest.xml(678B)
----res()
--------drawable-ldpi()
--------drawable-hdpi()
--------drawable-mdpi()
--------layout()
--------drawable-xhdpi()
--------values()
----.settings()
--------org.eclipse.jdt.core.prefs(177B)
----assets()
----gen()
--------com()
----.classpath(475B)
----bin()
--------resources.ap_(242KB)
--------classes()
--------AndroidManifest.xml(678B)
--------dexedLibs()
--------res()
--------classes.dex(12KB)
--------Calculator.apk(249KB)