티스토리 뷰
728x90
반응형
SMALL
안녕하세요, 안드로이드 앱 개발에 관심 있는 여러분! 이번에는 Kotlin에서 안드로이드 앱의 사용자 인터페이스(UI)를 설계하고 다루는 핵심인 XML 레이아웃과의 상호작용에 대해 알아보겠습니다. 사용자에게 보여지는 화면을 어떻게 디자인하고 제어하는지를 배우면서, 안드로이드 앱 개발의 또 다른 중요한 부분을 익히게 될 것입니다.
1. XML 레이아웃이란?
안드로이드에서 UI를 설계하는 데에는 XML(Extensible Markup Language)이 사용됩니다. XML 레이아웃은 사용자 인터페이스의 구조와 디자인을 설명하는데에 효과적입니다.
2. XML 레이아웃의 기본 구조
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- UI 구성 요소들을 정의합니다 -->
</LinearLayout>
3. UI 구성 요소 추가하기
3-1. 텍스트뷰(TextView)
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, Kotlin!"
android:textSize="20sp"/>
3-2. 버튼(Button)
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:onClick="onButtonClick"/>
4. Kotlin에서 XML 레이아웃 사용하기
4-1. 뷰 참조하기
val textView = findViewById<TextView>(R.id.textView)
val button = findViewById<Button>(R.id.button)
4-2. 이벤트 핸들링
fun onButtonClick(view: View) {
textView.text = "Button Clicked!"
}
5. 레이아웃과 코드의 연동
5-1. 데이터 바인딩(Data Binding)
val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.textView.text = "Hello, Kotlin!"
binding.button.setOnClickListener { onButtonClick(it) }
6. 통합 예제: 간단한 계산기 앱
<?xml version="1.0" encoding="utf-8"?>
<!-- activity_main.xml -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/number1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter number 1"
android:inputType="numberDecimal"/>
<EditText
android:id="@+id/number2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter number 2"
android:inputType="numberDecimal"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:onClick="onAddClick"/>
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result: "/>
</LinearLayout>
// MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
}
fun onAddClick(view: View) {
val num1 = binding.number1.text.toString().toDoubleOrNull() ?: 0.0
val num2 = binding.number2.text.toString().toDoubleOrNull() ?: 0.0
val result = num1 + num2
binding.result.text = "Result: $result"
}
}
마무리
이제 여러분은 Kotlin으로 안드로이드 앱의 UI를 디자인하고 코드와 연동하는 방법에 대해 알게 되었습니다. XML 레이아웃을 통해 사용자에게 어떻게 정보를 보여줄지를 정의하고, Kotlin 코드에서 해당 UI를 제어하는 방법을 익힐 수 있습니다. 다음 시간에는 안드로이드에서 데이터를 저장하고 관리하는 방법을 알아보도록 하겠습니다. 계속해서 흥미로운 Kotlin 코딩되세요! 🚀
728x90
반응형
LIST
'안드로이드 첫번째 걸음' 카테고리의 다른 글
Kotlin 기초 : 안드로이드 앱 개발을 위한 시작 (8. 사용자 입력과 이벤트 처리) (0) | 2023.12.26 |
---|---|
Kotlin 기초 : 안드로이드 앱 개발을 위한 시작 (7. 데이터 저장 / 관리) (0) | 2023.12.25 |
Kotlin 기초 : 안드로이드 앱 개발을 위한 시작 (5. 널 안정성) (0) | 2023.12.22 |
Kotlin 기초 : 안드로이드 앱 개발을 위한 시작 (4. 클래스와 객체) (2) | 2023.12.21 |
Kotlin 기초 : 안드로이드 앱 개발을 위한 시작 (3. 함수) (2) | 2023.12.21 |
반응형
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Fragment
- MVVM패턴
- 비동기
- kotlin
- 안드로이드기초
- 부동산 경매
- 안드로이드앱개발
- 앱개발
- 안드로이드
- 코틀린
- 부동산
- 모듈화
- 안드로이드개발
- 생명주기
- RxJava
- 디자인패턴
- fragment생명주기
- UI스레드
- 안드로이드 앱 개발
- RoomDatabase
- 사이버보안
- Android
- 클린아키텍처
- 경매
- 내부데이터베이스
- 기초
- cipher
- 비동기처리
- coroutine
- 코루틴
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함