태지쌤

로봇 & 코딩교육 No.1 크리에이터

카테고리 없음

[안드로이드 앱개발] 텍스트뷰 / 버튼 / 에디트텍스트 예제

태지쌤 2022. 7. 6. 10:24
반응형
<?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"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ff0000"
        android:textSize="20sp"
        android:text="안드로이드"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#0004ff"
        android:textSize="40sp"
        android:text="두번째 텍스트뷰"/>

    <EditText
        android:id="@+id/et1"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:hint="아이디를 입력하세요..."/>

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="버튼이에요"/>

</LinearLayout>
package com.example.test1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    EditText et1;
    Button btn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et1 = findViewById(R.id.et1);
        btn1 = findViewById(R.id.btn1);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et1.setText("만나서반가워요");
            }
        });
    }
}

버튼을 클릭하면 에디트텍스트 창에 "만나서반가워요"로 바뀜

XML로 디자인을 구성하고, 자바로 동적인 움직임을 코드로 구현한다.

반응형