작성
·
216
답변 2
0
package com.gundol.dice_app
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import com.gundol.dice_app.databinding.ActivityMainBinding
import kotlin.random.Random
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
//binding 기능으로 레이아웃찾기
val diceImage1 = binding.dice1
val diceImage2 = binding.dice2
binding.diceStartBtn.setOnClickListener {
Toast.makeText(this, "주사위 굴러가유~", Toast.LENGTH_LONG).show()
Log.d("MainActivity", Random.nextInt(1, 7).toString())
Log.d("MainActivity", Random.nextInt(1, 7).toString())
val number1 = Random.nextInt(1, 7)
val number2 = Random.nextInt(1, 7)
if (number1 == 1) {
diceImage1.setImageResource(R.drawable.dice_1)
//setImageResource를 이용해서 이미지를 변경할수 있다.
} else if (number1 == 2) {
diceImage1.setImageResource(R.drawable.dice_2)
} else if (number1 == 3) {
diceImage1.setImageResource(R.drawable.dice_3)
} else if (number1 == 4) {
diceImage1.setImageResource(R.drawable.dice_4)
} else if (number1 == 5) {
diceImage1.setImageResource(R.drawable.dice_5)
} else {
diceImage1.setImageResource(R.drawable.dice_6)
}
if (number2 == 1) {
diceImage2.setImageResource(R.drawable.dice_1)
} else if (number2 == 2) {
diceImage2.setImageResource(R.drawable.dice_2)
} else if (number2 == 3) {
diceImage2.setImageResource(R.drawable.dice_3)
} else if (number2 == 4) {
diceImage2.setImageResource(R.drawable.dice_4)
} else if (number2 == 5) {
diceImage2.setImageResource(R.drawable.dice_5)
} else {
diceImage2.setImageResource(R.drawable.dice_6)
}
}
}
}
기존의 코드가 이렇게 되어 있는데
Log.d("MainActivity", Random.nextInt(1, 7).toString())
Log.d("MainActivity", Random.nextInt(1, 7).toString())
val number1 = Random.nextInt(1, 7)
val number2 = Random.nextInt(1, 7)
val number1 = Random.nextInt(1, 7)
val number2 = Random.nextInt(1, 7)
Log.d("MainActivity", number1.toString())
Log.d("MainActivity", number2.toString())
이렇게 변경해보세요~
0
여기있습니다~!