320x100
android strings 파일에 하드코딩 되어 있는 text중
일부분을 데이터를 동적으로 처리 하고 싶을 때
사용하면 된다
코드는 크게 복잡한게 없어
별다른 주석 처리는 해놓지 않았다
코드 1 : MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var tvMsg: TextView
private lateinit var bPlus: Button
private var count = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tvMsg = findViewById(R.id.tv_msg)
bPlus = findViewById(R.id.b_plus)
bPlus.setOnClickListener { countUpdate() }
}
private fun countUpdate() {
tvMsg.text = getString(R.string.select_text, ++count)
}
}
코드 2 : 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/tv_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="none"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/b_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/btn_text"
app:layout_constraintEnd_toEndOf="@+id/tv_msg"
app:layout_constraintStart_toStartOf="@+id/tv_msg"
app:layout_constraintTop_toBottomOf="@+id/tv_msg" />
</androidx.constraintlayout.widget.ConstraintLayout>
코드 3 : strings.xml
<resources>
<string name="select_text">현재 숫자는 \n
\'%d\' 입니다</string>
<string name="btn_text">증가</string>
</resources>
320x100
'Android > Kotlin' 카테고리의 다른 글
[Android][Kotlin] 안드로이드 view Binding 사용하기 findViewById 대체 (0) | 2022.11.01 |
---|---|
[Android][Kotlin] 캘린더(Calendar)로 오늘 날짜(today), 마지막 일(last day) 가져오기 (0) | 2022.10.24 |
[Android][Kotlin] 단말기(device) 가로 세로 실제 해상도 가져오기 (0) | 2022.10.21 |
[Android][Kotlin] Thread 에러 처리 및 UI 변경 활용 예제 (0) | 2022.10.20 |
[Android][Kotlin] 알람매니저(AlarmManager)를 이용한 알림(Notification) 호출하기 (0) | 2022.10.14 |