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
- TextView
- Linux
- Hook
- Java
- 자바스크립트
- scrollview
- 랜덤번호
- React
- JS
- 구글맵스
- 스프링부트
- npm
- Kotlin
- 코틀린
- button
- SpringBoot
- stylesheet
- 안드로이드
- TypeScript
- GoogleMaps
- 오버라이딩
- nodejs
- 리액트
- Android
- 랜덤넘버
- Javscript
- array
- RecyclerView
- fragment
- JavaScript
Archives
- Today
- Total
타닥타닥 개발자의 일상
코틀린 Kotlin 안드로이드 기계 연결 후 앱 이름, 버튼, 토스트 toast, 알림창 설정하기 본문
인텔리제이에서 새 프로젝트 👉Android👉프로젝트 템프릿 선택 👉 Empty Activity 👉 다음버튼 👉 완료
주로 사용할 기능들
activity_main.xml / MainActivity.kt / buildgradle(:app) / AndroidManifest.xml / strings.xml
activity_main.xml
우측 상단의 Code/Design을 선택함에 따라 코드로 화면을 구성할지 직접 디자인하며 구성할지 선택 가능
activity_main의 Code 화면
<?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">
<TextView
android:id="@+id/textSay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" //화면에서 보이는 문자열
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:id="@+id/btnSay"
android:text="버튼(●'◡'●)" //버튼에 표시되는 문자열
android:layout_width="157dp"
android:layout_height="62dp"
app:layout_constraintTop_toBottomOf="@+id/textSay"
android:layout_marginTop="44dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="220dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main의 designe화면, Code화면에서 보았던 속성 TextView가 보인다.
메인화면 설정을 위한 build.gradle(:app)
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.sample1"
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에서 객체화하여 접근할수 있게 만들었다.
MainActivity.kt
package com.example.sample1
import android.app.Activity
import android.app.AlertDialog
import android.content.ContentValues
import android.content.DialogInterface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import com.example.sample1.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
/*
val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
build.gradle 이용해 하나의 객체로 묶어놓고 접근하게 만든것
<build.gradle에 있는 부분>
buildFeatures{
viewBinding true
}
*/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//build.gradle 이용해 객체로 접근하게 하는 방법
/* setContentView(binding.root) //val binding은 이 코드로 초기화 한다는 말
//버튼을 클릭했을때 괄호 안의 내용이 실시
>>binding.을 통해서 체인형식으로 연결한다.
binding.btnSay.setOnClickListener{
Log.i(ContentValues.TAG,"btn-say 클릭했습니다.") //하단의 Logcat에서 태블릿의 버튼을 터치하면 출력된다.
println("btnSay 클릭했습니다.") //하단 탭의 실행부분에서 태블릿의 버튼을 터치하면 출력된다.
binding.textSay.setText("Hello Android")
}*/
setContentView(R.layout.activity_main) //이 코드를 선언해야지 화면 설정이 가능하다.
val textSay = findViewById<TextView>(R.id.textSay) //R= resource, R통해서 모든 곳에 접근 가능하다
val btnSay = findViewById<Button>(R.id.btnSay)
btnSay.setOnClickListener {
textSay.text = "Android World!"
//버튼을 클릭하면 Hello World 란 텍스트가 Android World로 변화
// toast: 알림창이 아닌 조그만 까만 버튼 같은 창
val toast = Toast.makeText(this.applicationContext, "버튼클릭", Toast.LENGTH_SHORT)
toast.show()
//.show()를 해야 toast창이 보인다.
AlertDialog.Builder(this@MainActivity)
.setTitle("알림창")
.setMessage("welcome (´▽`ʃ♡ƪ)")
.setCancelable(false)
.setNeutralButton("닫기",DialogInterface.OnClickListener{ dialog, which->
//닫기 버튼을 클릭시!
}).show()
}
Log.i(ContentValues.TAG,"OnCreate 실행")
}
override fun onStart() {
super.onStart()
Log.i(ContentValues.TAG,"OnStart 실행")
}
override fun onResume() {
super.onResume()
Log.i(ContentValues.TAG,"OnResume 실행")
}
override fun onRestart() {
super.onRestart()
Log.i(ContentValues.TAG,"OnRestart 실행")
}
override fun onPause() {
super.onPause()
Log.i(ContentValues.TAG,"OnPause 실행")
}
override fun onStop() {
super.onStop()
Log.i(ContentValues.TAG,"OnStop 실행")
}
override fun onDestroy() {
super.onDestroy()
Log.i(ContentValues.TAG,"OnDestroy 실행")
}
}
앱이름 설정을 위한 strings.xml
<resources>
<string name="app_name">My App</string>
</resources>
위의 설정대로 구현한 실제 App sample
'코딩 기록 > Kotlin' 카테고리의 다른 글
Comments