《計(jì)算器Android版(源碼).doc》由會(huì)員分享,可在線(xiàn)閱讀,更多相關(guān)《計(jì)算器Android版(源碼).doc(14頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。
100%運(yùn)行通過(guò)
這是自己編寫(xiě)的,通過(guò)eclipse運(yùn)行
簡(jiǎn)易計(jì)算器Android版(源碼)
這是自己整理得,歡迎大家下載
說(shuō)明:每完成一次計(jì)算后,需先clear 清除記憶,當(dāng)然也可以連算
XML布局
源代碼:
package android.sdk;
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;
import android.widget.Toast;
public class Android_calculatorActivity extends Activity
{
private Button bt_1;
private Button bt_2;
private Button bt_3;
private Button bt_4;
private Button bt_5;
private Button bt_6;
private Button bt_7;
private Button bt_8;
private Button bt_9;
private Button bt_0;
private Button bt_add;
private Button bt_sub; // 減
private Button bt_multiply; // 乘
private Button bt_divide; // 除
private Button bt_back;
private Button bt_equal; // 等于
private Button bt_point; // 點(diǎn)
private Button bt_clear; // 清除
private EditText et_play; // 顯示
private String str_oper = "+"; // 運(yùn)算符
private StringBuffer str_display = new StringBuffer();; // 顯示
private String str_result; // 結(jié)果顯示
private double num1;
private double num2;
private boolean flag = true; // 小數(shù)點(diǎn)個(gè)數(shù)開(kāi)關(guān)控制;
private boolean b_sub, b_mul, b_div; // 運(yùn)算符開(kāi)關(guān)控制
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt_0 = (Button) findViewById(R.id.bt_0);
bt_1 = (Button) findViewById(R.id.bt_1);
bt_2 = (Button) findViewById(R.id.bt_2);
bt_3 = (Button) findViewById(R.id.bt_3);
bt_4 = (Button) findViewById(R.id.bt_4);
bt_5 = (Button) findViewById(R.id.bt_5);
bt_6 = (Button) findViewById(R.id.bt_6);
bt_7 = (Button) findViewById(R.id.bt_7);
bt_8 = (Button) findViewById(R.id.bt_8);
bt_9 = (Button) findViewById(R.id.bt_9);
bt_add = (Button) findViewById(R.id.bt_add);
bt_sub = (Button) findViewById(R.id.bt_sub);
bt_multiply = (Button) findViewById(R.id.bt_multiply);
bt_divide = (Button) findViewById(R.id.bt_divide);
bt_back = (Button) findViewById(R.id.bt_back);
bt_equal = (Button) findViewById(R.id.bt_equal);
bt_point = (Button) findViewById(R.id.bt_point);
bt_clear = (Button) findViewById(R.id.bt_clear);
et_play = (EditText) findViewById(R.id.et);
et_play.setText("0.0");
bt_0.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
str_display.append("0");
et_play.setText(str_display.toString());
}
});
bt_1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
str_display.append("1");
et_play.setText(str_display.toString());
}
});
bt_2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
str_display.append("2");
et_play.setText(str_display.toString());
}
});
bt_3.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
str_display.append("3");
et_play.setText(str_display.toString());
}
});
bt_4.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
str_display.append("4");
et_play.setText(str_display.toString());
}
});
bt_5.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
str_display.append("5");
et_play.setText(str_display.toString());
}
});
bt_6.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
str_display.append("6");
et_play.setText(str_display.toString());
}
});
bt_7.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
str_display.append("7");
et_play.setText(str_display.toString());
}
});
bt_8.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
str_display.append("8");
et_play.setText(str_display.toString());
}
});
bt_9.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
str_display.append("9");
et_play.setText(str_display.toString());
}
});
bt_point.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (flag)
{
str_display.append(".");
flag = false;
}
}
});
bt_back.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (str_display.length() != 0)
{
str_display.deleteCharAt(str_display.length() - 1);
et_play.setText(str_display.toString());
}
}
});
bt_add.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
str_oper = "+";
if (!(str_display.toString() == ""))
{
num1 += Double.parseDouble(str_display.toString());
str_display = new StringBuffer("");
}
if (!(str_result == null))
{
num1 = Double.parseDouble(str_result);
str_result = null;
}
et_play.setText(String.valueOf(num1));
flag = true;
}
});
bt_sub.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
str_oper = "-";
if (!b_sub && !(str_display.toString() == ""))
{
num1 = Double.parseDouble(str_display.toString());
et_play.setText(String.valueOf(num1));
str_display = new StringBuffer("");
b_sub = true;
} else
{
if (!(str_display.toString() == ""))
{
num1 -= Double.parseDouble(str_display.toString());
str_display = new StringBuffer("");
}
if (!(str_result == null))
{
num1 = Double.parseDouble(str_result);
str_result = null;
}
et_play.setText(String.valueOf(num1));
}
flag = true;
}
});
bt_multiply.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
str_oper = "*";
if (!b_mul && !(str_display.toString() == ""))
{
num1 = Double.parseDouble(str_display.toString());
et_play.setText(String.valueOf(num1));
str_display = new StringBuffer("");
b_mul = true;
} else
{
if (!(str_display.toString() == ""))
{
num1 *= Double.parseDouble(str_display.toString());
str_display = new StringBuffer("");
}
if (!(str_result == null))
{
num1 = Double.parseDouble(str_result);
str_result = null;
}
et_play.setText(String.valueOf(num1));
}
flag = true;
}
});
bt_divide.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
str_oper = "/";
if (!b_div && !(str_display.toString() == ""))
{
num1 = Double.parseDouble(str_display.toString());
et_play.setText(String.valueOf(num1));
str_display = new StringBuffer("");
b_div = true;
} else
{
if (!(str_display.toString() == ""))
{
if (Double.parseDouble(str_display.toString()) == 0)
{
Toast.makeText(Android_calculatorActivity.this,
"除數(shù)不能為0!", Toast.LENGTH_LONG).show();
} else
{
num1 /= Double.parseDouble(str_display.toString());
str_display = new StringBuffer("");
}
}
if (!(str_result == null))
{
num1 = Double.parseDouble(str_result);
str_result = null;
}
et_play.setText(String.valueOf(num1));
}
flag = true;
}
});
bt_clear.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
str_oper = "+";
str_display = new StringBuffer("");
str_result = null;
num1 = 0;
num2 = 0;
flag = true;
b_sub = false;
b_mul = false;
b_div = false;
et_play.setText("0.0");
}
});
bt_equal.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (str_oper.equals("+"))
{
num2 = Double.parseDouble(str_display.toString());
str_result = String.valueOf((num1 + num2));
et_play.setText(str_result);
str_display = new StringBuffer("");
}
if (str_oper.equals("-"))
{
num2 = Double.parseDouble(str_display.toString());
str_result = String.valueOf((num1 - num2));
et_play.setText(str_result);
str_display = new StringBuffer("");
}
if (str_oper.equals("*"))
{
num2 = Double.parseDouble(str_display.toString());
str_result = String.valueOf((num1 * num2));
et_play.setText(str_result);
str_display = new StringBuffer("");
}
if (str_oper.equals("/"))
{
num2 = Double.parseDouble(str_display.toString());
if (!(num2 == 0))
{
str_result = String.valueOf((num1 / num2));
et_play.setText(str_result);
} else
{
Toast.makeText(Android_calculatorActivity.this,
"除數(shù)不能為0!", Toast.LENGTH_LONG).show();
}
str_display = new StringBuffer("");
}
}
});
}
鏈接地址:http://italysoccerbets.com/p-6573921.html