본문으로 바로가기

[안드로이드 스튜디오] 버튼클릭시 토스트 메시지

 감성 자동제어 

 

안녕하세요! 버튼을 클릭했을 때 토스트 메시지를 띄우는 방법에 대해서 알아보겠습니다. 매우매우 간단하니까 코드를 보고 천천히 따라해주시면 감사하겠습니다!

 

Activity_main.xml(layout)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <Button
        android:id="@+id/btn_toast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="토스트 메시지 띄우기"
        android:textSize="40dp" />
 
</LinearLayout>
cs

 

MainActivity(Class)

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
package com.example.toast_message;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 
    Button mbtn_toast;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        mbtn_toast = findViewById(R.id.btn_toast);
 
        mbtn_toast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"토스트 메시지 생성", Toast.LENGTH_SHORT).show();
            }
        });
 
    }
}
cs

 

마무리

이상으로 버튼을 눌러서 토스트 메시지를 띄우는 방법에 대해 알아보았습니다. 따라하시느라 고생하셨습니다. 감사합니다!!

 

실행결과

 

긴 글 읽느라 수고하셨습니다.

오늘도 일상 속 소소한 행복을 느끼길 바랍니다!

 

더 많은 정보

https://engineering-mino.tistory.com/