[Android TIL] 240328 (๊ฐœ์ธ๊ณผ์ œ ์‹ฌํ™” ๊ตฌํ˜„ ์™„๋ฃŒ, ์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ฐฝ์—… ๊ฐ•์ขŒ)
๋ฐ˜์‘ํ˜•

 

 

 

 

 

๐ŸŒฑ Today I Learned (์ง‘์ค‘์‹œ๊ฐ„ 6์‹œ๊ฐ„ 30๋ถ„)

  1. (09:10 ~ 11:50 / 1์‹œ๊ฐ„ 40๋ถ„) - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2๋ฌธ์ œ ํ’€์ด
  2. (12:10 ~ 12:40 / 30๋ถ„) ๊ฐœ์ธ ๊ณผ์ œ ์ œ์ถœ ๋ฐ ์ด๋ฒˆ ๋ฐœ์ œ ํ•™์Šต๋‚ด์šฉ ๋ณต์Šต
  3. (16:10 ~ 17:25 / 1์‹œ๊ฐ„ 15๋ถ„) ์ด๋ฒˆ ๋ฐœ์ œ ์‹ฌํ™” ๋‹จ๊ณ„๊นŒ์ง€ ๋ชจ๋‘ ๊ตฌํ˜„ (registerForActivityResult, Random)
  4. (19:30 ~ 20:30 / 1์‹œ๊ฐ„) ์ด๋ฒˆ ๋ฐœ์ œ ๊ฐœ์ธ์ ์ธ ๊ตฌํ˜„ (Visible)
  5. (20:30 ~ 21:00 / 30๋ถ„) ํŒ€ ๋ฐ์ผ๋ฆฌ ํšŒ๊ณ 
  6. (17:25 ~ 17:50, 21:00 ~ 21:30 / 55๋ถ„) ๊นƒํ—ˆ๋ธŒ ๋ฆฌ๋“œ๋ฏธ ๋ฐ TIL ์ž‘์„ฑ ๋ฐ ์ •๋ฆฌ
  7. (21:40 ~ 22:10 / 30๋ถ„) ๋ถ€์ŠคํŠธ์ฝ”์Šค ๊ฐ•์ขŒ ๋“ฑ (๊ฐœ์ธ/๋ฒ•์ธ)

*** ์ ์‹ฌ 13:00 ~ 14:00 / ์ €๋… 18:00 ~ 19:00 ์ œ์™ธ

โ€‹

 

 

 

1. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2๋ฌธ์ œ ํ’€์ด

 

 

[Kotlin] ๋ฌธ์ž์—ด ๋‹ค๋ฃจ๊ธฐ ๊ธฐ๋ณธ (ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜) : isDigit(), isLetter(), all(), toIntOrNull()

๋ฌธ์ œ https://school.programmers.co.kr/learn/courses/30/lessons/12918 ์ ‘๊ทผ 1) ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ํ™•์ธํ•˜์—ฌ 4 ๋˜๋Š” 6์ธ์ง€ ํ™•์ธํ•œ๋‹ค. 2) ๋ฌธ์ž์—ด์˜ ๋ชจ๋“  ๋ฌธ์ž๊ฐ€ ์ˆซ์ž๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. class Solution { fun solu

limheejin.tistory.com

 

[Kotlin] ํ–‰๋ ฌ์˜ ๋ง์…ˆ (ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜)

๋ฌธ์ œ https://school.programmers.co.kr/learn/courses/30/lessons/12950 ์ ‘๊ทผ class Solution { fun solution(arr1: Array, arr2: Array): Array { return (arr1.size).map{it -> (arr1[i].toInt() + arr2[i].toInt())} } } ์œ„์™€ ๊ฐ™์ด arr1.size ๋งŒํผ map์„ ์ด

limheejin.tistory.com

  • ์ฒ˜์Œ์œผ๋กœ 2์ฐจ์› ๋ฐฐ์—ด ์ด์ƒ์— ๋Œ€ํ•œ ์ƒ๊ฐ์„ ํ•ด๋ณด์•˜๋‹ค. 
  • toIntOrNull์„ ์ด์šฉํ•ด ๋ฌธ์ž์—ด์ด ์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”์ง€ ์ฐพ๋Š” ๋ฐฉ์‹์€ ์‹ ๋ฐ•ํ–ˆ๋‹ค.

 

 

๐Ÿ’กisDigit()

val char1 = '5'
val char2 = 'a'
println(char1.isDigit()) // true
println(char2.isDigit()) // false
  • ๋ฌธ์ž๊ฐ€ ์ˆซ์ž์ธ์ง€๋ฅผ ํŒ๋ณ„
  • ๋ฌธ์ž์—ด์˜ ๊ฒฝ์šฐ ์ˆซ์ž๋ฅผ ํ•˜๋‚˜๋ผ๋„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉด true์ž„์— ์ฃผ์˜
  • ์‚ฌ์šฉ๋ฒ•: Char.isdigit()

 

 

๐Ÿ’กisLetter()

val char1 = 'A'
val char2 = '5'
println(char1.isLetter()) // true
println(char2.isLetter()) // false
  •  ๋ฌธ์ž๊ฐ€ ์•ŒํŒŒ๋ฒณ ๋ฌธ์ž์ธ์ง€๋ฅผ ํŒ๋ณ„ 
  • ๋ฌธ์ž์—ด์˜ ๊ฒฝ์šฐ ์ˆซ์ž๋ฅผ ํ•˜๋‚˜๋ผ๋„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉด true์ž„์— ์ฃผ์˜
  • ์‚ฌ์šฉ๋ฒ•: Char.isLetter()

 

 

๐Ÿ’กall()

val numbers = listOf(1, 2, 3, 4, 5)
val allEven = numbers.all { it % 2 == 0 }
println(allEven) // false

val letters = listOf('a', 'b', 'c', 'd', 'e')
val allLowercase = letters.all { it.isLowerCase() }
println(allLowercase) // true
  • ์ปฌ๋ ‰์…˜์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š”์ง€๋ฅผ ํ™•์ธ
  • ์‚ฌ์šฉ๋ฒ•: Collection.all { predicate(๊ฐ ์š”์†Œ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ ์กฐ๊ฑด์„ ๊ฒ€์‚ฌํ•˜๋Š” ํ•จ์ˆ˜, true or false) }

 

 

๐Ÿ’ก.toIntOrNull()

"123".toIntOrNull() -> ์ •์ˆ˜ 123์„ ๋ฐ˜ํ™˜
"abc".toIntOrNull() -> null์„ ๋ฐ˜ํ™˜
"ab3c".toIntOrNull() -> null์„ ๋ฐ˜ํ™˜
  • ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธ
  • ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ๊ทธ ์ •์ˆ˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๋ณ€ํ™˜ํ•  ์ˆ˜ ์—†๋‹ค๋ฉด null์„ ๋ฐ˜ํ™˜

 

 

 

 

2. ์ด๋ฒˆ ๋ฐœ์ œ ์‹ฌํ™” ๋‹จ๊ณ„๊นŒ์ง€ ๋ชจ๋‘ ๊ตฌํ˜„ ๋ฐ ๊ฐœ์ธ์ ์ธ ๊ตฌํ˜„

 

  • ์˜ค๋Š˜์€ ํ•ด๋‹น ๊ณผ์ œ๋ฅผ ์‹ฌํ™”์‹œํ‚ค๊ณ  ๊ณต๋ถ€ํ•˜๋Š” ๋ฐ 2์‹œ๊ฐ„ 15๋ถ„์„ ์†Œ์š”ํ–ˆ๋‹ค.
  • 1) ํšŒ์›๊ฐ€์ž…์— ์ž…๋ ฅ๋œ ์•„์ด๋”” ๋„˜๊ธฐ๊ธฐ(registerForActivityResult ์ด์šฉ)
  • 2) ๋žœ๋ค ์ด๋ฏธ์ง€ ์ถœ๋ ฅ(Random ์ด์šฉ)
  • 3) ๋น„๋ฐ€๋ฒˆํ˜ธ ์ฒดํฌ๋ฐ•์Šค Visible ์„ค์ • (transformationMethod์ด์šฉ)

 

 

๐Ÿ”จ ํšŒ์›๊ฐ€์ž…์— ์ž…๋ ฅ๋œ ์•„์ด๋”” ๋„˜๊ธฐ๊ธฐ (registerForActivityResult)

class SignInActivity : AppCompatActivity() {
...
    private lateinit var resultLauncher: ActivityResultLauncher<Intent>
    // ActivityResultLauncher์ž๋ฃŒํ˜•์ธ resultLauncher ๋ณ€์ˆ˜๋ฅผ ์ „์—ญ ๋ณ€์ˆ˜๋กœ ์„ ์–ธ

    override fun onCreate(savedInstanceState: Bundle?) {
...
        // setResultNext ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ resultLauncher ์ดˆ๊ธฐํ™”
        setResultNext()
...
        signupButton.setOnClickListener {
            val intent = Intent(this, SignupActivity::class.java)
            resultLauncher.launch(intent)
        }
    }
...
    private fun setResultNext(){
        resultLauncher = registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()){result ->
            if(result.resultCode == RESULT_OK){
                val id = result.data?.getStringExtra("id") ?: ""
                val loginId = findViewById<EditText>(R.id.login_id)
                loginId.setText(id)     
                // ์˜ค๋ฅ˜ ํ•ด๊ฒฐ: Edittext์˜ ๊ฒฝ์šฐ .text = ""๊ฐ€ ์•„๋‹ˆ๋ผ setText("") ์ด์šฉ
            }
        }
    }
  • ์ด์ „์—๋Š” putExtra() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์ธํ…ํŠธ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ฒจ๋ถ€ํ•˜๊ณ , startActivityForResult() ๋ฐ onActivityResult() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ–ˆ๋‹ค๊ณ  ํ•˜๋Š”๋ฐ ์ด startActivityForResult() ์™€ onActivityResult()๊ฐ€ deprecated ๋๋‹ค๊ณ  ํ•œ๋‹ค.
  • ๋”ฐ๋ผ์„œ ์ง€๊ธˆ์€ registerForActivityResult()๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ถŒ์žฅ๋˜๊ณ  ์žˆ๋‹ค.
  • registerForActivityResult()
    - ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›๊ธฐ ์œ„ํ•œ ๋ฆฌ์Šค๋„ˆ(ActivityResultLauncher)๋ฅผ ๋ฏธ๋ฆฌ ์„ค์ •ํ•˜๊ณ , ํ•ด๋‹น ๋ฆฌ์Šค๋„ˆ์—์„œ ๊ฒฐ๊ณผ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝœ๋ฐฑ์„ ์ •์˜
    - ๋” ๊ฐ„๊ฒฐํ•˜๊ณ  ์ง๊ด€์ ์ด๋ฉฐ, onActivityResult() ๋ฉ”์„œ๋“œ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์šด ์ž‘์—…๋“ค์„ ๊ฐ„์†Œํ™”ํ•จ
    - ๋žŒ๋‹ค์‹์„ ์ด์šฉํ•˜์—ฌ ์ฝœ๋ฐฑ์„ ์‚ฌ์šฉ, ๊ฐ€๋…์„ฑ๊ณผ ์œ ์ง€๋ณด์ˆ˜์„ฑ ํ–ฅ์ƒ
  • ์ฐธ๊ณ ์‚ฌ์ดํŠธ (๋งํฌ)
  • ๋‹ค์Œ์— ์ž์„ธํ•œ ์„ค๋ช…๊ณผ ์‚ฌ์šฉ๋ฒ•์„ ๋”ฐ๋กœ ํฌ์ŠคํŒ… ํ•  ์ƒ๊ฐ์ด๋‹ค.

 

 

๐Ÿ”จ 2) ๋žœ๋ค ์ด๋ฏธ์ง€ ์ถœ๋ ฅ (Random)

val profilePic = findViewById<ImageView>(R.id.img_profile)
val img : IntArray = intArrayOf(R.drawable.pic1, R.drawable.pic2, R.drawable.pic3)
val random = Random()
val num = random.nextInt(img.size) // num ๋ณ€์ˆ˜์— 0๋ถ€ํ„ฐ (images์˜ ๊ธธ์ด - 1) ๋งŒํผ ์ˆซ์ž ์ค‘ ๋ฌด์ž‘์œ„ Int ์ €์žฅ
profilePic.setImageResource(img[num])
  • ์ด๋ฏธ์ง€๋ฅผ IntArray์— ๋„ฃ๊ณ  random์„ ์ด์šฉํ–ˆ๋‹ค.
  • ์ฐธ๊ณ ์‚ฌ์ดํŠธ (๋งํฌ)

 

 

๐Ÿ”จ 3) ๋น„๋ฐ€๋ฒˆํ˜ธ ์ฒดํฌ๋ฐ•์Šค Visible ์„ค์ • (transformationMethod)

val loginPw = findViewById<EditText>(R.id.login_pw)
val visibleCheckBox = findViewById<CheckBox>(R.id.btn_visible)

visibleCheckBox.setOnCheckedChangeListener{ buttonView, isChecked ->
	if (isChecked) {
		// ์ฒดํฌ๋œ ๊ฒฝ์šฐ, ์ผ๋ฐ˜ ํ…์ŠคํŠธ ์ž…๋ ฅ ํ˜•ํƒœ๋กœ ๋ณ€๊ฒฝ
		loginPw.transformationMethod = null
	} else {
		// ์ฒดํฌ๊ฐ€ ํ•ด์ œ ๋œ ๊ฒฝ์šฐ, ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ ํ˜•ํƒœ๋กœ ๋ณ€๊ฒฝ
		loginPw.transformationMethod = PasswordTransformationMethod.getInstance()
	}
}
  • ์›๋ž˜๋Š” xml ์ž์ฒด์—์„œ ์ˆจ๊ธฐ๊ธฐ/๋ณด์ด๊ธฐ ํ† ๊ธ€์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ธฐ๋Šฅ์ด ์žˆ๋‹ค.
  •   <com.google.android.material.textfield.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:passwordToggleEnabled="true"> ์•ˆ์— ํ…์ŠคํŠธ๋ทฐ ๋„ฃ์–ด์ฃผ๊ธฐ
    </com.google.android.material.textfield.TextInputLayout>
  • ํ•˜์ง€๋งŒ ์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์€ ์ปค์Šคํ…€๋„ ๋ถˆ๊ฐ€๋Šฅํ•˜๊ณ , ์—ฌ๋Ÿฌ๋ชจ๋กœ ์ œํ•œ์‚ฌํ•ญ์ด ๋งŽ์•„์„œ ์ง์ ‘ ์ฒดํฌ๋ฐ•์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ์•กํ‹ฐ๋น„ํ‹ฐ์— ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ˆ˜์ •ํ–ˆ๋‹ค. ์ฒดํฌ๋ฐ•์Šค id๋ฅผ ๊ฐ€์ ธ์™€์„œ setOnCheckedChangeListener๋ฅผ ๋งŒ๋“ค๊ณ , ๊ธฐ์กด Edittext์— ์„ ์–ธ๋˜์–ด ์žˆ๋˜ ํ˜•์‹์„ transformationMethod๋กœ ๋ฐ”๊พธ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

 

 

 

 

๐Ÿ’ญ Retrospect

๊ฐœ์ธ ๊ณผ์ œ๋ฅผ ์„ ํƒ ์‹ฌํ™” ๋‚ด์šฉ๊นŒ์ง€ ๊ตฌํ˜„ํ•˜๋ฉด์„œ ๋งค์šฐ ์žฌ๋ฏธ์žˆ์—ˆ๋‹ค. ๋‹ค์Œ ์ฃผ ์ฒซ ํ˜‘์—… ํ”„๋กœ์ ํŠธ๋„ ๊ธฐ๋Œ€๋œ๋‹ค!

โ€‹

 

 

 

 

๋ฐ˜์‘ํ˜•
 ๐Ÿ’ฌ C O M M E N T