添加一个+/-按钮我的计算器? - Android电子 [英] Adding a +/- Button to my calculator ? - Android

查看:299
本文介绍了添加一个+/-按钮我的计算器? - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我想添加此+/-按钮来改变在结果窗口中当前显示的数字的符号(一个EditText组件)

Okay So I want to add this +/- button to change the sign of the number currently being shown in the result window (An EditText Component)

下面是的OnClick在功能可按按钮的按钮preSS:

Here's the OnClick Funciton at the button press of the button :

public void onClickListenerPM(View v) {
    vibrator.vibrate(30);
    if (press == '=') {
        onClickListenerReset(buttonReset);
    }

    double masag = -1 * Double.parseDouble(EditTextMsg);
    String s = Double.toString(masag);
    editText.setText(s);
}

我所做的是
从视图的EditText采取的字符串--->
把它做成一个双--->
反转,再次将其转换成字符串,所以,无论数字的符号是相反的。 --->
它显示在视图的EditText

What I've done is Taken the string from the EditText view ---> made it into a double ---> Reverse , Put that into a string again , so that the sign of whatever number is reversed. ---> Show it in the edittext view.

但每当我按一下按钮,该应用程序强制关闭...

But whenever I click the button, the app force Closes ...

什么错?我认为逻辑是正确的,但最有可能的转换导致功能故障。 (不知道虽然)

What's going wrong ? I think the logic is correct but most probably the conversions are causing the function to malfunction. (Not sure though)

你能找出任何可能会造成这个?

Can you spot anything that might be causing this ?

(这是在活动类中定义明显...)

(This is defined in the activity class obviously...)

编辑:
答:
    EditTextMsg = editText.getText()的toString();

EDIT : Answer : EditTextMsg = editText.getText().toString();

忘记添加此-.-

推荐答案

您还没有公布如何提取的EditText 字符串值。你用...

You haven't posted how you extracted the EditText String value. Did you use ...

EditText edit = (EditText)findViewById(R.id.edit_text_id);
String editTextMsg = edit.getText().toString();

... 的toString 在这里很重要!我宁愿用 Double.toString()的toString(双)

... toString is important here! And I would rather use Double.toString() than toString(double):

Double masag = -1 * Double.valueOf(editTextMsg);
String s = masag.toString();

在这里。希望这有助于...

here. Hope this helps ...

干杯!

这篇关于添加一个+/-按钮我的计算器? - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