继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

【大家来找BUG】Android萌新纯数学问题解决计算器的APK开发

Ave林荫
关注TA
已关注
手记 1
粉丝 2
获赞 10

以下是MainActivity的代码,萌新作品,存在许多bug,欢迎大神们解决(⊙v⊙)

package com.example.funny;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private TextView textView1;
    private Button bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,bt0;
    private Button btAdd,btSubtract,btMultiply,btDivide;
    private Button btPoint,btSign;
    private Button btClear;

    private double num1=0,num2=0,result=0;
    private int numOperater1=1,numOperater2=1;
    private int operater=0;
    private double point=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculator_layout);

        textView1 = (TextView) findViewById(R.id.textView1);

        bt1 = (Button) findViewById(R.id.button1);
        bt2 = (Button) findViewById(R.id.button2);
        bt3 = (Button) findViewById(R.id.button3);
        bt4 = (Button) findViewById(R.id.button4);
        bt5 = (Button) findViewById(R.id.button5);
        bt6 = (Button) findViewById(R.id.button6);
        bt7 = (Button) findViewById(R.id.button7);
        bt8 = (Button) findViewById(R.id.button8);
        bt9 = (Button) findViewById(R.id.button9);
        bt0 = (Button) findViewById(R.id.button0);

        btAdd = (Button) findViewById(R.id.buttonAdd);
        btSubtract = (Button) findViewById(R.id.buttonSubtract);
        btMultiply = (Button) findViewById(R.id.buttonMultiply);
        btDivide = (Button) findViewById(R.id.buttonDivide);

        btPoint = (Button) findViewById(R.id.buttonPoint);
        btSign = (Button) findViewById(R.id.buttonSign);

        btClear = (Button) findViewById(R.id.buttonClear);

        bt1.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(operater==0){
                    if(point == 1)
                        num1=num1*10+1;
                    else{
                        num1+=1/point;
                        point = point*10;
                    }
                    textView1.setText(num1*numOperater1+"");
                }else{
                    if(point == 1)
                        num2=num2*10+1;
                    else{
                        num2+=1/point;
                        point = point*10;
                    }
                    textView1.setText(num2*numOperater2+"");
                }
            }
        });
        bt2.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(operater==0){
                    if(point == 1)
                        num1=num1*10+2;
                    else{
                        num1+=2/point;
                        point = point*10;
                    }
                    textView1.setText(num1*numOperater1+"");
                }else{
                    if(point == 1)
                        num2=num2*10+2;
                    else{
                        num2+=2/point;
                        point = point*10;
                    }
                    textView1.setText(num2*numOperater2+"");
                }
            }
        });
        bt3.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(operater==0){
                    if(point == 1)
                        num1=num1*10+3;
                    else{
                        num1+=3/point;
                        point = point*10;
                    }
                    textView1.setText(num1*numOperater1+"");
                }else{
                    if(point == 1)
                        num2=num2*10+3;
                    else{
                        num2+=3/point;
                        point = point*10;
                    }
                    textView1.setText(num2*numOperater2+"");
                }
            }
        });
        bt4.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(operater==0){
                    if(point == 1)
                        num1=num1*10+4;
                    else{
                        num1+=4/point;
                        point = point*10;
                    }
                    textView1.setText(num1*numOperater1+"");
                }else{
                    if(point == 1)
                        num2=num2*10+4;
                    else{
                        num2+=4/point;
                        point = point*10;
                    }
                    textView1.setText(num2*numOperater2+"");
                }
            }
        });
        bt5.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(operater==0){
                    if(point == 1)
                        num1=num1*10+5;
                    else{
                        num1+=5/point;
                        point = point*10;
                    }
                    textView1.setText(num1*numOperater1+"");
                }else{
                    if(point == 1)
                        num2=num2*10+5;
                    else{
                        num2+=5/point;
                        point = point*10;
                    }
                    textView1.setText(num2*numOperater2+"");
                }
            }
        });
        bt6.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(operater==0){
                    if(point == 1)
                        num1=num1*10+6;
                    else{
                        num1+=6/point;
                        point = point*10;
                    }
                    textView1.setText(num1*numOperater1+"");
                }else{
                    if(point == 1)
                        num2=num2*10+6;
                    else{
                        num2+=6/point;
                        point = point*10;
                    }
                    textView1.setText(num2*numOperater2+"");
                }
            }
        });
        bt7.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(operater==0){
                    if(point == 1)
                        num1=num1*10+7;
                    else{
                        num1+=7/point;
                        point = point*10;
                    }
                    textView1.setText(num1*numOperater1+"");
                }else{
                    if(point == 1)
                        num2=num2*10+7;
                    else{
                        num2+=7/point;
                        point = point*10;
                    }
                    textView1.setText(num2*numOperater2+"");
                }
            }
        });
        bt8.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(operater==0){
                    if(point == 1)
                        num1=num1*10+8;
                    else{
                        num1+=8/point;
                        point = point*10;
                    }
                    textView1.setText(num1*numOperater1+"");
                }else{
                    if(point == 1)
                        num2=num2*10+8;
                    else{
                        num2+=8/point;
                        point = point*10;
                    }
                    textView1.setText(num2*numOperater2+"");
                }
            }
        });
        bt9.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(operater==0){
                    if(point == 1)
                        num1=num1*10+9;
                    else{
                        num1+=9/point;
                        point = point*10;
                    }
                    textView1.setText(num1*numOperater1+"");
                }else{
                    if(point == 1)
                        num2=num2*10+9;
                    else{
                        num2+=9/point;
                        point = point*10;
                    }
                    textView1.setText(num2*numOperater2+"");
                }
            }
        });
        bt0.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(operater==0){
                    if(point == 1)
                        num1=num1*10;
                    else{
                        point = point*10;
                    }
                    textView1.setText(num1*numOperater1+"");
                }else{
                    if(point == 1)
                        num2=num2*10;
                    else{
                        point = point*10;
                    }
                    textView1.setText(num2*numOperater2+"");
                }
            }
        });

        btAdd.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(num2!=0)
                    getResult();
                operater = 1;
                textView1.setText(num2+"");
                point = 1;
            }
        });
        btSubtract.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(num2!=0)
                    getResult();
                if(operater==0 && num1==0){
                    numOperater1=-1;
                }else if(operater!=0 && num2==0){
                    numOperater2=-1;
                }else{
                    operater = 2;
                    textView1.setText(num2+"");
                    point = 1;
                }
            }
        });
        btMultiply.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(num2!=0)
                    getResult();
                operater = 3;
                textView1.setText(num2+"");
                point = 1;
            }
        });
        btDivide.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(num2!=0)
                    getResult();
                operater = 4;
                textView1.setText(num2+"");
                point = 1;
            }
        });

        btPoint.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                point = 10;
            }
        });
        btSign.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                getResult();
                textView1.setText(result+"");
            }
        });

        btClear.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                num1=0;num2=0;numOperater1=1;numOperater2=1;result=0;point=1;operater=0;
                textView1.setText(result+"");;
            }
        });
    }

    public void getResult(){
        switch (operater) {
        case 0:
            result = num1*numOperater1;
            break;
        case 1:
            result = num1*numOperater1+num2*numOperater2;
            break;
        case 2:
            result = num1*numOperater1-num2*numOperater2;
            break;
        case 3:
            result = num1*numOperater1*num2*numOperater2;
            break;
        case 4:
            result = (num1*numOperater1)/(num2*numOperater2);
            break;
        default:
            Toast.makeText(getApplicationContext(), "Error!!!", Toast.LENGTH_SHORT).show();
            break;
        }
        num1 = result;
        num2 = 0;
        numOperater2 = 1;
        operater = 0;
        point = 1;
    }
}

class MyOnClickLisenter implements OnClickListener{

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
}
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP

热门评论

每个按钮里放一个onClick属性,可以统一写click事件,不然代码太多了

感觉你写的有点麻烦了,输入可以用字符串,然后再转成数字。我自己也写过一个Android的计算器,你有兴趣我可以给你我的代码,一起交流

没有看的欲望、格式跑了

查看全部评论