Android-简便计算器
Android-简便计算器
自己做的简单计算器。
源代码项目下载地址。
FTP地址:ftp://www.bkjia.com
用户名:www.bkjia.com
密码:www.muu.cc
在 2011年LinuxIDC.com\8月\Android-简便计算器
下载方法见这里 http://www.bkjia.net/thread-1187-1-1.html
就一个activity
- package zhang.calculator;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.os.Bundle;
- import android.os.Vibrator;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MyCalculator extends Activity {
- private Vibrator vibrator;
- private Double num_a;
- private Double num_b;
- private TextView text = null;
- private String temp = null;// 计算符号
- private boolean isDot = true;// 小数点控制
- private boolean clickable = true;// 标志是否按过计算按钮
- private double memoryd; // 使用内存中存储的数字
- private int memoryi;
- private TextView lable=null;
- private boolean isVib=true;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.calculator_main);
- final Button num1 = (Button) findViewById(R.id.num1);
- final Button num2 = (Button) findViewById(R.id.num2);
- final Button num3 = (Button) findViewById(R.id.num3);
- final Button num4 = (Button) findViewById(R.id.num4);
- final Button num5 = (Button) findViewById(R.id.num5);
- final Button num6 = (Button) findViewById(R.id.num6);
- final Button num7 = (Button) findViewById(R.id.num7);
- final Button num8 = (Button) findViewById(R.id.num8);
- final Button num9 = (Button) findViewById(R.id.num9);
- Button num0 = (Button) findViewById(R.id.zero);
- Button point=(Button) findViewById(R.id.point);
- Button mul=(Button)findViewById(R.id.mul);
- Button sub=(Button)findViewById(R.id.sub);
- Button add=(Button)findViewById(R.id.add);
- Button div=(Button)findViewById(R.id.div);
- Button sqrt=(Button)findViewById(R.id.sqrt);
- Button equal=(Button) findViewById(R.id.equal);
- Button c=(Button) findViewById(R.id.clear);
- Button cm=(Button)findViewById(R.id.clearm);
- Button m=(Button)findViewById(R.id.memory);
- Button tf=(Button)findViewById(R.id.tf);
- Button mshow=(Button)findViewById(R.id.showmemory);
- Button back=(Button)findViewById(R.id.back);
- text = (TextView) findViewById(R.id.showText);
- lable = (TextView) findViewById(R.id.lable);
- vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
- //0
- num0.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- zd();
- if(text.getText().toString().equalsIgnoreCase("0")){
- }else{
- if (clickable == false) {
- text.setText("");
- text.setText(text.getText().toString()+"0");
- clickable = true;
- } else {
- text.setText(text.getText().toString()+"0");
- }
- }
- }
- });
- //1
- num1.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- show(num1.getText().toString());
- }
- });
- // 2
- num2.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- show(num2.getText().toString());
- }
- });
- // 3
- num3.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- show(num3.getText().toString());
- }
- });
- // 4
- num4.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- show(num4.getText().toString());
- }
- });
- // 5
- num5.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- show(num5.getText().toString());
- }
- });
- // 6
- num6.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- show(num6.getText().toString());
- }
- });
- // 7
- num7.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- show(num7.getText().toString());
- }
- });
- // 8
- num8.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- show(num8.getText().toString());
- }
- });
- // 9
- num9.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- show(num9.getText().toString());
- }
- });
- //.
- point.setOnClickListener(new View.OnClickListener() {
- public void onClick(View arg0) {
- zd();
- if(text.getText().toString().equalsIgnoreCase("")){
- }else{
- if (text.getText().toString()!=""&&isDot == true) {
- text.setText(text.getText()+".");
- isDot = false;
- }else{
- text.setText(text.getText().toString());
- }
- }
- }
- });
- //加
- add.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- zd();
- if(text.getText().toString().equalsIgnoreCase("")){
- }else{
- if (text.getText() != null){
- num_a =Double.parseDouble(text.getText().toString());
- temp = "add";
- clickable = false;
- isDot=true;
- lable.setText("+");
- }
- }
- }
- });
- //减
- sub.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- zd();
- if(text.getText().toString().equalsIgnoreCase("")){
- }else{
- if (text.getText() != null){
- num_a =Double.parseDouble(text.getText().toString());
- temp = "sub";
- clickable = false;
- lable.setText("—");
- }else{
- text.setText("—");
- }
- isDot=true;
- }
- }
- });
- //乘
- mul.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- zd();
- if(text.getText().toString().equalsIgnoreCase("")){
- }else{
- if (text.getText() != null){
- num_a =Double.parseDouble(text.getText().toString());
- temp = "mul";
- lable.setText("×");
- clickable = false;
- isDot=true;
- }
- }
- }
- });
- // 除
- div.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- zd();
- if(text.getText().toString().equalsIgnoreCase("")){
- }else{
- if (text.getText() != null){
- num_a =Double.parseDouble(text.getText().toString());
- temp = "div";
- lable.setText("÷");
- clickable = false;
- isDot=true;
- }
- }
- }
- });
- //开方
- sqrt.setOnClickListener(new View.OnClickListener() {
- public void onClick(View arg0) {
- zd();
- if(text.getText().toString().equalsIgnoreCase("")){
- }else{
- String s = text.getText().toString();
- if (s.charAt(0) == '-') {
- Toast.makeText(MyCalculator.this, "负数不能开根号!!",Toast.LENGTH_SHORT).show();
- clickable = false;
- } else {
- text.setText(Double.toString(java.lang.Math.sqrt(Double.parseDouble(text.getText().toString()))));
- lable.setText("√");
- clickable = false;
- }
- }
- }
- });
- //等于
- equal.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- zd();
- if (temp != null && text.getText() != null){
- num_b = (Double.parseDouble(text.getText().toString()));
- if (temp == "add"){
- text.setText(Float.toString((float) (num_a + num_b)));
- temp = null;
- }else if (temp == "sub"){
- text.setText(Float.toString((float) (num_a - num_b)));
- temp = null;
- }else if (temp == "mul"){
- text.setText(Float.toString((float) (num_a * num_b)));
- temp = null;
- }else if (temp == "div"){
- text.setText(Float.toString((float) (num_a / num_b)));
- temp = null;
- }
- clickable = false;
- if(text.getText().toString()==""){
- isDot =true;
- }else{
- isDot =false;
- }
- }
- }
- });
- //c
- c.setOnClickListener(new View.OnClickListener() {
- public void onClick(View arg0) {
- zd();
- text.setText("");
- lable.setText("");
- num_a = 0.0;
- temp = null;
- clickable = false;
- isDot = true;
- }
- });
- //cm
- cm.setOnClickListener(new View.OnClickListener() {
- public void onClick(View arg0) {
- zd();
- memoryd = memoryi = 0;
- }
- });
- //m
- m.setOnClickListener(new View.OnClickListener() {
- public void onClick(View arg0) {
- zd();
- if (text.getText().toString() != " "){
- boolean isDot = false;
- for (int i = 0; i < text.getText().length(); i++){
- if ('.' == text.getText().charAt(i)) {
- isDot = true;
- break;
- }
- }
- if(text.getText().toString().equalsIgnoreCase("")){
- memoryd = memoryi = 0;
- }
- else{
- if (isDot == true) {
- memoryd = Double.parseDouble(text.getText().toString());
- memoryi = 0;
- }
- else {
- memoryi = Integer.parseInt(text.getText().toString());
- memoryd = 0;
- }
- }
- clickable = false;
- }
- }
- });
- //ms
- mshow.setOnClickListener(new View.OnClickListener() {
- public void onClick(View arg0) {
- zd();
- if (memoryd != 0) {
- text.setText(Double.toString(memoryd));
- }
- if (memoryi != 0) {
- text.setText(Integer.toString(memoryi));
- }
- clickable = false;
- }
- });
- //取反
- tf.setOnClickListener(new View.OnClickListener() {
- public void onClick(View arg0) {
- zd();
- if(text.getText().toString().equalsIgnoreCase("")){
- }else{
- boolean isNumber = true;
- String s = text.getText().toString();
- for (int i = 0; i < s.length(); i++)
- if (!(s.charAt(i) >= '0' && s.charAt(i) <= '9'
- || s.charAt(i) == '.' || s.charAt(i) == '-')) {
- isNumber = false;
- break;
- }
- if (isNumber == true) {
- //如果当前字符串首字母有'-'号,代表现在是个负数,再按下时,则将首符号去掉
- if (s.charAt(0) == '-') {
- text.setText("");
- for (int i = 1; i < s.length(); i++) {
- char a = s.charAt(i);
- text.setText(text.getText().toString() + a);
- }
- }
- //如果当前字符串第一个字符不是符号,则添加一个符号在首字母处
- else
- text.setText('-' + s);
- }
- }}
- });
- //back
- back.setOnClickListener(new View.OnClickListener() {
- public void onClick(View arg0) {
- zd();
- if(clickable == true)
- if (text.getText().toString() != "") {
- String s = text.getText().toString();
- text.setText("");
- for (int i = 0; i < s.length() - 1; i++) {
- char a = s.charAt(i);
- text.setText(text.getText().toString() + a);
- }
- }
- }
- });
- }
- public void zd(){ //振动的方法
- if(isVib==true){
- long[] pattern = {0, 20, 0, 10}; // OFF/ON/OFF/ON...
- vibrator.vibrate(pattern, -1);
- }else{
- vibrator.cancel();
- }
- }
- public void show(String i){ //1-9的数
- zd();
- if (clickable == false) {
- text.setText("");
- text.setText(text.getText() + i);
- clickable = true;
- } else {
- text.setText(text.getText() + i);
- }
- }
- public boolean onCreateOptionsMenu(Menu menu) { //添加菜单
- // TODO Auto-generated method stub
- menu.add(0, 1, 1,R.string.exit);
- menu.add(0, 2, 2, R.string.about);
- menu.add(1,3,3,"开/关震动");
- return super.onCreateOptionsMenu(menu);
- }
- public boolean onOptionsItemSelected(MenuItem item) { //设置菜单事件
- switch(item.getItemId()){
- case 1:
- finish();
- break;
- case 2:
- AlertDialog.Builder dialog = new AlertDialog.Builder(this);
- dialog.setTitle("关于").setMessage("This is a black cool convenient calculator!\nBy:没落凄凉").show();
- break;
- case 3:
- if(isVib){
- isVib=false;
- }else{
- isVib=true;
- }
- }
- return super.onOptionsItemSelected(item);
- }
- }
|
评论暂时关闭