Android开发之SimpleCalculate简易单行计算器源码
这是一个Android开发的简易的计算器,只能进行简单的四则运算,其亮点单行显示,是为“简易”,当我们要计算某两个数的四种运算时,将会是很好的效果
先上主界面:
package com.qq.zxjzzg;
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 CalculateActivity extends Activity {
private EditText factorOne;
private EditText factorTwo;
private Button minus;
private Button finalresult;
int position = 0;
Double result;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
factorOne = (EditText)findViewById(R.id.factorOne);
factorTwo = (EditText)findViewById(R.id.factorTwo);
minus = (Button)findViewById(R.id.minus);
finalresult = (Button)findViewById(R.id.result);
minus.setOnClickListener(new OnClickListener() {
//先新一个数组,用来装运算符号
private int[] operation =
{
R.string.plus,
R.string.minus,
R.string.multips,
R.string.divide,
};
@Override
public void onClick(View v) {
//首先要确定是否有输入数字
if(!(factorTwo.getText().toString().equals("") || factorOne.getText().toString().equals(""))){
minus.setText(this.operation[position]);//显示运算符号
//转换数据类型
int factorone = Integer.parseInt(factorOne.getText().toString());//在收集数据时强制进行数据转换
int factortwo = Integer.parseInt(factorTwo.getText().toString());
//根据当前的运算符来计算结果
switch (position) {
case 0:
result = (double) (factorone + factortwo);
break;
case 1:
result = (double) (factorone - factortwo);
break;
case 2:
result = (double) (factorone * factortwo);
break;
case 3:
result = (double) (factorone / factortwo);
break;
}
//if(result = )
String r = Double.toString(result);//int转String用于在TextView中显示
finalresult.setText(r);
//改变运算符号
position++;
if(position > 3){position = 0;}
}else{
Toast.makeText(CalculateActivity.this, R.string.error, 5000).show();
}
}
});
//下面是有关于计算的一些代码,可供完善计算器作为参考
/* String str1 = factorOne.getText().toString();
String str2 = factorTwo.getText().toString();
int int1 = Integer.parseInt(str1);
int int2 = Integer.parseInt(str2);
int plus1 = int1 + int2;
int minus1 = int1 - int2;
int multiply1 = int1 * int2;
int divide1 = int1 / int2;
result.setText(minus1);
result.setText(multiply1);
result.setText(divide1);
plus.setOnClickListener(new plusListener());
/* minus.setOnClickListener(new minusListener());
multiply.setOnClickListener(new multiplyListener());
divide.setOnClickListener(new divideListener());
*/
}
/*
class plusListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str1 = factorOne.getText().toString();
String str2 = factorTwo.getText().toString();
int int1 = Integer.parseInt(str1);
int int2 = Integer.parseInt(str2);
int plus1 = int1 + int2;
//result.setText(plus1);
}
}
/* class minusListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str1 = factorOne.getText().toString();
String str2 = factorTwo.getText().toString();
int int1 = Integer.parseInt(str1);
int int2 = Integer.parseInt(str2);
int minus1 = int1 - int2;
result.setText(minus1);
}
}
class multiplyListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str1 = factorOne.getText().toString();
String str2 = factorTwo.getText().toString();
int int1 = Integer.parseInt(str1);
int int2 = Integer.parseInt(str2);
int multips1 = int1 * int2;
result.setText(multips1);
}
}
class divideListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str1 = factorOne.getText().toString();
String str2 = factorTwo.getText().toString();
int int1 = Integer.parseInt(str1);
int int2 = Integer.parseInt(str2);
int divide1 = int1 / int2;
result.setText(divide1);
}
}
*/
}
相关推荐
preserve log:勾选,页面发生跳转,接口不丢失;;Disable cache:不使用缓存,勾选,拿服务器的缓存;不勾选,用本地缓存;;All那列,表示浏览器的请求类型,对应下面的列type;