티스토리 뷰
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="혀가 길지 않은 개발자"
android:textSize="30dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<Button
android:id="@+id/btnChange"
android:layout_width="180dp"
android:layout_height="80dp"
android:text="Change"
android:textSize="16dp"
android:layout_marginTop="30dp"
app:layout_constraintTop_toBottomOf="@+id/tvText"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.jwsoft.kotlinproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnChange.setOnClickListener {
Thread {
tvText.text = "혀가 긴 개발자"
}.start()
}
}
}
UI 작업은 무조건 메인쓰레드에서 해야됨.
Handler.post 사용해서 해결하자.
MainActivity.kt
package com.jwsoft.kotlinproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var handler = Handler()
btnChange.setOnClickListener {
handler.post {
tvText.text = "혀가 긴 개발자"
}
}
}
}
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] runOnUiThread (0) | 2020.06.28 |
---|---|
[Kotlin] Retrofit2 (0) | 2020.06.27 |
[Kotlin] Handler (0) | 2020.06.27 |
[Kotlin] RecyclerView (0) | 2020.06.26 |
[Kotlin] 4. Button 활용 (0) | 2020.06.11 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- fragment
- activity
- Android
- James Kim
- 자바
- MVVM
- ViewModel
- 안드로이드 #코틀린 #Android #Kotlin
- CoordinatorLayout
- 코틀린
- JSONObject
- Architecture Pattern
- XML
- Livedata
- 안드로이드
- Vue.js #Vue.js + javascript
- ViewPager2
- recyclerview
- handler
- Kotlin
- TabLayout
- 혀가 길지 않은 개발자
- coroutine
- View
- java
- DataBinding
- Design Pattern
- Intent
- ArrayList
- JSONArray
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함