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

[android/안드로이드] Dialog list, radio, check box형태의 dialog 예제

by 새아SaeA 2020. 1. 8.

다양한 형태의 dialog 예제

 

*기본 형태의 dialog

*list형태의 dialog

*radio button형태의 dialog

*checkbox button형태의 dialog 

 

1. 결과물

  1) 기본 형태 dialog

 

더 자세한 내용은 이 내용을 참고해 주세요.

[프로젝트 기록/android] - [android/안드로이드] Dialog button 버튼 눌러 다이얼로그 띄우기

 

  2) List Dialog ※ 클릭하자마자 확인을 누를 새도 없이 다음 action이 실행됨.

3) Radio Dialog

4) Checkbox Dialog

2. 각각 코드

/res/layout/activity_main.xml

<이전 포스팅 참고>

[프로젝트 기록/android] - [android/안드로이드] Dialog button 버튼 눌러 다이얼로그 띄우기

 

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

 

1) List Dialog

public void ListClick(View view) {
        new AlertDialog.Builder(this).setTitle("선택").setItems(words, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "words : " + words[which], Toast.LENGTH_LONG).show();
            }
        }).setNeutralButton("닫기", null).setPositiveButton("확인", null).show();
    }

2) Radio Dialog

public void RadioClick(View view) {
        new AlertDialog.Builder(this).setTitle("선택").setSingleChoiceItems(words, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "words : " + words[which], Toast.LENGTH_SHORT).show();
            }
        }).setNeutralButton("closed",null).setPositiveButton("OK",null).setNegativeButton("cancel", null).show();
    }

3) Checkbox Dialog

  public void CheckboxClick(View view) {
        new AlertDialog.Builder(this).setTitle("선택").setMultiChoiceItems(words, null, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                Toast.makeText(MainActivity.this, "words : " + words[which], Toast.LENGTH_SHORT).show();
            }
        }).setNeutralButton("closed", null).setPositiveButton("OK",null).setNegativeButton("cancel",null).show();
    }

 

3. 전체 코드

package com.example.dialog;

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.Toast;

public class MainActivity extends AppCompatActivity {


    final String[] words = new String[] {"사랑", "감사", "이해", "성공", "노력", "행운"};

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

    public void BasicClick(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();
    }

    public void ListClick(View view) {
        new AlertDialog.Builder(this).setTitle("선택").setItems(words, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "words : " + words[which], Toast.LENGTH_LONG).show();
            }
        }).setNeutralButton("닫기", null).setPositiveButton("확인", null).show();
    }

    public void RadioClick(View view) {
        new AlertDialog.Builder(this).setTitle("선택").setSingleChoiceItems(words, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "words : " + words[which], Toast.LENGTH_SHORT).show();
            }
        }).setNeutralButton("closed",null).setPositiveButton("OK",null).setNegativeButton("cancel", null).show();
    }

    public void CheckboxClick(View view) {
        new AlertDialog.Builder(this).setTitle("선택").setMultiChoiceItems(words, null, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                Toast.makeText(MainActivity.this, "words : " + words[which], Toast.LENGTH_SHORT).show();
            }
        }).setNeutralButton("closed", null).setPositiveButton("OK",null).setNegativeButton("cancel",null).show();
    }

}