From 5801bbdc25b17896adc65aa40ce158257c68bd55 Mon Sep 17 00:00:00 2001 From: profick Date: Sat, 26 Dec 2015 15:06:41 +0800 Subject: [PATCH 1/4] first commit --- .idea/gradle.xml | 18 ++++++++++++++++++ homework1.iml | 19 +++++++++++++++++++ settings.gradle | 1 + 3 files changed, 38 insertions(+) create mode 100644 .idea/gradle.xml create mode 100644 homework1.iml create mode 100644 settings.gradle diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..8d2df47 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/homework1.iml b/homework1.iml new file mode 100644 index 0000000..cc789d2 --- /dev/null +++ b/homework1.iml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..e7b4def --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +include ':app' From 6432b497d73aaa9652ae7e41d69b32ef80bb0852 Mon Sep 17 00:00:00 2001 From: profick Date: Sun, 27 Dec 2015 03:40:25 +0800 Subject: [PATCH 2/4] Homework1 Done --- .gitattributes | 17 ++ .gitignore | 7 + .idea/.name | 1 + .idea/compiler.xml | 22 ++ .idea/copyright/profiles_settings.xml | 3 + .idea/encodings.xml | 6 + .idea/misc.xml | 62 +++++ .idea/modules.xml | 9 + .idea/runConfigurations.xml | 12 + .idea/vcs.xml | 6 + homework1.iml => SimpleCalculator.iml | 2 +- app/.gitignore | 1 + app/app.iml | 97 ++++++++ app/build.gradle | 26 ++ app/proguard-rules.pro | 17 ++ .../simplecalculator/ApplicationTest.java | 13 + app/src/main/AndroidManifest.xml | 20 ++ .../simplecalculator/MainActivity.java | 224 +++++++++++++++++ app/src/main/res/layout-land/myscreen.xml | 216 +++++++++++++++++ app/src/main/res/layout/activity_main.xml | 9 + app/src/main/res/layout/myscreen.xml | 226 ++++++++++++++++++ app/src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3418 bytes app/src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2206 bytes app/src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4842 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 7718 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 10486 bytes app/src/main/res/values-w820dp/dimens.xml | 6 + app/src/main/res/values/colors.xml | 23 ++ app/src/main/res/values/dimens.xml | 5 + app/src/main/res/values/strings.xml | 3 + app/src/main/res/values/styles.xml | 11 + .../simplecalculator/ExampleUnitTest.java | 15 ++ build.gradle | 23 ++ gradle.properties | 18 ++ gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 49896 bytes gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 164 +++++++++++++ gradlew.bat | 90 +++++++ 38 files changed, 1359 insertions(+), 1 deletion(-) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .idea/.name create mode 100644 .idea/compiler.xml create mode 100644 .idea/copyright/profiles_settings.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/runConfigurations.xml create mode 100644 .idea/vcs.xml rename homework1.iml => SimpleCalculator.iml (70%) create mode 100644 app/.gitignore create mode 100644 app/app.iml create mode 100644 app/build.gradle create mode 100644 app/proguard-rules.pro create mode 100644 app/src/androidTest/java/profick/simplecalculator/ApplicationTest.java create mode 100644 app/src/main/AndroidManifest.xml create mode 100644 app/src/main/java/profick/simplecalculator/MainActivity.java create mode 100644 app/src/main/res/layout-land/myscreen.xml create mode 100644 app/src/main/res/layout/activity_main.xml create mode 100644 app/src/main/res/layout/myscreen.xml create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 app/src/main/res/values-w820dp/dimens.xml create mode 100644 app/src/main/res/values/colors.xml create mode 100644 app/src/main/res/values/dimens.xml create mode 100644 app/src/main/res/values/strings.xml create mode 100644 app/src/main/res/values/styles.xml create mode 100644 app/src/test/java/profick/simplecalculator/ExampleUnitTest.java create mode 100644 build.gradle create mode 100644 gradle.properties create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100644 gradlew create mode 100644 gradlew.bat diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..bdb0cab --- /dev/null +++ b/.gitattributes @@ -0,0 +1,17 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Custom for Visual Studio +*.cs diff=csharp + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9c4de58 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..59c1fd2 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +SimpleCalculator \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..cca2cda --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1.8 + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..9892fe7 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/homework1.iml b/SimpleCalculator.iml similarity index 70% rename from homework1.iml rename to SimpleCalculator.iml index cc789d2..e50ab2a 100644 --- a/homework1.iml +++ b/SimpleCalculator.iml @@ -1,5 +1,5 @@ - + diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/app.iml b/app/app.iml new file mode 100644 index 0000000..73a66a7 --- /dev/null +++ b/app/app.iml @@ -0,0 +1,97 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..2633807 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,26 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 23 + buildToolsVersion "23.0.2" + + defaultConfig { + applicationId "profick.simplecalculator" + minSdkVersion 15 + targetSdkVersion 23 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + testCompile 'junit:junit:4.12' + compile 'com.android.support:appcompat-v7:23.1.0' +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..061f802 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in C:\android\sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/app/src/androidTest/java/profick/simplecalculator/ApplicationTest.java b/app/src/androidTest/java/profick/simplecalculator/ApplicationTest.java new file mode 100644 index 0000000..ed5a24a --- /dev/null +++ b/app/src/androidTest/java/profick/simplecalculator/ApplicationTest.java @@ -0,0 +1,13 @@ +package profick.simplecalculator; + +import android.app.Application; +import android.test.ApplicationTestCase; + +/** + * Testing Fundamentals + */ +public class ApplicationTest extends ApplicationTestCase { + public ApplicationTest() { + super(Application.class); + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..1163b8a --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + diff --git a/app/src/main/java/profick/simplecalculator/MainActivity.java b/app/src/main/java/profick/simplecalculator/MainActivity.java new file mode 100644 index 0000000..d718d02 --- /dev/null +++ b/app/src/main/java/profick/simplecalculator/MainActivity.java @@ -0,0 +1,224 @@ +package profick.simplecalculator; + +import android.graphics.Color; +import android.os.Bundle; +import android.support.v4.content.ContextCompat; +import android.support.v4.content.res.ResourcesCompat; +import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; +import android.widget.LinearLayout; +import android.widget.TextView; + +import java.util.Random; + + +public class MainActivity extends AppCompatActivity implements OnClickListener { + + TextView tvResult; + private StringBuilder stringNumber; + private double number; + private boolean isFirst; + private char operation; + + + private void toScreen(String str) { + if (str.length() > 2) { + if (str.charAt(str.length() - 2) == '.' && str.charAt(str.length() - 1) == '0') { + str = str.substring(0, str.length() - 2); + } + } + + + tvResult.setText(str); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.myscreen); + + if (savedInstanceState == null) { + stringNumber = new StringBuilder(); + clearStringNumber(); + number = 0; + isFirst = true; + operation = 0; + } + + + findViewById(R.id.buttonZero).setOnClickListener(this); + findViewById(R.id.buttonOne).setOnClickListener(this); + findViewById(R.id.buttonTwo).setOnClickListener(this); + findViewById(R.id.buttonThree).setOnClickListener(this); + findViewById(R.id.buttonFour).setOnClickListener(this); + findViewById(R.id.buttonFive).setOnClickListener(this); + findViewById(R.id.buttonSix).setOnClickListener(this); + findViewById(R.id.buttonSeven).setOnClickListener(this); + findViewById(R.id.buttonEight).setOnClickListener(this); + findViewById(R.id.buttonNine).setOnClickListener(this); + findViewById(R.id.buttonPoint).setOnClickListener(this); + findViewById(R.id.buttonAdd).setOnClickListener(this); + findViewById(R.id.buttonSub).setOnClickListener(this); + findViewById(R.id.buttonMul).setOnClickListener(this); + findViewById(R.id.buttonDiv).setOnClickListener(this); + findViewById(R.id.buttonAC).setOnClickListener(this); + findViewById(R.id.buttonRNG).setOnClickListener(this); + findViewById(R.id.buttonEqual).setOnClickListener(this); + findViewById(R.id.buttonSign).setOnClickListener(this); + + tvResult = (TextView) findViewById(R.id.textViewResult); + tvResult.setText(stringNumber); + + } + + + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + outState.putString("stringNumber", stringNumber.toString()); + outState.putChar("operation", operation); + outState.putDouble("number", number); + outState.putBoolean("isFirst", isFirst); + } + + @Override + protected void onRestoreInstanceState(Bundle savedInstanceState) { + super.onRestoreInstanceState(savedInstanceState); + stringNumber = new StringBuilder(savedInstanceState.getString("stringNumber")); + operation = savedInstanceState.getChar("operation"); + getButton(operation).setBackgroundColor(Color.GREEN); + number = savedInstanceState.getDouble("number"); + isFirst = savedInstanceState.getBoolean("isFirst"); + + } + + @Override + public void onClick(View v) { + switch (v.getId()) { + case R.id.buttonZero: pushDigit('0'); break; + case R.id.buttonOne: pushDigit('1'); break; + case R.id.buttonTwo: pushDigit('2'); break; + case R.id.buttonThree: pushDigit('3'); break; + case R.id.buttonFour: pushDigit('4'); break; + case R.id.buttonFive: pushDigit('5'); break; + case R.id.buttonSix: pushDigit('6'); break; + case R.id.buttonSeven: pushDigit('7'); break; + case R.id.buttonEight: pushDigit('8'); break; + case R.id.buttonNine: pushDigit('9'); break; + + case R.id.buttonAdd: pushOperation('+'); break; + case R.id.buttonSub: pushOperation('-'); break; + case R.id.buttonMul: pushOperation('*'); break; + case R.id.buttonDiv: pushOperation('/'); break; + + case R.id.buttonSign: changeSign(); break; + case R.id.buttonAC: pushAC(); break; + case R.id.buttonRNG: pushRNG(); break; + case R.id.buttonEqual: pushEqual(); break; + case R.id.buttonPoint: pushPoint(); break; + + + + } + } + + private void pushDigit(char digit) { + if (stringNumber.length() == 1 && stringNumber.charAt(0) == '0') { + if (digit == '0') { + return; + } + stringNumber.setLength(0); + + } + stringNumber.append(digit); + toScreen(stringNumber.toString()); + } + + private void pushOperation(char oper) { + if (isFirst) { + number = Double.parseDouble(stringNumber.toString()); + isFirst = false; + stringNumber.setLength(0); + stringNumber.append('0'); + } + + if (operation != 0) { + getButton(operation).setBackgroundColor(new ResourcesCompat().getColor(getResources(), R.color.orange, null)); + } + getButton(oper).setBackgroundColor(Color.GREEN); + + operation = oper; +// toScreen(String.valueOf(number)); + } + + private void pushPoint() { + if (!stringNumber.toString().contains(".")) { + stringNumber.append('.'); + } + toScreen(stringNumber.toString()); + } + + private void changeSign() { + if (stringNumber.charAt(0) == '-') { + stringNumber.delete(0, 1); + } else { + stringNumber.insert(0, "-"); + } + toScreen(stringNumber.toString()); + } + + private void pushAC() { + number = 0; + clearStringNumber(); + isFirst = true; + getButton(operation).setBackgroundColor(new ResourcesCompat().getColor(getResources(), R.color.orange, null)); + operation = 0; + toScreen(stringNumber.toString()); + } + + private void pushRNG() { + stringNumber = new StringBuilder(Integer.toString(new Random().nextInt(100))); + toScreen(stringNumber.toString()); + } + + private void pushEqual() { + getButton(operation).setBackgroundColor(new ResourcesCompat().getColor(getResources(), R.color.orange, null)); + switch (operation) { + case '+': + number += Double.parseDouble(stringNumber.toString()); + clearStringNumber(); + break; + case '-': + number -= Double.parseDouble(stringNumber.toString()); + clearStringNumber(); + break; + case '*': + number *= Double.parseDouble(stringNumber.toString()); + clearStringNumber(); + break; + case '/': + number /= Double.parseDouble(stringNumber.toString()); + clearStringNumber(); + break; + } + toScreen(String.valueOf(number)); + } + + private void clearStringNumber() { + stringNumber.setLength(0); + stringNumber.append('0'); + } + + private Button getButton(char ch) { + switch (ch) { + case '+': return (Button)findViewById(R.id.buttonAdd); + case '-': return (Button)findViewById(R.id.buttonSub); + case '*': return (Button)findViewById(R.id.buttonMul); + default: return (Button)findViewById(R.id.buttonDiv); + } + } + +} diff --git a/app/src/main/res/layout-land/myscreen.xml b/app/src/main/res/layout-land/myscreen.xml new file mode 100644 index 0000000..9f7459a --- /dev/null +++ b/app/src/main/res/layout-land/myscreen.xml @@ -0,0 +1,216 @@ + + + + + + + + +