Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. package com.example.simplecalculator6088102
  2.  
  3. import android.app.AlertDialog
  4. import android.content.Context
  5. import android.os.Bundle
  6. import android.view.View
  7. import android.widget.Button
  8. import android.widget.EditText
  9. import android.widget.TextView
  10. import androidx.appcompat.app.AppCompatActivity
  11.  
  12. class MainActivity : AppCompatActivity() {
  13. private var input1: EditText? = null
  14. private var input2: EditText? = null
  15. private var solution: EditText? = null
  16. private var operator: TextView? = null
  17. private val mContext: Context? = null
  18.  
  19. override fun onCreate(savedInstanceState: Bundle?) {
  20. super.onCreate(savedInstanceState)
  21. setContentView(R.layout.activity_main)
  22. input1 = findViewById<View>(R.id.editText) as EditText
  23. input2 = findViewById<View>(R.id.editText2) as EditText
  24. solution = findViewById<View>(R.id.editText3) as EditText
  25. operator = findViewById<View>(R.id.textView) as TextView
  26. val plusButton = findViewById<View>(R.id.button) as Button
  27. val minusButton = findViewById<View>(R.id.button2) as Button
  28. val mulButton = findViewById<View>(R.id.button3) as Button
  29. val divButton = findViewById<View>(R.id.button4) as Button
  30. val equalButton = findViewById<View>(R.id.button5) as Button
  31. plusButton.setOnClickListener { operator!!.text = "+" }
  32. minusButton.setOnClickListener { operator!!.text = "-" }
  33. mulButton.setOnClickListener { operator!!.text = "*" }
  34. divButton.setOnClickListener { operator!!.text = "/" }
  35. equalButton.setOnClickListener {
  36. if (input1!!.text.length == 0
  37. || input1!!.text.toString() === ""
  38. || input2!!.text.length == 0
  39. || input2!!.text.toString() === "") {
  40. AlertDialog.Builder(mContext).setTitle("Error")
  41. .setMessage("Some inputs are empty")
  42. .setPositiveButton("OK", null).show()
  43. } else if (operator!!.text == "") {
  44. AlertDialog.Builder(mContext).setTitle("Error")
  45. .setMessage("Operator is null")
  46. .setPositiveButton("OK", null).show()
  47. } else if (operator!!.text == "+") {
  48. val result: Double = input1!!.text.toString().toDouble() + input2!!.text.toString().toDouble()
  49. solution!!.setText(java.lang.Double.toString(result))
  50. } else if (operator!!.text == "-") {
  51. val result: Double = input1!!.text.toString().toDouble() - input2!!.text.toString().toDouble()
  52. solution!!.setText(java.lang.Double.toString(result))
  53. } else if (operator!!.text == "*") {
  54. val result: Double = input1!!.text.toString().toDouble() * input2!!.text.toString().toDouble()
  55. solution!!.setText(java.lang.Double.toString(result))
  56. } else if (operator!!.text == "/") {
  57. val result: Double = input1!!.text.toString().toDouble() / input2!!.text.toString().toDouble()
  58. solution!!.setText(java.lang.Double.toString(result))
  59. }
  60. }
  61. }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement