Loading...

Android Studio 2.3 陽春型計算機進階版

Temperature: 0 °C

ChungChung
author_tools


Android Studio 2.3 陽春型計算機進階版
將之前的陽春型計算機用了不怎麼聰明的方式改善了幾個地方。

首先,將數字的宣告由int變成了double,這是為了讓運算答案能夠顯示小數點。

加上了含稅跟未稅的按鈕。

MainActivity.java部分

package com.innstory.calculator;



import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.text.NumberFormat;
import android.widget.EditText;



public class MainActivity extends AppCompatActivity
implements View.OnClickListener, View.OnLongClickListener{

//定義變數
EditText ans_list_s,ans_list;//輸出點擊按鈕及輸出結果
Button bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,bt0,bt_add,bt_less,bt_Multiply
,bt_except,bt_ac,bt_answer,bt_dot,bt_Tax,bt_noTax;//按鈕
double previous = 0;//儲存上1筆資料
double counter = 0; //目前的值
double counter_ = 0;//原運算值
String Operation;//運算符號
String dot;//符號


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ans_list = (EditText) findViewById(R.id.ans_list);
ans_list_s = (EditText) findViewById(R.id.ans_list_s);
bt1 = (Button) findViewById(R.id.button1);
bt2 = (Button) findViewById(R.id.button2);
bt3 = (Button) findViewById(R.id.button3);
bt4 = (Button) findViewById(R.id.button4);
bt5 = (Button) findViewById(R.id.button5);
bt6 = (Button) findViewById(R.id.button6);
bt7 = (Button) findViewById(R.id.button7);
bt8 = (Button) findViewById(R.id.button8);
bt9 = (Button) findViewById(R.id.button9);
bt0 = (Button) findViewById(R.id.button0);
bt_add = (Button) findViewById(R.id.button_add);
bt_less = (Button) findViewById(R.id.button_less);
bt_Multiply = (Button) findViewById(R.id.button_Multiply);
bt_except = (Button) findViewById(R.id.button_except);
bt_ac = (Button) findViewById(R.id.button_ac);
bt_answer = (Button) findViewById(R.id.button_answer);
bt_dot = (Button) findViewById(R.id.button_dot);
bt_Tax = (Button) findViewById(R.id.button_Tax);
bt_noTax = (Button) findViewById(R.id.button_noTax);

bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
bt4.setOnClickListener(this);
bt5.setOnClickListener(this);
bt6.setOnClickListener(this);
bt7.setOnClickListener(this);
bt8.setOnClickListener(this);
bt9.setOnClickListener(this);
bt0.setOnClickListener(this);
bt_dot.setOnClickListener(this);
bt_add.setOnClickListener(this);
bt_less.setOnClickListener(this);
bt_Multiply.setOnClickListener(this);
bt_except.setOnClickListener(this);
bt_ac.setOnClickListener(this);
bt_answer.setOnClickListener(this);
bt_Tax.setOnClickListener(this);
bt_noTax.setOnClickListener(this);

}



@Override
public void onClick(View v) {

NumberFormat nf = NumberFormat.getInstance(); // 數字格式
nf.setMaximumFractionDigits(4); // 限制小數第四位

if (v.getId() == R.id.button1){

if (dot != null){

if (dot == "1"){
counter = counter + 0.01;
}else {
counter = counter / 10 + 1 * 0.1;
}
dot = "1";
}else{
counter = counter * 10 + 1; // x 10 原因為了進位
}

ans_list.setText(ans_list.getText() + "1");
ans_list_s.setText("" + counter);
} else if (v.getId() == R.id.button2){

if (dot != null){
if (dot == "1"){
counter = counter + 0.02;
}else {
counter = counter / 10 + 2 * 0.1;
}
dot = "1";
}else{
counter = counter * 10 + 2;
}

ans_list.setText(ans_list.getText() + "2");
ans_list_s.setText("" + counter);
} else if (v.getId() == R.id.button3){

if (dot != null){
if (dot == "1"){
counter = counter + 0.03;
}else {
counter = counter / 10 + 3 * 0.1;
}
dot = "1";
}else{
counter = counter * 10 + 3;
}
ans_list.setText(ans_list.getText() + "3");
ans_list_s.setText("" + counter);
} else if (v.getId() == R.id.button4){

if (dot != null){
if (dot == "1"){
counter = counter + 0.04;
}else {
counter = counter / 10 + 4 * 0.1;
}
dot = "1";
}else{
counter = counter * 10 + 4;
}
ans_list.setText(ans_list.getText() + "4");
ans_list_s.setText("" + counter);
} else if (v.getId() == R.id.button5){

if (dot != null){
if (dot == "1"){
counter = counter + 0.05;
}else {
counter = counter / 10 + 5 * 0.1;
}
dot = "1";
}else{
counter = counter * 10 + 5;
}
ans_list.setText(ans_list.getText() + "5");
ans_list_s.setText("" + counter);
} else if (v.getId() == R.id.button6){

if (dot != null){
if (dot == "1"){
counter = counter + 0.06;
}else {
counter = counter / 10 + 6 * 0.1;
}
dot = "1";
}else{
counter = counter * 10 + 6;
}
ans_list.setText(ans_list.getText() + "6");
ans_list_s.setText("" + counter);
} else if (v.getId() == R.id.button7){

if (dot != null){
if (dot == "1"){
counter = counter + 0.07;
}else {
counter = counter / 10 + 7 * 0.1;
}
dot = "1";
}else{
counter = counter * 10 + 7;
}
ans_list.setText(ans_list.getText() + "7");
ans_list_s.setText("" + counter);
} else if (v.getId() == R.id.button8){

if (dot != null){
if (dot == "1"){
counter = counter + 0.08;
}else {
counter = counter / 10 + 8 * 0.1;
}
dot = "1";
}else{
counter = counter * 10 + 8;
}
ans_list.setText(ans_list.getText() + "8");
ans_list_s.setText("" + counter);
} else if (v.getId() == R.id.button9){

if (dot != null){
if (dot == "1"){
counter = counter + 0.09;
}else {
counter = counter / 10 + 9 * 0.1;
}
dot = "1";
}else{
counter = counter * 10 + 9;
}
ans_list.setText(ans_list.getText() + "9");
ans_list_s.setText("" + counter);
} else if (v.getId() == R.id.button0){

if (dot != null){
if (dot == "1"){
counter = counter + 0.00;
}else {
counter = counter / 10 + 0 * 0.1;
}
dot = "1";
}else{
counter = counter * 10 + 0;
}
ans_list.setText(ans_list.getText() + "0");
ans_list_s.setText("" + counter);
} else if (v.getId() == R.id.button_dot){
counter = counter * 10;
dot = "yes";
ans_list.setText(ans_list.getText() + ".");

} else if (v.getId() == R.id.button_ac){
counter = 0;
ans_list.setText(String.valueOf(""));
ans_list_s.setText(String.valueOf(""));
dot = null;
}


if (v.getId() == R.id.button_Tax){
ans_list.setText(nf.format(counter * 1.05));//含稅
previous = 0;
}

if (v.getId() == R.id.button_noTax){
ans_list.setText(nf.format(counter * 1.05 / 1.05));//未稅
previous = 0;
}



//符號
if (v.getId() == R.id.button_add){
previous = counter;
counter = 0;
dot = null;
Operation = bt_add.getText().toString();
ans_list.setText(String.valueOf(Operation));
}

else if (v.getId() == R.id.button_less){
previous = counter;
counter = 0;
dot = null;
Operation = bt_less.getText().toString();
ans_list.setText(String.valueOf(Operation));
}

else if (v.getId() == R.id.button_Multiply){
previous = counter;
counter = 0;
dot = null;
Operation = bt_Multiply.getText().toString();
ans_list.setText(String.valueOf(Operation));
}

else if (v.getId() == R.id.button_except){
previous = counter;
counter = 0;
dot = null;
Operation = bt_except.getText().toString();
ans_list.setText(String.valueOf(Operation));
}



//運算
if (v.getId() == R.id.button_answer){

if (Operation.equals("+")) {
counter = previous + counter;
counter_ = counter - previous;
ans_list.setText(String.valueOf(counter));
ans_list_s.setText("運算子 = " + previous + " + " + counter_);
previous = 0;
Operation = "";
} else if (Operation.equals("-")) {
counter = previous - counter;
counter_ = previous - counter;
ans_list.setText(String.valueOf(counter));
ans_list_s.setText("運算子 = " + previous + " - " + counter_);
previous = 0;
Operation = "";
} else if (Operation.equals("×")) {
counter = previous * counter;
counter_ = counter / previous;
ans_list.setText(String.valueOf(counter));
ans_list_s.setText("運算子 = " + previous + " × " + counter_);
previous = 0;
Operation = "";
} else if (Operation.equals("÷")) {
if (counter == 0) {
ans_list.setText("被除數除數不能為0");
previous = 0;
} else {
counter = previous / counter;
counter_ = previous / counter;
ans_list.setText(nf.format(counter));
ans_list_s.setText("運算子 = " + previous + " ÷ " + counter_);
previous = 0;
}
Operation = "";
}


}


}

