3/20 수업
(일단 저번 수업 내용)
여기 만약 Number이 아니고 Plain Text라면, 클릭했을때 모든 자판이 보인다.
이런 상황에서 프로그램을 실행하면 상황에 따라 소수점 자리 처리를 하느냐 마느냐에 따라 프로그램이 제대로 처리 안될수도 있음.
그러므로 예외처리를 잘 해줘야 함.
View Binding: 눈에 보이는 객체들을 다른 객체들과 연결해준다.
View Binding하는 법
위에 뜨는 Sync Now 클릭.
14,20,21 line 내용을 적어준다.
객체 이름이므로 binding 말고 다른 이름으로 써도 된다.
layout 패키지에 있는 파일이름이 activity_main.xml 이므로 ActivityMainBinding 이라는 이름으로 자동으로 바뀌는것.
lateinit var binding: ActivityMainBinding
lateinit: 늦게 초기화를 하겠다 라는 뜻.
lateinit 안 쓰면 에러가 뜬다.
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
inflate : 부풀리다라는 뜻. layoutInflater로 화면을 꽉 채운다는 뜻임.
위 두 문장과
setContentView(R.layout.activity_main)
이 문장은 동일함.
아래 전체 코드 첨부
package com.example.gradetest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.RadioButton
import android.widget.Switch
import android.widget.TextView
import com.example.gradetest.databinding.ActivityMainBinding
import kotlin.math.roundToInt
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val swr: Switch = findViewById(R.id.swRound)
binding.btnOK.setOnClickListener{
val pr = binding.txtPrice.text.toString().toDouble()
var res: Double = pr
if (binding.rbGrade1.isChecked) res += pr * 0.20
if (binding.rbGrade2.isChecked) res += pr * 0.15
if (binding.rbGrade3.isChecked) res -= pr * 0.10
if (binding.swRound.isChecked){
res = kotlin.math.ceil(res)
}
binding.txtResult.text = res.roundToInt().toString()
}
}
}
'수업' 카테고리의 다른 글
코드 디버깅하는 법, 에러 체크하는 법, Log.e 사용, kotlin (0) | 2023.03.21 |
---|---|
모바일프로그래밍 - spinner 사용 (0) | 2023.03.20 |
웹 프로그래밍 - 02. HTML 폼 태그 (0) | 2023.03.20 |
컴퓨터그래픽스 - 3. 컴퓨터 그래픽스 컬러모델 (2) | 2023.03.19 |
객체지향소프트웨어공학 - 시스템공학과 소프트웨어공학 (0) | 2023.03.19 |