易网时代-编程资源站
Welcome
微信登录
首页
/
操作系统
/
Linux
/
Android-简便计算器
自己做的简单计算器。源代码项目下载地址。 FTP地址:ftp://www.linuxidc.com用户名:www.linuxidc.com密码:www.muu.cc在 2011年LinuxIDC.com8月Android-简便计算器下载方法见这里 http://www.linuxidc.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
;
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图