와챠의 우당탕탕 개발 기록장
[안드로이드] 4장 연습문제 8번 본문
반응형
8번 :
에디트 텍스트의 키가 눌릴 때마다 바뀐 글자가 토스트 메시지로 나오도록 프로젝트를 작성하시오.

activity_main.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity" | |
android:orientation="vertical"> | |
<EditText | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/edit" /> | |
</LinearLayout> |
MainActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.mytest | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.widget.* | |
import kotlin.system.exitProcess | |
class MainActivity : AppCompatActivity() { | |
// 변수 선언 | |
lateinit var edit : EditText | |
lateinit var str : String | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
title = "연습문제 4-8" | |
// 코틀린과 변수 연결 | |
edit = findViewById<EditText>(R.id.edit) | |
// 에디트 텍스트의 키가 눌릴 때마다 바뀐 글자가 토스트 메시기로 나오도록 | |
edit.setOnKeyListener { view, i, keyEvent -> | |
// 입력된 문자열 가져오기 | |
str = edit.text.toString() | |
// 입력된 문자열을 토스트로 보이기 | |
Toast.makeText(applicationContext, str, Toast.LENGTH_SHORT).show() | |
// 리턴값 | |
false | |
} | |
} | |
} |
영어로 치면 바로바로 나오는데
한글로 치면 한 박자 느리게 토스트가 뜬다.
반응형
'코딩 일기장 > Android(Kotlin)' 카테고리의 다른 글
[안드로이드] 직접 풀어보기 5-1 (0) | 2021.03.20 |
---|---|
[안드로이드] 4장 연습문제 9번 (0) | 2021.03.20 |
[안드로이드] 4장 연습문제 7번 (0) | 2021.03.20 |
[안드로이드] 직접 풀어보기 4-4 (0) | 2021.03.20 |
[안드로이드] 직접 풀어보기 4-3 (0) | 2021.03.20 |