Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Hook
- 오버라이딩
- 안드로이드
- array
- fragment
- npm
- 코틀린
- Android
- 리액트
- TypeScript
- nodejs
- JS
- RecyclerView
- JavaScript
- 랜덤번호
- Javscript
- GoogleMaps
- 구글맵스
- SpringBoot
- stylesheet
- TextView
- React
- 자바스크립트
- Linux
- 랜덤넘버
- 스프링부트
- scrollview
- Java
- button
- Kotlin
Archives
- Today
- Total
타닥타닥 개발자의 일상
Kotlin 라디오 버튼으로 항목 선택하고 텍스트 뷰에 보이게 하는 안드로이드 페이지 만들기 radio, TextView 본문
코딩 기록/Kotlin
Kotlin 라디오 버튼으로 항목 선택하고 텍스트 뷰에 보이게 하는 안드로이드 페이지 만들기 radio, TextView
NomadHaven 2022. 2. 7. 00:55activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="253dp"
android:layout_height="207dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.55" app:layout_constraintVertical_bias="0.463">
<RadioButton
android:text="사과"
android:checked="true"
android:layout_width="match_parent"
android:layout_height="71dp"
android:id="@+id/radio1"/>
<RadioButton
android:text="바나나"
android:layout_width="match_parent"
android:layout_height="59dp"
android:id="@+id/radio2"/>
<RadioButton
android:text="오렌지"
android:layout_width="match_parent"
android:layout_height="74dp"
android:id="@+id/radio3"/>
</RadioGroup>
<TextView
android:text="TextView"
android:id="@+id/textView"
android:layout_width="248dp"
android:layout_height="73dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginBottom="232dp"
android:layout_marginTop="53dp"
app:layout_constraintTop_toBottomOf="@+id/radioGroup"
app:layout_constraintVertical_bias="0.043"
app:layout_constraintHorizontal_bias="0.542"/>
</androidx.constraintlayout.widget.ConstraintLayout>
build.gradle(:app)
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.sample11"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures{
viewBinding true
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
추가된 부분
buildFeatures{
viewBinding true
}
MainActivity.kt
package com.example.sample11
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.RadioGroup
import android.widget.TextView
import com.example.sample11.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
val binding by lazy { ActivityMainBinding.inflate(layoutInflater)}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
/* setContentView(binding.root)
build.gradle에서 추가된 부분인 viewBinding과 연결.
binding 통해 모든 루트에 접근 가능
binding.radioGroup.setOnCheckedChangeListener{group,checkID->
Log.d("RadioButton","radio 버튼 클릭")
when(checkID){
R.id.radio1 -> binding.textView.text ="사과가 선택되었습니다."
R.id.radio2 -> binding.textView.text ="바나나가 선택되었습니다."
R.id.radio3 -> binding.textView.text ="오렌지가 선택되었습니다."
//binding을 통해서 textView에 접근
}
}*/
setContentView(R.layout.activity_main)
//binding.root 쓸 때는 못쓴다.
val radioGroup = findViewById<RadioGroup>(R.id.radioGroup)
val textView = findViewById<TextView>(R.id.textView)
radioGroup.setOnCheckedChangeListener{_,checkedid->
Log.d("RadioButton","radio 버튼 클릭")
when(checkedid){
R.id.radio1 -> textView.text ="사과가 선택되었습니다."
R.id.radio2 -> textView.text ="바나나가 선택되었습니다."
R.id.radio3 -> textView.text ="오렌지가 선택되었습니다."
}
}
}
}
결과 화면
'코딩 기록 > Kotlin' 카테고리의 다른 글
kotlin 원하는 이미지파일 android 화면에서 배경으로 설정하는 방법 android:background (0) | 2022.02.07 |
---|---|
Kotlin 안드로이드 화면에 Spinner 보이게 하기, Spinner로 선택한 항목 textView랑 연결하기 (0) | 2022.02.07 |
코틀린으로Kotlin 증감하는 Counter만들기, setOnClickListener과 증감 연산자 이용 (0) | 2022.02.03 |
코틀린 Kotlin 안드로이드 기계 연결 후 앱 이름, 버튼, 토스트 toast, 알림창 설정하기 (0) | 2022.01.30 |
Kotlin에서 MutableList, File, 상속 이용해서 야구선수 등록 프로그램 만들기 (0) | 2022.01.30 |
Comments