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 | package com.example.a3; 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.Toast; public class MainActivity extends AppCompatActivity { private String[] items = {"망고쥬스", "토마토쥬스", "포도쥬스"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button listButton = (Button)findViewById(R.id.listButton); listButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("리스트"); builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(getApplicationContext(), items[i], Toast.LENGTH_SHORT).show(); } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } }); Button exitButton = (Button)findViewById(R.id.exitButton); exitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setMessage("정말로 종료하시겠습니까?"); builder.setTitle("종료 알림창") .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { finish(); } }) .setNegativeButton("NO", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int i) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.setTitle("종료 알림창"); alert.show(); } }); } } | cs |
'안드로이드 ' 카테고리의 다른 글
액티비티 화면전환 (0) | 2019.08.02 |
---|---|
리스트 뷰 꾸미기 (0) | 2019.08.02 |
메뉴 바 여러 개 만들기 (0) | 2019.08.02 |
계산기 (0) | 2019.08.02 |