이 글은 유데미 강의 Android 12 및 Kotlin 개발 완전 정복을 참고하여 작성하였습니다.
작성자 : 조성현 (본문3)
개발환경은 Mac, Android Studio입니다. (본문3)
강의 영상 104 ~ 105 의 리뷰 입니다.
- 질문 모델 만들기!
요구 사항 분석:
요구 사항 리스트 :
제한 사항 :
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val buttonStart:Button = findViewById(R.id.btn_start) //like spring boot
val etName:AppCompatEditText = findViewById(R.id.et_name) /// kotlin 은 snake case 를 싫어함
buttonStart.setOnClickListener {
if (etName.text.toString().isEmpty()){
Toast.makeText(this,"Please Enter Your Name",Toast.LENGTH_SHORT).show()
}else{
val intent = Intent(this@MainActivity, QuizQuestionsActivity::class.java)
// TODO Pass the name through intent using the constant variable which we have created.
intent.putExtra(Constants.USER_NAME, etName.text.toString()) //ntent란 간단히 말하자면 여러 화면(창) 간의 이동 엑티비비 전환
startActivity(intent)
finish() // app 을 종료
}
}
}
xml et_name : 버튼에 들어가는 이름 텍스트
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionGo"
android:hint="Name"
android:inputType="textCapWords"
android:textColor="#363A43"
android:textColorHint="#7A8089" />
싱글톤 객체 constant , 코틀린은 static 이 없다,
package eu.tutorials.quizapp
object Constants {
// TODO Create a constant variables which we required in the result screen
const val USER_NAME: String = "user_name"
const val TOTAL_QUESTIONS: String = "total_questions"
const val CORRECT_ANSWERS: String = "correct_answers"
fun getQuestions(): ArrayList<Question> { // 질문을 가져오는 질문 함수
val questionsList = ArrayList<Question>() // 만든 객체를 담아주는 배열
// 1
val que1 = Question(
1, "What country does this flag belong to?",
R.drawable.ic_flag_of_argentina, /// resource folder의 사진을 가져옴
"Argentina", "Australia",
"Armenia", "Austria", 1
)
questionsList.add(que1)
return questionsList
}
}
Question class
package eu.tutorials.quizapp
data class Question(
val id:Int,
val question:String,
val image:Int,
val optionOne:String,
val optionTwo:String,
val optionThree:String,
val optionFour:String,
val correctAnswer:Int
)
class QuizQuestionsActivity : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_quiz_questions)
val ql = Constants.getQuestions()
Log.i("msg","${ql.size}");
}
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:indeterminate="false"
android:max="10"
android:minHeight="50dp"
android:progress="0" />
'GDSC HUFS 4기 > Kotlin Team #7' 카테고리의 다른 글
[7팀] 퀴즈 앱 안드로이드 12 part 4 (0) | 2022.11.07 |
---|---|
[7팀] 퀴즈 앱 안드로이드 12 part 3 (0) | 2022.11.07 |
[7팀] 퀴즈 앱 안드로이드 12 part 1 (0) | 2022.11.07 |
[7팀] 계산기 -xml 사용법과 UI 생성 배우기 part 2 (0) | 2022.10.31 |
[7팀] 계산기 - XML 사용법과 UI 생성법 배우기 - 안드로이드12 (0) | 2022.10.31 |