Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.nguye.chngtrnhtnhchsbmi
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.os.Message
- import android.widget.Button
- import android.widget.EditText
- import android.widget.Toast
- import kotlinx.android.synthetic.main.activity_main.*
- import java.math.RoundingMode
- import java.text.DecimalFormat
- import kotlin.math.pow
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- button_TinhBMI.setOnClickListener(){
- Tinh()
- }
- }
- fun NgoaiLeTen(message: String): Nothing
- {
- throw Exception(message)
- }
- fun Tinh()
- {
- try {
- val butChanDoan = findViewById<Button>(R.id.button_TinhBMI)
- val editTen = findViewById<EditText>(R.id.editText_ten)
- val editChieuCao = findViewById<EditText>(R.id.editText_ChieuCao)
- val editCanNang = findViewById<EditText>(R.id.editText_CanNang)
- val editBMI = findViewById<EditText>(R.id.editText_BMI)
- val editChanDoan = findViewById<EditText>(R.id.editText_ChanDoan)
- val H: Double = editChieuCao.text.toString().toDouble()
- val W: Double = editCanNang.text.toString().toDouble()
- val BMI: Double = W / (H / 100.0).pow(2)
- var chuandoan = ""
- val HoTen = editTen.text.toString().trim()
- var isNum = true
- isNum = HoTen.matches("-?\\d+(\\.\\d+)?".toRegex())
- if (HoTen == "" || isNum)
- throw NgoaiLeTen("Vui lòng nhập lại tên")
- if (BMI < 18)
- chuandoan = "Bạn gầy"
- else if (BMI <= 24.9)
- chuandoan = "Bạn bình thường"
- else if (BMI <= 29.9)
- chuandoan = "Bạn béo phì độ 1"
- else if (BMI <= 34.9)
- chuandoan = "Bạn béo phì độ 2"
- else
- chuandoan = "Khủng long"
- val df = DecimalFormat("#.0")
- df.roundingMode = RoundingMode.CEILING
- Toast.makeText(this@MainActivity, "Vui lòng chờ! Bạn đã Click vào nút" + butChanDoan.text.toString(), Toast.LENGTH_LONG).show()
- Toast.makeText(this@MainActivity, "Xin chào " + editTen.text.toString() + " đã có kết quả!", Toast.LENGTH_LONG).show()
- editBMI.setText(df.format(BMI))
- editChanDoan.setText(chuandoan)
- }
- catch (ex: NullPointerException)
- {
- Toast.makeText(this@MainActivity, "Lỗi NullPointerException", Toast.LENGTH_LONG).show()
- }
- catch (ex: Exception)
- {
- Toast.makeText(this@MainActivity, "Không hợp lệ. " + ex.message, Toast.LENGTH_LONG).show()
- }
- }
- }
Add Comment
Please, Sign In to add comment