@Override
public boolean onLongClick(View v) {
return false;
}
}

說了是不聰明的方式,主要是在顯示小數點的部分。

其實想做到的是可以自動判別小數點後面有幾個數字。

但目前仍想不出用甚麼方式,索性就用了不聰明的方式做判斷。

而且這個判斷目前也只能讓輸入小數點後2位數字,多了就會出問題了(╯з╰)

總之,研究中。

台北市內湖區江南街71巷75弄 Go

https://innstory.com/story-Android_Studio_23_陽春型計算機進階版-1193
寫程式筆記

Prev
 淡紅色小藥丸

Next
今早我從睡夢中驚醒 

Nearby Attractions

台北市內湖區江南街71巷75弄-Nearby Attractions

  • 原臺灣軍司令部 Go
  • 臺北府城—東門、南門、小南門... Go
  • 總督府農業試驗所昆蟲部 Go
  • 內湖清代採石場 Go
  • 艋舺龍山寺 Go
  • 番學堂遺構 Go

About the Author

Chung

我是chung
網路工作者
主業是網站系統開發建置
副業是做夢,寫故事
作品請參考/teme.biz
做夢請參考/innstory.com
聯絡/chung.teme@gmail.com

#有人用筆寫日記,有人用歲月寫日記,有人用照片寫日記,而我,用innstory寫日記。

Visitor message

Leave some footprints to prove that you visited me

Recommended reading

Author's other related stories

Android Studio 2.3 陽春型計算機

Android Studio...

andrio55 原本這次的學習課題是,與使用者互動之「按一下」事件處理。 我按照工具書上寫的,完成...

動態留言功能調整

動態留言功能調整

guestmap55 最近改進了朋友動態的發文方式.... 一開始在功能方面加上了googlemap...

Mysql 查詢時間區間是否包含特定日期

Mysql 查詢時間區間是否...

這其實只是一個簡單的問題,以前並不常用... 但這陣子寫的案子比較偏系統面,這樣的查詢問題變多了@@...

Recommended reading

Other stories happened at this address

青椒炒肉絲佐高粱

青椒炒肉絲佐高粱

很簡單,卻也很下飯。 家裡買的沙茶也放了一陣子了,這晚就決定來盤沙茶口味的青椒炒肉絲。 其實我也不清...

5U的第1張生日卡片

5U的第1張生日卡片

s U的第張生日卡片,謝謝威廷跟瓊慧的用心。 U也謝謝威廷姊夫跟瓊慧姊姊\(^o^)/ 感覺這週是U...

筋疲力盡

筋疲力盡

在洗澡間淋著浴,這是我稍稍可以喘息的時間。 腦袋裡閃過Men of Honor裡的台詞~ 這是我非常...

看不透的事情太多~

看不透的事情太多~

s 看不透的事情太多,只是讓自己徒增了許多煩惱。 知道容易,實際做起來卻真的挺難的。

HTML5 Geolocation

HTML5 Geolocat...

說真的,HTML Geolocation的功能真的好用... 輕鬆就可以讓網站也能取得目前所在的地理...

PHP取得檔案路徑及檔案名稱

PHP取得檔案路徑及檔案名稱...

例如我有一支檔案是放在根目錄下的demo資料夾中,檔案名稱為test.php 、首先取得目前網頁的檔...

Please select an option

error

Hi, thank you for your participation, but you cannot vote repeatedly~

Join innstory now and start recording your story.

"Innstory" is a place to store stories. We are committed to becoming a warm platform. Deepening the bonds between people is our direction.
We are convinced that the blockchain between people is not just a cold calculation. Join us now.

Wrong format