Last active
June 5, 2016 13:16
-
-
Save mikkipastel/abefbb6c7df0eccf435f2bbf78ecd564 to your computer and use it in GitHub Desktop.
intercheese_sample1
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.mikkipastel.sample; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.RadioButton; | |
import android.widget.RadioGroup; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
public class MainActivity extends AppCompatActivity { | |
EditText input1, input2; | |
TextView result; | |
Button cal; | |
RadioGroup operator; | |
//RadioButton plus, minus, mutiply, divide; | |
int in1, in2, out; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
input1 = (EditText)findViewById(R.id.input1); | |
input2 = (EditText)findViewById(R.id.input2); | |
result = (TextView)findViewById(R.id.output); | |
cal = (Button)findViewById(R.id.calculate); | |
//old | |
//plus = (RadioButton)findViewById(R.id.rbPlus); | |
//minus = (RadioButton)findViewById(R.id.rbMinus); | |
//mutiply = (RadioButton)findViewById(R.id.rbMutiply); | |
//divide = (RadioButton)findViewById(R.id.rbDivide); | |
operator = (RadioGroup)findViewById(R.id.rgOperation); | |
cal.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
try { | |
in1 = Integer.parseInt(input1.getText().toString()); | |
} | |
catch (NumberFormatException e) { | |
} | |
try { | |
in2 = Integer.parseInt(input2.getText().toString()); | |
} | |
catch (NumberFormatException e) { | |
} | |
switch (operator.getCheckedRadioButtonId()){ | |
case R.id.rbPlus: //plus.isChecked() | |
out = in1 + in2; | |
break; | |
case R.id.rbMinus: //minus.isChecked() | |
out = in1 - in2; | |
break; | |
case R.id.rbMutiply: //multiply.isChecked() | |
out = in1 * in2; | |
break; | |
case R.id.rbDivide: //divide.isChecked() | |
if (in2 != 0){ | |
out = in1 / in2; | |
} else { | |
Toast.makeText(MainActivity.this, "Divide by Zero", Toast.LENGTH_SHORT).show(); | |
} | |
break; | |
default: | |
out = 0; | |
} | |
result.setText(out + ""); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment