본문 바로가기
프로젝트 기록/android

[android/안드로이드] Dialog button 버튼 눌러 다이얼로그 띄우기 예제

by 새아SaeA 2020. 1. 8.

버튼 눌러서 dialog 실행시키기 예제

 

1. 속성

AlertDialog

creat()

AlertDialog 생성

AloerDialog.Builder

setCancelable(boolean cancelabe)

뒤로가기(Back) 버튼 으로 대화상자를 닫을 수 있는지 여부/디폴트 true

AlertDialog.Builder

setIcon(Drawable icon)

아이콘을 타이틀에 설정

AlertDialog.Builder

setMessage(int messageId)

메시지를 리소스 아이디로 설정. 즉, 다른 res파일에 이 메시지를 따로 저장하는거

AlertDIalog.Builder

setMessage(CharSequence message)

메시지를 문자열로 설정한다.

AlertDialog.Builder

setPositiveButton(Charsequence text, DialogInterface.OnClickListener listener)

긍정(positive)버튼이 눌렸을 때 호출 될 리스너 설정

AlertDialog.Builder

setNegativeButton(Charsequence text, DialogInterface.OnClickListener listener)

부정(negative)버튼이 눌렸을 때 호출 될 리스너 설정

AlertDialog.Builder

setNeutralButton(Charsequence text, DialogInterface.OnClickListener listener)

중립(nuetral)버튼이 눌렸을 때 호출 될 리스너 설정

AlertDialog

show()

현재 빌더에 설정된 옵션으로 AlertDialog를 생성하고 이를 보여줌

 

2. 결과물

3. 코드

/res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activitymain">

    <Button
        android:id="@+id/dialogBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="DialogClick"
        android:layout_gravity="center"
        android:text="Dialog" />

</LinearLayout>

/java/com.example.dialog/MainActivity.java

package com.example.dialog;

public class MainActivity extends AppCompatActivity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void DialogClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("다이얼로그").setMessage("다이얼로그가 보인다면 성공입니다. 축하합니다!");
        builder.setPositiveButton("성공", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "Yeah!!", Toast.LENGTH_LONG).show();
            }
        });
        builder.setNegativeButton("실패", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(),"Try again!", Toast.LENGTH_LONG).show();
            }
        });

        builder.setNeutralButton("Nuetral", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(),"neutral click", Toast.LENGTH_LONG).show();
            }
        });

        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
}