From c84200a101feb328f5a44604796b974e82c5cee5 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 24 Dec 2015 15:34:45 +0100 Subject: [PATCH] implement digit buttons --- app/build.gradle | 1 + app/proguard-rules.pro | 14 +++- .../com/simple_calculator/MainActivity.java | 66 ++++++++++++++++++- app/src/main/res/layout/activity_main.xml | 6 +- 4 files changed, 83 insertions(+), 4 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 87c968c7..bd11c602 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -23,4 +23,5 @@ dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' + compile 'com.jakewharton:butterknife:7.0.1' } diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index bbaa5f09..aaf6b8cf 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -1,6 +1,6 @@ # Add project specific ProGuard rules here. # By default, the flags in this file are appended to flags specified -# in /home/tibbi/misc/sdk/tools/proguard/proguard-android.txt +# in $ANDROID_HOME/tools/proguard/proguard-android.txt # You can edit the include path and order by changing the proguardFiles # directive in build.gradle. # @@ -15,3 +15,15 @@ #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} + +-keep class butterknife.** { *; } +-dontwarn butterknife.internal.** +-keep class **$$ViewBinder { *; } + +-keepclasseswithmembernames class * { + @butterknife.* ; +} + +-keepclasseswithmembernames class * { + @butterknife.* ; +} 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 7025ceec..d6a63c4a 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 @@ -1,13 +1,77 @@ package calculator.simplemobiletools.com.simple_calculator; -import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.widget.TextView; + +import butterknife.Bind; +import butterknife.ButterKnife; +import butterknife.OnClick; public class MainActivity extends AppCompatActivity { + @Bind(R.id.result) TextView result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + ButterKnife.bind(this); + } + + private void addDigit(int number) { + final String currentValue = result.getText().toString(); + final String newValue = getFormattedValue(currentValue + number); + result.setText(newValue); + } + + private String getFormattedValue(String str) { + return formatDouble(Double.parseDouble(str)); + } + + private String formatDouble(double d) { + if (d == (long) d) { + return String.format("%d", (long) d); + } else { + return String.format("%s", d); + } + } + + @OnClick({R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7, R.id.btn_8, R.id.btn_9}) + public void digitClicked(View view) { + switch (view.getId()) { + case R.id.btn_0: + addDigit(0); + break; + case R.id.btn_1: + addDigit(1); + break; + case R.id.btn_2: + addDigit(2); + break; + case R.id.btn_3: + addDigit(3); + break; + case R.id.btn_4: + addDigit(4); + break; + case R.id.btn_5: + addDigit(5); + break; + case R.id.btn_6: + addDigit(6); + break; + case R.id.btn_7: + addDigit(7); + break; + case R.id.btn_8: + addDigit(8); + break; + case R.id.btn_9: + addDigit(9); + break; + default: + break; + } } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 0436c6ef..1d339298 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -11,11 +11,13 @@ tools:context="calculator.simplemobiletools.com.simple_calculator.MainActivity"> @@ -23,7 +25,7 @@ + android:layout_below="@+id/result">