작성
·
476
답변 5
0
0
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)
val diceImage1 = binding.dice1
val diceImage2 = binding.dice2
binding.diceStartBtn.setOnClickListener {
Toast.makeText(this, "주사위 GO!", Toast.LENGTH_LONG).show()
val number1 = Random.nextInt(1, 6)
val number2 = Random.nextInt(1, 6)
Log.d("MainActivity", number1.toString())
Log.d("MainActivity", number2.toString())
if (number1 == 1) {
diceImage1.setImageResource(R.drawable.dice_1)
} 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)
}
}
}
}
0
올려주신 코드랑 비교 했는데요. 어디가 다른지 못 찼겠습니다. ㅜㅜ
package com.hyo.dice_app
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.DataBindingUtil.setContentView
import com.hyo.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 = setContentView(this, R.layout.activity_main)
val diceImage1 = binding.dice1 //xml에서 이미지뷰의 ID를 바인딩으로 찾아라//
val diceImage2 = binding.dice2
binding.diceStartBtn.setOnClickListener {
Toast.makeText(this, "주사위Go", Toast.LENGTH_LONG).show()
Log.d("MainActivity", Random.nextInt(1,6).toString())
Log.d("MainActivity", Random.nextInt(1,6).toString())
val number1 = Random.nextInt(1,6)
val number2 = Random.nextInt(1,6)
if (number1 == 1) {
diceImage1.setImageResource(R.drawable.dice_1)
} 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)
}
}
}
}
0
<?xml version="1.0" encoding="utf-8"?>
<layout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#008000"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="100dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:gravity="center"
android:textColor="@color/black"
android:layout_margin="20dp"
android:textStyle="bold"
android:text="인생은 주사위 한방에 가는거 아니겠습니까?"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:weightSum="2">
<ImageView
android:id="@+id/dice1"
android:src="@drawable/dice_1"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_weight="1"/>
<ImageView
android:id="@+id/dice2"
android:src="@drawable/dice_1"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_weight="1"/>
</LinearLayout>
<Button
android:id="@+id/diceStartBtn"
android:text="인생고고"
android:layout_width="match_parent"
android:background="@color/black"
android:textColor="@color/white"
android:layout_height="50dp"
android:layout_margin="50dp"
tools:ignore="InvalidId" />
</LinearLayout>
</layout>
네 확인할께요. 근데 xml은 이상이 없는 거죠??
처음 문의할때 영상 에서도 로그와 이미지가 다른 것 같았는데요..