print("와챠의 개발 기록장")
[안드로이드] 직접 풀어보기 5-3 본문
반응형
직접 풀어보기 5-3
다음 화면을 XML 파일 없이 Kotlin 코드만으로 작성하라.
- 레이아웃에 에디트 텍스트 1개, 버튼 1개, 텍스트큐 1개를 생성한다.
- 버튼을 클릭하면 에디트 텍스트에 쓰인 문자열이 텍스트뷰에 나타나게 한다.

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 android.graphics.Color | |
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 btn : Button | |
lateinit var tv : TextView | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
//setContentView(R.layout.activity_main) | |
// 리니어 레이아웃의 설정 | |
var params = LinearLayout.LayoutParams( | |
LinearLayout.LayoutParams.MATCH_PARENT, | |
LinearLayout.LayoutParams.MATCH_PARENT) | |
// 리니어 레이아웃 생성 및 설정 | |
val baseLayout = LinearLayout(this) | |
baseLayout.orientation = LinearLayout.VERTICAL | |
setContentView(baseLayout, params) | |
// 에디트 텍스트 생성 및 설정 | |
edit = EditText(this) | |
edit.setHint("입력하세요.") | |
// 버튼 생성 및 설정 | |
btn = Button(this) | |
btn.text = "버튼입니다." | |
btn.setBackgroundColor(Color.rgb(134, 229, 127)) | |
btn.setTextColor(Color.BLACK) | |
btn.setOnClickListener { | |
var str = edit.text.toString() | |
tv.setText(str) | |
} | |
// 텍스트뷰 생성 및 설정 | |
tv = TextView(this) | |
tv.setTextColor(Color.MAGENTA) | |
tv.setTextSize(20.0f) | |
// 레이아웃에 뷰 달기 | |
baseLayout.addView(edit) | |
baseLayout.addView(btn) | |
baseLayout.addView(tv) | |
} | |
} |
헉헉 아니...
왜 있는 기능을 쓰질 못하게 하는겨
반응형
'코딩 일기장 > Android(Kotlin)' 카테고리의 다른 글
[안드로이드] 직접 풀어보기 6-2(뷰플리퍼로 자동 사진 넘기기) (0) | 2021.03.27 |
---|---|
[안드로이드] 직접 풀어보기 5-4 (0) | 2021.03.20 |
[안드로이드] 직접 풀어보기 5-2 (0) | 2021.03.20 |
[안드로이드] 직접 풀어보기 5-1 (0) | 2021.03.20 |
[안드로이드] 4장 연습문제 9번 (0) | 2021.03.20 |