티스토리 뷰

먼저 화면과 xml코드를 보여드리겠습니다

실행했을때 화면
ㅎㅎㅎㅎ를 입력하고 전송을 눌렀을 때의 화면

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="#7500FF"
        android:gravity="center"
        android:textColor="#ffffff"
        android:textSize="20sp"
        android:textStyle="bold|italic"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:hint="적어주세요"
        android:inputType="text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:onClick="button_click"
        android:text="전송!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

먼저 위와 같이 xml에 입력합니다

 

EditText에 hint는 에디트텍스트에 아무것도 안 적혀있을 때 나타낼 문구를 적어주시면 됩니다

inputType은 용도에 맞게 설정해주시면 됩니다

위에선 text로 지정했지만 number을 지정한다면 숫자만 적을 수 있으며 종류가 많이 있습니다

 

Button에서 onClick은 버튼 클릭시 java에 button_click함수를 호출합니다

 

package com.ehsehsl.osz;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {
    TextView textView;
    EditText editText;

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

        textView = findViewById(R.id.textView);
        editText = findViewById(R.id.editText);
    }

    public void button_click(View view){
        textView.setText(editText.getText().toString());
    }
}

앱이 실행되면 onCreate가 실행되며 xml에 만들어둔 텍스트뷰와 에디트텍스트를 변수에 저장합니다

버튼 클릭시 button_click가 호출되어 텍스트뷰에 에디트텍스트에 적혀있는 텍스트를 받아와서 저장합니다

여기서 주의하실 점은 onClick으로 java에 있는 함수를 호출할때는 (View view)를 입력해주셔야 합니다(view는 원하는 대로 바꿔서 적어도 됨)

 

전송! 을 눌렀을 때 에디트텍스트에 있는 문구가 지워지도록 하고 싶다면

editText.setText("");

위 코드를 textView.setText(editText.getText().toString()); 밑에 추가해주시면 됩니다

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함