MainActivity

...더보기
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.example.boostcoursepractice;
 
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
 
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
import com.google.android.material.snackbar.Snackbar;
 
import org.w3c.dom.Text;
 
public class MainActivity extends AppCompatActivity {
 
    Button btn;
    TextView text;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        btn = (Button)findViewById(R.id.button);
        text = (TextView)findViewById(R.id.textView);
 
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showMessage();
            }
        });
    }
 
    public void showMessage() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("안내");
        builder.setMessage("종료하시겠습니까?");
        builder.setIcon(android.R.drawable.ic_dialog_alert);
 
        builder.setPositiveButton("예"new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Snackbar.make(text, "예 버튼이 눌렸습니다.", Snackbar.LENGTH_LONG).show();
            }
        });
 
        builder.setNegativeButton("아니오"new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Snackbar.make(text, "아니오 버튼이 눌렸습니다.", Snackbar.LENGTH_LONG).show();
            }
        });
 
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
}
 
cs

 

activity_main.xml

...더보기
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
<?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">
 
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="알림 대화상자 띄우기"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="88dp"
        android:text="대화상자 뷰"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
cs

 

 

알림 대화 상자는 다음과 같이 builder를 이용하여 설정해주고

 

  AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("안내");

        builder.setMessage("종료하시겠습니까?");

        builder.setIcon(android.R.drawable.ic_dialog_alert);

 

        builder.setPositiveButton("예"new DialogInterface.OnClickListener() {

            @Override

            public void onClick(DialogInterface dialogInterface, int i) {

                Snackbar.make(text, "예 버튼이 눌렸습니다.", Snackbar.LENGTH_LONG).show();

            }

        });

 

        builder.setNegativeButton("아니오"new DialogInterface.OnClickListener() {

            @Override

            public void onClick(DialogInterface dialogInterface, int i) {

                Snackbar.make(text, "아니오 버튼이 눌렸습니다.", Snackbar.LENGTH_LONG).show();

            }

        });

 

마지막에 

 

AlertDialog alertDialog = builder.create();

        alertDialog.show();

 

create하여 show해주면 된다.

 

 

결과화면

+ Recent posts