Created
August 9, 2013 01:41
-
-
Save uhfx/6190495 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.dentakutest; | |
import android.os.Bundle; | |
import android.app.Activity; | |
import android.view.Menu; | |
import android.view.View; | |
import android.widget.TextView; | |
public class MainActivity extends Activity { | |
float number1; | |
float number2; | |
float answer; | |
float ope; | |
TextView num1Text; | |
TextView opeText; | |
TextView num2Text; | |
TextView answerText; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
//各変数を初期化 | |
number1 = 0; | |
number2 = 0; | |
answer = 0; | |
ope = 0; | |
//ラベルを関連づけ | |
num1Text = (TextView)findViewById(R.id.num1); | |
opeText = (TextView)findViewById(R.id.ope); | |
num2Text = (TextView)findViewById(R.id.num2); | |
answerText = (TextView)findViewById(R.id.answer); | |
//ラベルの値を初期状態に | |
num1Text.setText("0"); | |
opeText.setText(""); | |
num2Text.setText(""); | |
answerText.setText(""); | |
} | |
//「1」ボタンが押されたとき | |
public void bt1 (View v){ | |
if (ope == 0){ | |
number1 = number1*10 + 1; | |
num1Text.setText(number1+""); | |
}else if (ope > 0){ | |
number2 = number2*10 + 1; | |
num2Text.setText(number2+""); | |
} | |
} | |
public void bt2 (View v){ | |
if (ope == 0){ | |
number1 = number1*10 + 2; | |
num1Text.setText(number1+""); | |
}else if (ope > 0){ | |
number2 = number2*10 + 2; | |
num2Text.setText(number2+""); | |
} | |
} | |
public void bt3 (View v){ | |
if (ope == 0){ | |
number1 = number1*10 + 3; | |
num1Text.setText(number1+""); | |
}else if (ope > 0){ | |
number2 = number2*10 + 3; | |
num2Text.setText(number2+""); | |
} | |
} | |
public void bt4 (View v){ | |
if (ope == 0){ | |
number1 = number1*10 + 4; | |
num1Text.setText(number1+""); | |
}else if (ope > 0){ | |
number2 = number2*10 + 4; | |
num2Text.setText(number2+""); | |
} | |
} | |
public void bt5 (View v){ | |
if (ope == 0){ | |
number1 = number1*10 + 5; | |
num1Text.setText(number1+""); | |
}else if (ope > 0){ | |
number2 = number2*10 + 5; | |
num2Text.setText(number2+""); | |
} | |
} | |
public void bt6 (View v){ | |
if (ope == 0){ | |
number1 = number1*10 + 6; | |
num1Text.setText(number1+""); | |
}else if (ope > 0){ | |
number2 = number2*10 + 6; | |
num2Text.setText(number2+""); | |
} | |
} | |
public void bt7 (View v){ | |
if (ope == 0){ | |
number1 = number1*10 + 7; | |
num1Text.setText(number1+""); | |
}else if (ope > 0){ | |
number2 = number2*10 + 7; | |
num2Text.setText(number2+""); | |
} | |
} | |
public void bt8 (View v){ | |
if (ope == 0){ | |
number1 = number1*10 + 8; | |
num1Text.setText(number1+""); | |
}else if (ope > 0){ | |
number2 = number2*10 + 8; | |
num2Text.setText(number2+""); | |
} | |
} | |
public void bt9 (View v){ | |
if (ope == 0){ | |
number1 = number1*10 + 9; | |
num1Text.setText(number1+""); | |
}else if (ope > 0){ | |
number2 = number2*10 + 9; | |
num2Text.setText(number2+""); | |
} | |
} | |
public void bt0 (View v){ | |
if (ope == 0){ | |
number1 = number1*10 + 0; | |
//+0はなくてもいい | |
num1Text.setText(number1+""); | |
}else if (ope > 0){ | |
number2 = number2*10 + 0; | |
num2Text.setText(number2+""); | |
} | |
} | |
//「+」ボタンが押されたとき | |
public void btplus (View v){ | |
ope = 1; | |
opeText.setText("+"); | |
num2Text.setText("0"); | |
} | |
public void btminus (View v){ | |
ope = 2; | |
opeText.setText("-"); | |
num2Text.setText("0"); | |
} | |
public void btmultiply (View v){ | |
ope = 3; | |
opeText.setText("*"); | |
num2Text.setText("0"); | |
} | |
public void btdivide (View v){ | |
ope = 4; | |
opeText.setText("/"); | |
num2Text.setText("0"); | |
} | |
//「=」ボタンが押されたとき | |
public void btequal (View v){ | |
if (ope == 1){ | |
answer = number1 + number2; | |
answerText.setText("=" + answer); | |
ope = -1; | |
}else if (ope == 2){ | |
answer = number1 - number2; | |
answerText.setText("=" + answer); | |
ope = -1; | |
}else if (ope == 3){ | |
answer = number1 * number2; | |
answerText.setText("=" + answer); | |
ope = -1; | |
}else if (ope == 4){ | |
answer = number1 / number2; | |
answerText.setText("=" + answer); | |
ope = -1; | |
} | |
} | |
public void btclear (View v){ | |
ope = 0; | |
opeText.setText(""); | |
number1 = 0; | |
num1Text.setText(""); | |
number2 = 0; | |
num2Text.setText(""); | |
answer = 0; | |
answerText.setText(""); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment