0

我是 Android 开发新手,目前正在学习设计一个基本的计算器应用程序。我已经自行设计了布局,但是从不同的网站获取 Main Activity 代码用于 0 到 9 的操作,+, -, *, /并且在合并后,它们可以正常工作。

但是,我确实想用小数点函数进一步修改 MainActivity。而整数可以使用“current = current * 10 + number”正确显示在屏幕上,例如 53 = 5*10+3;

我正在考虑使用循环函数对小数点应用相同的方法,想法如下: 1. current = current + 如果按下点按钮,则剩余 2. 创建一个整数 i,一旦单击任何数字按钮,i 就会增加 3 . 这样当例如输入 5.3,i =1 时,它将 = 5 + 3/(10^i) = 5.3 4. 5.3 循环到这里,然后当例如输入为 5.39 时,现在 i=2,它将 = 5.3 + 9/(10^i) = 5.39

问题>> *然而...真的...我很新鲜,我不知道如何设计十进制按钮的编码,有没有人可以建议代码?* 首先忽略以下要检测错误的插件(例如如果点输入两次或多次,则删除第二个点,如果说是输入,则0在前面添加)..5

按钮id如下,点击一次参考DecimalClickEvent Button b_decimal = (Button) findViewById(R.id.decimal); b_decimal.setOnClickListener(new DecimalClickEvent(???));}

非常感谢提前!下面附上代码以供参考和您的意见:

==========================MainActivity.java====================== ================

package com.trial.newcalculator;

import java.io.Serializable;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

@SuppressLint("ParserError")
public class MainActivity extends Activity {
    State s;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        s = new State();

        int[] opNumbers = new int[] { 
                R.id.zero,
                R.id.one,
                R.id.two,
                R.id.three,
                R.id.four,
                R.id.five,
                R.id.six,
                R.id.seven,
                R.id.eight,
                R.id.nine,     
        };

        final TextView textView = (TextView) findViewById(R.id.ansEditText);
        for (int i = 0; i < 10;i++){
            final Button button = (Button) findViewById(opNumbers[i]);        
            button.setOnClickListener(new NumberClickEvent(textView,s,i));          
        }

      int[] opButtons = new int[] { R.id.add, R.id.subtract, R.id.multiply, R.id.divide };
      State.Operation[] states = new State.Operation[] {
                State.Operation.PLUS, 
                State.Operation.MINUS,
                State.Operation.MULTIPLY, 
                State.Operation.DIVIDE};

        for(int i = 0; i < opButtons.length;i++){
        Button b_op = (Button) findViewById(opButtons[i]);
        b_op.setOnClickListener(new OperationClickEvent(textView, s, states[i]));
        }

// Memory functions
        int[] memButtons = new int[] { R.id.MC, R.id.MR, R.id.Mdeduct, R.id.Mplus};
        State.Operation[] mstates = new State.Operation[] {
                State.Operation.MEMORYCLEAR, 
                State.Operation.MEMORYCALL,
                State.Operation.MEMORYMINUS, 
                State.Operation.MEMORYPLUS};

        for(int i = 0; i < memButtons.length;i++){
        Button b_mem = (Button) findViewById(memButtons[i]);
        b_mem.setOnClickListener(new OperationClickEvent(textView, s, states[i]));
        }
//  Memory functions    

//decimal
//      Button b_decimal = (Button) findViewById(R.id.decimal);
//      b_decimal.setOnClickListener(new DecimalClickEvent(textView, s, "."));   
//decimal

        Button b_eq = (Button) findViewById(R.id.equal);
        b_eq.setOnClickListener(new EqualClickEvent(textView, s));

        Button b_op = (Button) findViewById(R.id.ac);
        b_op.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                s.clear();
                textView.setText(s.getDisplay());
            }
        });               
    }      
@Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putSerializable("STATE", s);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        Serializable serializable = savedInstanceState.getSerializable("STATE");
        if(serializable!= null){
            s = (State) serializable;
        }
    }

    public void onPause(){
         super.onPause();
    }

}

================================State.java================ ===================

package com.trial.newcalculator;

import java.io.Serializable;
import android.text.Editable;
import android.widget.TextView;

