안드로이드 스튜디오 알림창 띄우기 ( AlertDIalog, Toast )

2019. 12. 23. 22:002020/Android App Develop

 

List Button Click => 알림창으로 리스트의 요소들이 출력 => 요소 Click => Toast창으로 어떤 요소를 클릭했는지 확인가능 

End Button Click => 알림창으로 확인버튼과 취소버튼이 표시 => 확인 버튼 클릭 시 앱 종료, 클릭 시 알림창 종료

 

< AlertDialog 관련 메서드 >

  AlertDialogBuilder builder = new AlertDialogBuilder( Context )

  builder.setTitle("")

  builder.setMessage("")

  builder.setItems( List )

  builder.setPositiveButton( TEXT , DialogInterface.OnClickListener .. )

  builder.setNegativeButton( TEXT, DIalogInterface.OnClickListener .. )

  AlertDialog AlertD = builder.create();

  AlertD.show()

 

<activity_practice.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
29
30
31
32
33
34
35
36
37
38
39
40
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PracticeActivity">
 
 
    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <RelativeLayout
            android:layout_margin="30dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
                <Button
                    android:id="@+id/Button_List"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:text="List"
                    ></Button>
 
                <Button
                    android:id="@+id/Button_End"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_alignParentBottom="true"
                    android:text="종료"
                    ></Button>
 
        </RelativeLayout>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

 

 

<PracticeActivity.java>

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.example.myapplication;
 
import android.content.DialogInterface;
 
 
public class PracticeActivity extends AppCompatActivity {
 
    public Button button_list;
    public Button button_end;
    public String[] list = {"Apple juice,","Grape Juice""Orange Juice"};
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_practice);
 
        button_list = findViewById(R.id.Button_List);
        //button 클릭 감지 메서드
        button_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 1.builder 선언
                AlertDialog.Builder builder = new AlertDialog.Builder(PracticeActivity.this);
                // 2.알림창 제목 설정
                builder.setTitle("LIST");
                // 3. 알림창에 띄울 요소 가져옴, 요소 클릭 감지 메서드
                builder.setItems(list, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 요소 클릭 시 Toast실행
                        Toast.makeText(getApplicationContext(),list[which],Toast.LENGTH_LONG).show();
                    }
                });
                // 알림창 생성 및 실행
                AlertDialog alertD = builder.create();
                alertD.show();
            }
 
        });
 
 
        button_end = findViewById(R.id.Button_End);
        button_end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(PracticeActivity.this);
                builder.setTitle("Program 종료");
                builder.setPositiveButton("종료하기"new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(),"Clicked End",Toast.LENGTH_LONG);
                        finish();
                    }
                });
                builder.setNegativeButton("취소"new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(),"Clicked Cancel", Toast.LENGTH_SHORT).show();
                        dialog.cancel();
                    }
                });
                AlertDialog alertD = builder.create();
                alertD.show();
            }
        });
 
 
 
 
 
    } // OnCreate END
 
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

< 실행결과 >