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 | 29 | 30 | 31 |
Tags
- SQL
- degit
- 답글
- 코틀린
- 스타일보험
- 버전일치
- kotlin
- 오버라이드
- Android
- 스프링
- 쿠키
- FIle
- java
- 함수
- Spring
- git
- sub query
- snowpack
- parcel
- 게시판
- 상속
- webpack
- 안드로이드
- 왕초보
- 미니게임
- java#왕초보
- Spinner
- React
- 숫자
- 시큐어코딩
Archives
- Today
- Total
YSHUSH
라디오 버튼 본문
라디오 버튼 클릭시 선택된 항목을
텍스트박스에 출력해보자
1. activity_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="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<RadioButton
android:text="사과"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio1"/>
<RadioButton
android:text="바나나"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio2"/>
<RadioButton
android:text="오렌지"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio3"/>
</RadioGroup>
<TextView
android:id="@+id/textView"
android:text="TextView"
android:layout_width="203dp"
android:layout_height="48dp"
app:layout_constraintTop_toBottomOf="@+id/radioGroup"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="28dp"
app:layout_constraintHorizontal_bias="0.501"/>
</androidx.constraintlayout.widget.ConstraintLayout>
2. build.gradle - android{···}내에 kotlinOptions 밑에 추가
buildFeatures{
viewBinding true
}
3. ManinActivity.kt
첫번째 방법
class MainActivity : AppCompatActivity() {
val binding by lazy {ActivityMainBinding.inflate(layoutInflater)}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
//setContentView(R.layout.activity_main)
binding.radioGroup.setOnCheckedChangeListener{ _, checkedId ->
Log.d("RadioButton", "radio 버튼 클릭")
when(checkedId){
R.id.radio1 -> binding.textView.text = "사과가 선택되었습니다."
R.id.radio2 -> binding.textView.text = "바나나가 선택되었습니다."
R.id.radio3 -> binding.textView.text = "오렌지가 선택되었습니다."
}
}
}
}
두번째 방법
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
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 = "오렌지가 선택되었습니다."
}
}
}
}
'Coding > Android(kotlin)' 카테고리의 다른 글
Context menu (0) | 2022.02.08 |
---|---|
체크박스 (0) | 2022.02.08 |
버튼(Button) 클릭시 alert dialog띄우기 (0) | 2022.02.08 |
버튼(Button) 클릭시 시간출력 이벤트 만들기 (0) | 2022.02.08 |
액션 바(Action Bar) (0) | 2022.02.07 |