public class State implements Serializable {
    private static final long serialVersionUID = -1231231231231233L;
    public TextView output;
    public enum Operation {     
        PLUS,       
        MINUS,      
        MULTIPLY,       
        DIVIDE,     
        MEMORYPLUS,
        MEMORYMINUS,
        MEMORYCALL,
        MEMORYCLEAR,
    }   
    public enum IOState{        
        INPUTTING,      
        DISPLAY_RESULT, 
    }   
    private Double accu = null; 
    private double current = 0; 
    private double memory = 0;
    private Operation currentOp = null; 
    private IOState currentState = IOState.INPUTTING;   
    public Operation getCurrentOp() {       
        return currentOp;   
    }   
    public void setCurrentOp(Operation currentOp) {     
        if (currentState == IOState.INPUTTING){         
            if (accu != null && this.currentOp != null ){calculateResult(); 
            }
            else{accu = Double.valueOf(current);current = 0;
            }
        }
        this.currentOp = currentOp;     
        if (currentState == IOState.INPUTTING){
            currentState = IOState.DISPLAY_RESULT;
        }   
    }   
    private void calculateResult() {
        double res = accu.doubleValue();
        switch (currentOp) {
            case PLUS:
                res += current;
                break;
            case MINUS:
                res -= current;
                break;
            case MULTIPLY:
                res *= current;
                break;
            case DIVIDE:
                res /= current;
                break;
            case MEMORYPLUS:
                memory += current;
                break;
            case MEMORYMINUS:
                memory -= current;
                break;
            case MEMORYCLEAR:
                memory = 0;
                break;
            case MEMORYCALL:
                current = memory;
                break;
        }
        accu = Double.valueOf(res);
        current = 0;
    }
    public void number(int number) {
        if (currentState == IOState.INPUTTING){
            current = current *10 + number;
        }
        else if(currentState == IOState.DISPLAY_RESULT){
            currentState = IOState.INPUTTING;
            current = number;
        }
    }
    public String getDisplay() {
        String res;
        Double d = getCurrentDisplayValue();
        double doubleValue = d.doubleValue();
        int intVal = (int)doubleValue;
        if (intVal == doubleValue){
            res = Integer.toString(intVal);
            }
        else{
            res = d.toString();
            }
        return res;
    }
    private Double getCurrentDisplayValue() {
        Double d = accu;
        if (currentState == IOState.INPUTTING){
            d = Double.valueOf(current);
        }
        return d;
    }
    public void clear() {
        accu = null;
        currentState = IOState.INPUTTING;
        currentOp = null;
        current = 0;
    }

    public void equal() {
        if (accu == null || currentOp == null){
            return;
        }
        calculateResult();
        currentState = IOState.DISPLAY_RESULT;
        currentOp = null;
        current = getCurrentDisplayValue(); 
        }
}

====================OperationClickEvent.java=========================== ====

package com.trial.newcalculator;

import com.trial.newcalculator.State.Operation;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

final class OperationClickEvent implements OnClickListener {
    private State s;
    private TextView textView;
    private Operation op;


    public OperationClickEvent(TextView textView, State s, State.Operation op) {
        super();
        this.op = op;
        this.s = s;
        this.textView = textView;
    }


    public void onClick(View v) {
        s.setCurrentOp(op);
        textView.setText(s.getDisplay());
    }
}

=================EqualClickEvent.java============================== =========

package com.trial.newcalculator;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

final class EqualClickEvent implements OnClickListener {
    private State s;
    private TextView textView;


    public EqualClickEvent(TextView textView, State s) {
        super();
        this.s = s;
        this.textView = textView;
    }


    public void onClick(View v) {
        s.equal();
        textView.setText(s.getDisplay());
    }
}

======================NumberClickEvent.java========================= =========

package com.trial.newcalculator;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

final class NumberClickEvent implements OnClickListener {
    private int number;
    private State s;
    private TextView textView;


    public NumberClickEvent(TextView textView, State s, int number) {
        super();
        this.number = number;
        this.s = s;
        this.textView = textView;
    }


    public void onClick(View v) {
        s.number(number);
        textView.setText(s.getDisplay());
    }
}
4

2 回答 2

0

我建议您获取它们的 id 并在单击特定按钮时执行适当的功能,而不是为负单击事件和 doublezeroclickevent 创建新类。

于 2012-08-13T12:07:32.970 回答
0

每个 OnClickListener 都可以是一个默认的匿名类,它只能由一个按钮触发。因此无需将它们命名为“NegativeClickEvent”、“DoublezeroClickEvent”等。

如果“。”,您可以保留一个布尔变量。被按下,只有在没有的情况下才接受另一个。如果您想要一个可用的计算器,您还需要一个“退格”按钮。因此请注意,该点可能会被删除。(需要跟踪它的位置)。

对于前导 0,您可以使用简单的“如果先按下点,则插入 0。”。

有很多方法可以实现你想要的,但为了简单起见,只需处理你需要处理的情况。

Other ways to implement this may include Double.parseDouble(s) with catch (NumberFormatException e) on each key pressed, or using a double as your buffer, and using the buttons to perform mathematical operations on it directly (such as multiply by 10 and adding the new digit on each button press) - this will ensure input validity without the need to parse String, but it's more complicated to implement.

于 2012-08-13T15:07:28.263 回答