[안드로이드 스튜디오] 버튼클릭시 토스트 메시지
감성 자동제어
안녕하세요! 버튼을 클릭했을 때 토스트 메시지를 띄우는 방법에 대해서 알아보겠습니다. 매우매우 간단하니까 코드를 보고 천천히 따라해주시면 감사하겠습니다!
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 |
마무리
이상으로 버튼을 눌러서 토스트 메시지를 띄우는 방법에 대해 알아보았습니다. 따라하시느라 고생하셨습니다. 감사합니다!!
'안드로이드 스튜디오' 카테고리의 다른 글
[안드로이드 스튜디오] XML을 이용한 옵션메뉴 만들기! (0) | 2020.11.25 |
---|---|
[안드로이드 스튜디오] 대화상자(dialog) 사용하는 방법-AlertDialog (0) | 2020.11.23 |
[안드로이드 스튜디오] 버튼 클릭시 이미지 변경!! (4) | 2020.11.20 |
[안드로이드 스튜디오] 버튼클릭시 홈페이지 열기!(Intent) (0) | 2020.11.18 |
[안드로이드 스튜디오] 버튼 클릭으로 화면전환하자!(Intent) (0) | 2020.11.16 |