์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค ๋ทฐ ์ด๋ฒคํŠธ

2022. 12. 6. 17:55ใ†_Study/AndroidStudio

728x90

๋ทฐ ์ด๋ฒคํŠธ  ๐Ÿ‡¸.•*¨*•¸.•*¨*•¸.•*¨*•¸.•*¨*•

ํ•ด๋‹น ์ž๋ฃŒ๋Š” ๊ฐ•์˜ ํ•™์Šต์ž๋ฃŒ์ด๋ฉฐ, Do it! ๊นก์ƒ˜์˜ ์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ ํ”„๋กœ๊ทธ๋ž˜๋ฐ with ์ฝ”ํ‹€๋ฆฐ์„ ์ฐธ๊ณ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.


์ด๋ฒคํŠธ๋Š” ์•กํ‹ฐ๋น„ํ‹ฐ์˜ ํ„ฐ์น˜ ์ด๋ฒคํŠธ๋ฅผ ์ด์šฉํ•˜์ง€ ์•Š๊ณ  ๊ฐ ๋ทฐ์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ณ„๋„์˜ ์ด๋ฒคํŠธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๋Š” ์ด๋ฒคํŠธ ์†Œ์Šค์™€ ๋ฆฌ์Šค๋„ˆ(์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ)๋กœ ์—ญํ• ์ด ์•„๋‰˜๋ฉฐ ์ด ๋‘˜์„ ๋ฆฌ์Šค๋„ˆ ๋“ฑ๋ก ํ•จ์ˆ˜๋กœ ์—ฐ๊ฒฐํ•ด์•ผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

- ์ด๋ฒคํŠธ ์†Œ์Šค : ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฐ์ฒด

- ๋ฆฌ์Šค๋„ˆ(์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ) : ์ด๋ฒคํŠธ ๋ฐœ์ƒ์‹œ ์‹คํ–‰ํ•  ๋กœ์ง์ด ๊ตฌํ˜„๋œ ๊ฐ์ฒด

- ๋ฆฌ์Šค๋„ˆ ๋“ฑ๋กํ•จ์ˆ˜ : ์ด๋ฒคํŠธ ์†Œ์Šค์™€ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ํ•จ์ˆ˜

 

์ด๋ฒคํŠธ์†Œ์Šค -> (๋ฆฌ์Šค๋„ˆ ๋“ฑ๋กํ•จ์ˆ˜) -> ๋ฆฌ์Šค๋„ˆ(์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ)

binding.button.setOnClickListener{
    Log.d("kkang","ํด๋ฆญ์ด๋ฒคํŠธ")
}

 

 

์ด๋ฒคํŠธ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• 1

 

์ด๋ฒคํŠธ์†Œ์Šค.๋ฆฌ์Šค๋„ˆ๋“ฑ๋กํ•จ์ˆ˜(๋ฆฌ์Šค๋„ˆ: ๋“ฑ๋กํ•จ์ˆ˜){} ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š”๋ฐ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•œ๋‹ค.

์ธํ„ฐํŽ˜์ด์Šค๋Š” ๊ฐ์ฒด๋ฅผ ๋ชป๋งŒ๋“ค๋ฉฐ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๊ณ  ๋‚ด๋ถ€์— ์ถ”์ƒ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ๋‹ค. ํ•œ๋ฒˆ๋งŒ ์“ฐ๊ณ  ๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”๋กœ ์ „๋‹ฌ๋˜๋ฉฐ ์ต๋ช…์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. (Anonymous class)

๋’ค์— ๋Œ€๋ฌธ์ž๋กœ ์ธํ„ฐํŽ˜์ด์Šค? ์—ฐ๊ฒฐํ•˜๋ฉด ๋นจ๊ฐ„ ์ „๊ตฌ๊ฐ€ ๋œฌ๋‹ค.

 

์ž๋™์œผ๋กœ ๋ฉค๋ฒ„๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค. TODO ๋งŒ ์ˆ˜์ •ํ•˜๋ฉด๋œ๋‹ค.

