计算器始终显示0

时间:2013-12-10 11:29:23

标签: android decimal calculator

我已经将一个计算器放入我的应用程序但是由于某种原因,无论我做什么,第一个数字总是为零,所以如果我点击1它显示01,它不会对结果执行此操作而是显示结果.0最后,这些都是我的问题,因为我绝对是一个初学者,需要这个计算器尽可能基本和简单。

我读到.0是一个“双重”问题,但我无法解决如何摆脱这两个问题中的任何一个。任何人都可以为我阐明这一点吗?这是我的代码。

public class CalcActivity extends Activity implements OnClickListener, OnInitListener {
private EditText Scr;
private float NumberBf;
private String Operation;
private ButtonClickListener btnClick;
private TextToSpeech myTTS;
private int MY_DATA_CHECK_CODE = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calc);
    Scr = (EditText) findViewById(R.id.editText);
    Scr.setEnabled(false);
    btnClick = new ButtonClickListener();
    Button speakButton = (Button)findViewById(R.id.speak);
    speakButton.setOnClickListener(this);
    Intent checkTTSIntent = new Intent();
    checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    myTTS= new TextToSpeech(this,this);
    startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);


    int idList[] =  
                     {R.id.button0,R.id.button1,R.id.button2,R.id.button3,
R.id.button4,
R.id.button5,R.id.button6,R.id.button7,
R.id.button8,R.id.button9,R.id.buttonDot,
R.id.buttonAdd,R.id.buttonSub,R.id.buttonMul,
R.id. buttonDiv,R.id.buttonEq,R.id.buttonC
  };

    for(int id:idList){
        View v =(View) findViewById(id);
        v.setOnClickListener(btnClick);
    }
}




public void mMath(String str){
    NumberBf = Float.parseFloat(Scr.getText().toString());
    Operation = str;
    Scr.setText("0");
}
public void getKeyboard(String str){
    String ScrCurrent = Scr.getText().toString();
    ScrCurrent += str;
    Scr.setText(ScrCurrent);

}
public void mResult() {
    float NumAf = Float.parseFloat(Scr.getText().toString());
    float result = 0;
    if(Operation.equals("+")){
        result = NumAf + NumberBf;
    }
    if(Operation.equals("-")){
        result = NumberBf - NumAf;
    }
    if(Operation.equals("*")){
        result = NumAf * NumberBf;
    }
    if(Operation.equals("/")){
        result = NumberBf / NumAf;
    }
Scr.setText(String.valueOf(result));
}

public void onClick(View v) {
    //get the text entered
    EditText enteredText = (EditText)findViewById(R.id.editText);
    String words = enteredText.getText().toString();
    speakWords(words);
}
private void speakWords(String speech) {
    //speak straight away
    myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == MY_DATA_CHECK_CODE) {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            myTTS = new TextToSpeech(this, this);
        }
        else {Intent installTTSIntent = new Intent();
            installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installTTSIntent);
        }
    }
}
public void onInit(int initStatus) {
    //check for successful instantiation
    if (initStatus == TextToSpeech.SUCCESS) {
        if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
            myTTS.setLanguage(Locale.US);
    }
    else if (initStatus == TextToSpeech.ERROR) {
    Toast.makeText(this, "Sorry! Text To Speech failed...",                

 Toast.LENGTH_LONG).show();
    }
}



private class ButtonClickListener implements View.OnClickListener{
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.buttonC:
                Scr.setText("0");
                NumberBf = 0;
                Operation = "";
                break;
            case R.id.buttonAdd:
                mMath("+");
                break;
            case R.id.buttonSub:
                mMath("-");
                break;
            case R.id.buttonMul:
                mMath("*");
                break;
            case R.id.buttonDiv:
                mMath("/");
                break;
            case R.id.buttonEq:
                mResult();
                break;
            default:
                String numb = ((Button) v).getText().toString();
                getKeyboard(numb);
                break;

        }
    }
}
}

2 个答案:

答案 0 :(得分:1)

关于最后的 .0 问题,在调用setText()之前,可以使用省略最后两个字符的结果的子字符串; 也就是说, str 是您用于通过Scr.settext方法分配的字符串变量 在调用setText之前放入这行代码:

str = str.substring(0,str.length-2);

因此,您可以在最后问题上克服 .0。

答案 1 :(得分:0)

你总是在ButtonClickListener上调用mMath(_),所以无论你点击什么按钮,它都会将它设置为“0”

    public void mMath(String str){
    NumberBf = Float.parseFloat(Scr.getText().toString());
    Operation = str;
    Scr.setText("0");  <--
}