From 9d7b67ba16bac60559ad70155c977e582d77478a Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 26 Dec 2015 16:33:03 +0100 Subject: [PATCH] implement Clear --- .../com/simple_calculator/MainActivity.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/MainActivity.java b/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/MainActivity.java index fbce4d53..0cf58bfc 100644 --- a/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/MainActivity.java +++ b/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/MainActivity.java @@ -8,6 +8,7 @@ import android.widget.TextView; import butterknife.Bind; import butterknife.ButterKnife; import butterknife.OnClick; +import butterknife.OnLongClick; public class MainActivity extends AppCompatActivity { @Bind(R.id.result) TextView result; @@ -32,6 +33,16 @@ public class MainActivity extends AppCompatActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); + resetValues(); + } + + private void resetValues() { + baseValue = 0; + secondValue = 0; + resetValue = false; + lastKey = 0; + lastOperation = 0; + result.setText("0"); } private void addDigit(int number) { @@ -121,6 +132,33 @@ public class MainActivity extends AppCompatActivity { updateResult(Math.sqrt(baseValue)); } + @OnClick(R.id.btn_clear) + public void clearClicked() { + final String oldValue = getDisplayedNumber(); + String newValue; + final int len = oldValue.length(); + int minLen = 1; + if (oldValue.contains("-")) + minLen++; + + if (len > minLen) + newValue = oldValue.substring(0, len - 1); + else + newValue = "0"; + + if (newValue.equals("-0")) + newValue = "0"; + + result.setText(newValue); + baseValue = Double.parseDouble(newValue); + } + + @OnLongClick(R.id.btn_clear) + public boolean clearLongClicked() { + resetValues(); + return true; + } + @OnClick(R.id.btn_equals) public void equalsClicked() { if (lastKey == EQUALS)