정보를 보호하는 대학생

<안드로이드 스터디 04> EditText 입력 후 버튼누르면 그대로 TextView 출력 본문

개발/안드로이드 스터디

<안드로이드 스터디 04> EditText 입력 후 버튼누르면 그대로 TextView 출력

정보를 보호하는 대학생 2022. 1. 7. 18:13

1. xml 코드

<?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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >


    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="버튼입니다"

        />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        />


</LinearLayout>

2. 코틀린 코드

package com.android.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.*

class MainActivity : AppCompatActivity() {
//////////변수선언/////////////////

  lateinit var textView1: TextView
  lateinit var button1 : Button
  lateinit var editText1 : EditText

/////////////////////////////////

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        textView1 = findViewById(R.id.textView1)
        button1 = findViewById(R.id.button1)
        editText1 = findViewById(R.id.editText1)

        button1.setOnClickListener{
            textView1.text = editText1.text.toString()

        }
    }

}

3. 결과화면