quilt code

[kotlin] 버튼 클릭 이벤트 (2) 본문

daily/Android

[kotlin] 버튼 클릭 이벤트 (2)

김뱅쇼 2023. 5. 26. 15:46

 

 


increase 클릭할때마다 숫자 하나씩 올라가기




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package kr.co.aiai.app
 
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import org.w3c.dom.Text
 
// : 상속
class MainActivity2 : AppCompatActivity() {
    //전역변수
    var tv:TextView? = null;
 
    //메소드
    //fun : 파이썬의 def
    override fun onCreate(savedInstanceState: Bundle?) {
        //super:자기 조상 호출
        super.onCreate(savedInstanceState)
        //화면으로 넘어가는 명령어
        setContentView(R.layout.activity_main2)
 
        //레이아웃 선언
        tv= findViewById<TextView>(R.id.tv)
        val btn : Button = findViewById(R.id.btn)
 
        //increase 버튼 클릭할 때마다 1씩 증가
        btn.setOnClickListener{
            myclick()
 
        }
 
 
    }
 
    fun myclick(){
        var a:String = tv?.text as String
        var aa:Int = a.toInt() //String을 명시 해줘야 toInt가 나옴
 
        aa++
 
        tv?.text=aa.toString()
 
 
    }
 
}
 
 
 
 
 
 
 
 
 
cs




클릭






 

구글링 했을땐 counter가 나와서 그대로 갖다 붙였는데 안됐다.. 

이유는 변수명이 달라서.. 개멍청!

 

'daily > Android' 카테고리의 다른 글

[kotlin] 버튼 클릭 이벤트 (1)  (0) 2023.05.26
[kotlin] 로또 번호 생성  (0) 2023.05.26