binding.checkbox.setOnClickListener(object : CompoundButton.OnCheckedChangeListener {
    override fun onCheckedChanged(p0: CompoundButton?, p1: Boolean) {
        TODO("Not yet implemented")
    }
})

 

 

์ด๋ฒคํŠธ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• 2

class MainActivity : AppCompatActivity() , CompoundButton.OnCheckedChangeListener {

    lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root) // ๋ทฐ ๊ฐ์ฒด ์ƒ์„ฑ

        binding.checkbox.setOnClickListener(this)
    }

    override fun onCheckedChanged(p0: CompoundButton?, p1: Boolean) {
        TODO("Not yet implemented")
    }

 

 

์ด๋ฒคํŠธ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• 3

class MyEventHandler : CompoundButton.OnCheckedChangeListener{
    override fun onCheckedChanged(p0: CompoundButton?, p1: Boolean) {
        TODO("Not yet implemented")
    }
}

class MainActivity : AppCompatActivity(){

    lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root) // ๋ทฐ ๊ฐ์ฒด ์ƒ์„ฑ

        binding.checkbox.setOnCheckedChangeListener(MyEventHandler())
    }
    }

 

 

 

 

์ด๋ฒคํŠธ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• 4

binding.checkbox.setOnCheckedChangeListener{
    compoundButton, b ->
    Log.d("kkang","์ฒดํฌ๋ฐ•์Šคํด๋ฆญ")
}

 

 

 

ํด๋ฆญ๊ณผ ๋กฑํด๋ฆญ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ

ClickEvent, LongClickEvent ๋Š” ๋ทฐ์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค์ธ View ์— ์ •์˜๋œ ์ด๋ฒคํŠธ์ด๋‹ค.

- open fun setOnClickListener(l: View.OnClickListener?): Unit

- open fun setOnLongClickListener(l: View.OnLongClickListener?): Unit

 

๋Œ€๋ฌธ์ž ์ธํ„ฐํŽ˜์ด์Šค, ๋‚ด๋ถ€์— ์ถ”์ƒ๋ฉ”์†Œ๋“œ

binding.button.setOnClickListener{
    Log.d("kkang","ํด๋ฆญ์ด๋ฒคํŠธ")
}

binding.button.setOnLongClickListener{
    Log.d("kkang","ํด๋ฆญ์ด๋ฒคํŠธ")
    true //๋žŒ๋‹ค ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ํ˜•์ด Boolean
}

 

 

 

SAM(Single Abstraction Method) ๊ธฐ๋ฒ•

์ž๋ฐ” API๋ฅผ ์ฝ”ํ‹€๋ฆฐ์—์„œ ํ™œ์šฉํ•  ๋•Œ ๋žŒ๋‹ค ํ‘œํ˜„์‹์œผ๋กœ ์‰ฝ๊ฒŒ ์ด์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๊ธฐ๋ฒ•์œผ๋กœ ํ•˜๋‚˜์˜ ์ถ”์ƒํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์ž๋ฐ”์— ์ž‘์„ฑ๋˜์–ด ์žˆ๊ณ  ๊ทธ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋“ฑ๋กํ•˜๋Š” ์„ธํ„ฐsetter ํ•จ์ˆ˜๋„ ์ž๋ฐ”์— ์ž‘์„ฑ๋˜์–ด ์žˆ์œผ๋ฉด ์ฝ”ํ‹€๋ฆฐ์—์„œ ์„ธํ„ฐ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ๊ฐ์ฒด๋ฅผ ๋“ฑ๋กํ•  ๋•Œ ๋žŒ๋‹คํ•จ์ˆ˜๋กœ ์‰ฝ๊ฒŒ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฐ•์˜ ์ž๋ฃŒ์ž…๋‹ˆ๋‹ค.

์ž๋ฐ”๋ฅผ ํ™œ์šฉํ•œ ๊ตฌํ˜„์€ ๋‚˜์ค‘์— ํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.