Loading...

Android Studio 2.3 陽春型計算機

Temperature: 0 °C

ChungChung
author_tools


Android Studio 2.3 陽春型計算機
原本這次的學習課題是,與使用者互動之「按一下」事件處理。

按照工具書上寫的,完成了按一下Button 會達到累加數字的效果。

但是沒耐心的,總覺得這樣的學習方式好枯燥無味。

因此,為了讓學習變有趣,既然是「按一下」事件處理,那這次索性訂了個專題,寫個計算機吧。

MainActivity.java部分

package com.innstory.computer;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;




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

//定義變數
TextView 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;//按鈕
int previous = 0;//儲存上1筆資料
int counter = 0; //目前的值
int counter_ = 0;//原運算值
String Operation;//運算符號


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ans_list = (TextView) findViewById(R.id.ans_list);
ans_list_s = (TextView) 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);

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_add.setOnClickListener(this);
bt_less.setOnClickListener(this);
bt_Multiply.setOnClickListener(this);
bt_except.setOnClickListener(this);
bt_ac.setOnClickListener(this);
bt_answer.setOnClickListener(this);

}


@Override
public void onClick(View v) {
if (v.getId() == R.id.button1){
counter = counter * 10 + 1; // x 10 原因為了進位
} else if (v.getId() == R.id.button2){
counter = counter * 10 + 2;
} else if (v.getId() == R.id.button3){
counter = counter * 10 + 3;
} else if (v.getId() == R.id.button4){
counter = counter * 10 + 4;
} else if (v.getId() == R.id.button5){
counter = counter * 10 + 5;
} else if (v.getId() == R.id.button6){
counter = counter * 10 + 6;
} else if (v.getId() == R.id.button7){
counter = counter * 10 + 7;
} else if (v.getId() == R.id.button8){
counter = counter * 10 + 8;
} else if (v.getId() == R.id.button9){
counter = counter * 10 + 9;
} else if (v.getId() == R.id.button0){
counter = counter * 10 + 0;
} else if (v.getId() == R.id.button_ac){
counter = 0;
ans_list.setText(String.valueOf(counter));
ans_list_s.setText(String.valueOf(counter));
}

ans_list.setText(String.valueOf(counter));

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

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

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

else if (v.getId() == R.id.button_except){
previous = counter;
counter = 0;
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;
}

else if(Operation.equals("-")){
counter = previous - counter;
counter_ = previous - counter;
ans_list.setText(String.valueOf(counter));
ans_list_s.setText("運算子 = " + previous + " - " + counter_);
previous = 0;
}

else if(Operation.equals("×")){
counter = previous * counter;
counter_ = counter / previous;
ans_list.setText(String.valueOf(counter));
ans_list_s.setText("運算子 = " + previous + " × " + counter_);
previous = 0;
}

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


}

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

當然,這只是一個極度陽春的計算機功能。

有一些bug可能也還需要研究怎麼處理,UI部分也要研究如何讓他變漂亮。

嗯,其實還可以多加個「未稅」「含稅」的Button。

但撇開這些缺點不談,他的確是個計算機了。

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

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

Prev
 就是要這樣挨著

Next
自從當了爸購物時看到6都會有莫名的感(衝)動 

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

瀏覽器升級ie9後FCKEditor無法使用對話框解決方法

瀏覽器升級ie9後FCKEd...

原文網址:http://ecshop.tw/bbs/archiver/tid.html 升級到 IE...

校外教學(木柵動物園)

校外教學(木柵動物園)

期中考週的校外教學,來到了上回去不成的木柵動物園。 炎熱的天,帶著位小蘿蔔頭,一邊觀察一邊完成老師交...

地圖故事

地圖故事

map 一切都從第一張畫布開始... Innstory裡原先已有寫故事的功能,接著我把故事裡頭的照片...

Recommended reading

Other stories happened at this address

1歲5個月

1歲5個月

s 看著他漸漸長大,其實還真有點忘記算他目前是多大了~ 歲個月,我想,以後躺在我腳上就睡著的時間慢慢...

這幾天,有點閒~

這幾天,有點閒~

這幾天有點閒~除了明天得去一趟高雄出差外....好像也沒什麼特別要忙的... 因為說好了,到農曆新年...

植入章節的功能

植入章節的功能

在寫故事的時候,我常會有機會跳著寫~ 例如寫下旅遊記事時,寫了第一天,正準備要寫第二天時卻因為剛好有...

拼圖時間

拼圖時間

父子時間。 發現原來玩拼圖也可以教母語,我一邊拚著一邊用客家話要U去找同顏色的拼圖。 這傢伙就一直檢...

今早我從睡夢中驚醒!!

今早我從睡夢中驚醒!!

s 說真~從睡夢中驚醒的例子太多了,但我從沒遇過今天早上的情形⊙⊙ U最近喜歡吃玉米,他可以雙手捧著...

新舊交替,使用phpmyadm出現1064 error in CREATE TABLE … TYPEMYISAM錯誤

新舊交替,使用phpmyad...

sql 系統移機的過程中,最重要的環節....我想...莫過於是系統的環境了....。 這次在協助移...

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