<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center"
android:padding="15dp"
tools:context=".MainActivity">
<Button
android:id="@+id/clickMeBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click Me" />
<Button
android:id="@+id/makeLogBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="로그 남기기 버튼"/>
<Button
android:id="@+id/toastBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="사용자가 눌렀다고 인식 가능한 버튼"/>
<EditText
android:id="@+id/contentEdt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="토스트로 출력할 내용 적기"/>
<Button
android:id="@+id/contentToastBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="입력된 문구 토스트로 띄우기"/>
<EditText
android:id="@+id/messageEdt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="텍스트뷰에 반영할 문구 적기"
android:singleLine="true" />
<Button
android:id="@+id/changeTextBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="하단의 텍스트뷰 문구 변경하기" />
<TextView
android:id="@+id/resultTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="변경될 뷰"
android:textSize="18sp"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/nameEdt"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:hint="이름 입력"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/greetingBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="인사 문구 출력"/>
</LinearLayout>
</LinearLayout>
package com.gdh.kotlinbasic
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
clickMeBtn.setOnClickListener {
// 클릭미 버튼이 눌리면 실행되는 코드
Log.d("메인화면","클릭미 버튼 눌림")
}
makeLogBtn.setOnClickListener {
Log.d("메인화면","로그 남기기 버튼 눌림")
// xml에 추가한 버튼을 makeLogBtn으로 이름 지어서 => 눌리면 "로그 남기기 버튼 눌림" 로그 남기기기
}
toastBtn.setOnClickListener {
// 사용자에게 "버튼 눌림" 안내 문구 제공
Toast.makeText(this,"버튼 눌림",Toast.LENGTH_SHORT ).show()
}
contentToastBtn.setOnClickListener {
// 입력한 문구를 받아서 => 토스트로 출력
// contentEdt의 문구 (text 속성의 값) => String으로 받아서 => 코틀린 변수에 저장
// 변수에 저장된 문구를 => 토스트의 재료로 사용
val inputContent = contentEdt.text.toString()
Toast.makeText(this,inputContent,Toast.LENGTH_SHORT).show()
// IF문으로 작성
if (inputContent == "안녕하세요") {
Toast.makeText(this,"인사",Toast.LENGTH_SHORT).show()
}else if (inputContent == "안녕히 가세요"){
Toast.makeText(this,"작별 인사",Toast.LENGTH_SHORT).show()
}else if (inputContent == "맛있게 드세요"){
Toast.makeText(this,"식사 인사",Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(this,"그 외의 문장",Toast.LENGTH_SHORT).show()
}
}
changeTextBtn.setOnClickListener {
// messageEdt에 저장된 문구를 String으로 받아서 (get)
// resultTxt의 문구 (text속성의 값)로 반영 (set)
val inputMessage = messageEdt.text.toString()
resultTxt.text = inputMessage
}
greetingBtn.setOnClickListener {
// 입력된 이름을 가지고 "안녕하세요? 저는 @@입니다 문장 출력
val inputName = nameEdt.text.toString()
val printMessage = "안녕하세요? 저는 ${inputName}입니다"
Toast.makeText(this,printMessage,Toast.LENGTH_SHORT).show()
}
}
}
전송이나 동작이 필요한 태그에 id를 부여하여 kotlin에서 setonclicklistener를 활용할 수 있도록 한다.
2020/08/28 - [분류 전체보기] - [안드로이드] 버튼 이벤트 처리 - setOnClickListener
[안드로이드] 버튼 이벤트 처리 - setOnClickListener
개요 앱의 동작 코드 작성: 어떤 상황에서 => 어떤 동작으로 대응할지 작성 사용자가 버튼을 누르면 실행할 내용을 지정해주는 방법 => [클릭 이벤트 처리] 라고 표현함 앱 코딩의 제일 기본: 사용
90052.tistory.com
로그는 사용자에게는 보이지 않고, 개발자만 알 수 있는 사항이다.
아래와 같이 Logcat 화면에서 로그가 찍힌 것을 확인할 수 있다.
토스트를 사용하면 사용자에게 간단하게 알림을 보여줄 수 있다.
아래와 같이 짧게 문구가 뜬다.
데이터를 가져올 때는 .을 사용한다.
2020/08/28 - [프로그래밍언어/Kotlin] - [안드로이드] 토스트(Toast)
[안드로이드] 토스트(Toast)
개요 사용자에게 심각하지 않은 알림을 주고 싶을 때 사용 => 심각: 진짜로 실행할건지 확인을 받아야 하는 것들 (ex. 삭제 / 차단 / 로그아웃) 심각한 알림은 별도로 Alert 등의 기능으로 표시 사용�
90052.tistory.com
'공부 > Kotlin' 카테고리의 다른 글
[안드로이드] 토스트(Toast) (0) | 2020.08.28 |
---|---|
[안드로이드] 버튼 이벤트 처리 - setOnClickListener (0) | 2020.08.28 |
[안드로이드] 로그인 화면 구현 연습 (0) | 2020.08.28 |
[안드로이드] 이미지뷰(ImageView) (0) | 2020.08.28 |
[안드로이드] 에디트 텍스트(EditText) (0) | 2020.08.28 |