Android开发教程案例分享-计算器
简单计算器,有加减乘除功能,删除功能。用于课程大作业不错的
一、思路:
没什么思路,处理好加减乘除即可
二、效果图:
三、关键代码:
public class MainActivity extends Activity {
private TextView tv;
private Button btn_cheng;
private Button btn_chu;
private Button btn_c;
private Button btn_ce;
private Button btn_jia;
private Button btn_dengyu;
private Button btn_jian;
private Button temp;
String str1; //记录输入的字符串
String[] str_shuzi; //存储拆分出来的数字
String[] str_fuhao; //存储拆分出来的运算符
String str_shi; //记录计算公式
String flag="0"; //加个标志,是10.0还是10。flag=0是10.0,flag=1是10
float result; //存储每一步计算出来的结果
int int_result; //为了处理10.0转换为10
int[] btn_shujis;
int i; //用来逐步删除字符串
int j=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init(); //初始化控件
btn_c.setOnClickListener(new Btn_cListener()); //清空
btn_ce.setOnClickListener(new Btn_ceListener()); //删除一个字符
btn_dengyu.setOnClickListener(new Btn_dyListener());
for(int n=0;n<btn_shujis.length;n++){
temp=(Button)findViewById(btn_shujis[n]);
temp.setOnClickListener(new TempListener());
}
}
class TempListener implements OnClickListener{
@Override
public void onClick(View v) {
if(tv.getText().toString().trim().contains("=")){
if(flag.equals("1")){
tv.setText(int_result+"");
}else{
tv.setText(result+"");
}
}
if(tv.getText().toString().trim()=="0"){
tv.setText("");
}
// TODO Auto-generated method stub
str1=tv.getText().toString().trim();
str1=str1+String.valueOf(((Button)v).getText());
tv.setText(str1);
//Toast.makeText(MainActivity.this, str1, Toast.LENGTH_LONG).show();
}
}
class Btn_dyListener implements OnClickListener{
@Override
public void onClick(View v) {
//记录输入的公式
str_shi=tv.getText().toString().trim();
//这里我采取的是拆分字符串的方法来计算,把数字和运算符都取出来。
String str=tv.getText().toString().trim();
int s=0;
int f=0;
//拆分数字
str_shuzi=new String[20]; //忘了如何定义任意长度了,先给它20个
Pattern p = Pattern.compile("[\\d.]+"); //运用正则表达式法
Matcher m = p.matcher(str);
while (m.find()) {
str_shuzi[s]=m.group();
//System.out.println(str_shuzi[i]);
//Toast.makeText(MainActivity.this, str_shuzi[i], Toast.LENGTH_LONG).show();
s++;
}
//拆分运算符
str_fuhao=new String[20];
Pattern p1 = Pattern.compile("[^0-9,.]");
Matcher m1 = p1.matcher(str);
while (m1.find()) {
str_fuhao[f]=m1.group();
//System.out.println(str_fuhao[j]);
//Toast.makeText(MainActivity.this, str_fuhao[j], Toast.LENGTH_LONG).show();
f++;
}
//下面开始计算,感觉这样计算有点繁琐。不管了
while(f>0){
if(str_fuhao[0].equals("+")){
result=Float.parseFloat(str_shuzi[0].trim())+Float.parseFloat(str_shuzi[1].trim());
}else if(str_fuhao[0].equals("-")){
result=Float.parseFloat(str_shuzi[0])-Float.parseFloat(str_shuzi[1]);
}else if(str_fuhao[0].equals("x")){
result=Float.parseFloat(str_shuzi[0])*Float.parseFloat(str_shuzi[1]);
}else if(str_fuhao[0].equals("÷")){
if(Float.parseFloat(str_shuzi[1])==0){
Toast.makeText(MainActivity.this, "除数不能为0", Toast.LENGTH_LONG).show();
return;
}else {
result=Float.parseFloat(str_shuzi[0])/Float.parseFloat(str_shuzi[1]);
}
}
f--;
if(f>0){
if(str_fuhao[1].equals("+")){
result=result+Float.parseFloat(str_shuzi[2]);
}else if(str_fuhao[1].equals("-")){
result=result-Float.parseFloat(str_shuzi[2]);
}else if(str_fuhao[1].equals("x")){
result=result*Float.parseFloat(str_shuzi[2]);
}else if(str_fuhao[1].equals("÷")){
if(Float.parseFloat(str_shuzi[2])==0){
Toast.makeText(MainActivity.this, "除数不能为0", Toast.LENGTH_LONG).show();
return;
}else {
result=Float.parseFloat(str_shuzi[1])/Float.parseFloat(str_shuzi[2]);
}
}
}else {
break; //如果没有运算符了就跳出循环
}
f--;
if(f>0){
if(str_fuhao[2].equals("+")){
result=result+Float.parseFloat(str_shuzi[3]);
}else if(str_fuhao[2].equals("-")){
result=result-Float.parseFloat(str_shuzi[3]);
}else if(str_fuhao[2].equals("x")){
result=result*Float.parseFloat(str_shuzi[3]);
}else if(str_fuhao[2].equals("÷")){
if(Float.parseFloat(str_shuzi[3])==0){
Toast.makeText(MainActivity.this, "除数不能为0", Toast.LENGTH_LONG).show();
return;
}else {
result=Float.parseFloat(str_shuzi[2])/Float.parseFloat(str_shuzi[3]);
}
}
}else {
break; //如果没有运算符了就跳出循环
}
f--;
if(f>0){
if(str_fuhao[3].equals("+")){
result=result+Float.parseFloat(str_shuzi[4]);
}else if(str_fuhao[3].equals("-")){
result=result-Float.parseFloat(str_shuzi[4]);
}else if(str_fuhao[3].equals("x")){
result=result*Float.parseFloat(str_shuzi[4]);
}else if(str_fuhao[3].equals("÷")){
if(Float.parseFloat(str_shuzi[4])==0){
Toast.makeText(MainActivity.this, "除数不能为0", Toast.LENGTH_LONG).show();
return;
}else {
result=Float.parseFloat(str_shuzi[3])/Float.parseFloat(str_shuzi[4]);
}
}
}else {
break; //如果没有运算符了就跳出循环
}
}
if((int)result==result){
int_result=(int)result;
tv.setText(str_shi+"="+"\n"+int_result);
flag="1";
}else{
tv.setText(str_shi+"="+"\n"+result);
}
}
}
四、项目demo源码结构图:
有问题或者需要完整demo源码的私信我,我每天都看私信