제목 | 안귀정 저자 안드로이드 코틀린 가이드 질문입니다. | 작성일 | 20-07-09 11:08 |
글쓴이 | 피곤한가제트 | 조회수 | 23,131 |
본문
현재 로또앱을 만들고 있습니다. 현재 디자인은 다 끝내고 로또번호 추출작업을 하고 있습니다.
해시코드 작업까지는 무난히 따라왔는데 날짜,요일,시간별로 번호가 다르게 나오는 작업에서 막히고 있습니다
ResultActivity에서 resultLabel 부분이 제대로 동작을 안하더라구요 빨간색 라인도 안뜨고 제대로 쓴거 같은데 런 실행만 하면
터미널에 Unresolved reference: resultLabel이 나옵니다
NameActivity는 제대로 작성한거 같은데
뭐가 문제인지 정말 골치아프네요
MainACtivity
package com.masterobi.lotto
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_name.*
import kotlin.random.Random
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
fun getLottoNumbersFromHash(name: String): MutableList<Int> {
val list = mutableListOf<Int>()
for (number in 1..45) {
list.add(number)
}
list.shuffle(
Random(name.hashCode().toLong()))
return list.subList(0, 6)
}
goButton.setOnClickListener{
val intent = Intent(this, ResultActivity::class.java)
intent.putIntegerArrayListExtra("result", ArrayList(getLottoNumbersFromHash(editText.text.toString())))
startActivity(intent)
}
randomCard.setOnClickListener {
val intent = (Intent(this, ResultActivity::class.java))
intent.putIntegerArrayListExtra("result", ArrayList(getLottoNumbersFromHash(editText.text.toString() )))
startActivity(intent)
}
constellationCard.setOnClickListener {
val intent = (Intent(this, ConstellationActivity::class.java))
intent.putIntegerArrayListExtra("result", ArrayList(getLottoNumbersFromHash(editText.text.toString() )))
startActivity(intent)
}
namecard.setOnClickListener {
val intent = (Intent(this, NameActivity::class.java))
intent.putIntegerArrayListExtra("result", ArrayList(getLottoNumbersFromHash(editText.text.toString() )))
startActivity(intent)
}
}
}
NameActivity
package com.masterobi.lotto
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_name.*
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
import kotlin.random.Random
class NameActivity : AppCompatActivity() {
override fun onCreate(saveInstanceState: Bundle?) {
super.onCreate(saveInstanceState)
setContentView(R.layout.activity_name)
fun getLottoNumbersFromHash(name: String): MutableList<Int> {
val list= mutableListOf<Int>()
for (number in 1..45){
list.add(number)
}
val targetString = SimpleDateFormat("yyyy-MM-dd", Locale.KOREA).format(Date()) + name
list.shuffle(Random(targetString.hashCode( ).toLong( )))
return list.subList(0, 6)
}
goButton.setOnClickListener {
if (TextUtils.isEmpty(editText.text.toString())) return@setOnClickListener
val intent = Intent(this, ResultActivity::class.java)
intent.putIntegerArrayListExtra("result", ArrayList(getLottoNumbersFromHash(editText.text.toString())))
intent.putExtra("name", editText.text.toString())
startActivity(intent)
}
backButton.setOnClickListener{
finish()
}
}
}
resultActivity
package com.masterobi.lotto
import android.annotation.SuppressLint
import android.os.Bundle
import android.text.TextUtils
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_result.*
class ResultActivity : AppCompatActivity() {
private val lottoImageStartId = R.drawable.lotto1
override fun onCreate(saveInstanceState: Bundle?) {
super.onCreate(saveInstanceState)
setContentView(R.layout.activity_result)
val result = intent.getIntegerArrayListExtra("result")
val name = intent.getStringExtra("name")
resultLabel.text = "랜덤으로 생성된₩n 로또번호입니다"
if (!TextUtils.isEmpty(name)) {
resultLabel.text =
"${name} 님의₩n${SimpleDateFormat(" yyyy년 MM월 dd일 ").format(Date())}₩n로또 번호입니다"
}
result?.let {
updateLottoballImage(result.sortedBy { it })
}
}
private fun updateLottoballImage(result: List<Int>) {
if (result.size < 6) return
imageView01.setImageResource(lottoImageStartId + (result[0] - 1))
imageView02.setImageResource(lottoImageStartId + (result[1] - 1))
imageView03.setImageResource(lottoImageStartId + (result[2] - 1))
imageView04.setImageResource(lottoImageStartId + (result[3] - 1))
imageView05.setImageResource(lottoImageStartId + (result[4] - 1))
imageView06.setImageResource(lottoImageStartId + (result[5] - 1))
}
}
activity result.xml
<view
android:id="@+id/resultLabel"
class="androidx.appcompat.widget.AppCompatTextView"
id="@+id/view2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
android:gravity="center"
android:maxLines="3"
android:text="홍길동님의₩n로또번호입니다"
android:textSize="60dp"
app:autoSizeMaxTextSize="60dp"
app:autoSizeMinTextSize="16dp"
app:autoSizeStepGranularity="1sp"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toBottomOf="@+id/guideline1"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintStart_toEndOf="@+id/guideline3"
app:layout_constraintTop_toTopOf="parent" />
이전글 | Re: 전산세무1급 2020-07-14 | ||
다음글 | Re: 안귀정 저자 안드로이드 코틀린 가이드 질문입니다. 2020-07-13 |