본문 바로가기
Android

Android Dialog

by 캡틴노랑이 2021. 4. 2.
반응형

Android Dialog로 사용가능한 모든 예제...

 

더 있나??

 

test device : note 4

android os : Marshmallow 6.0

 

 

 

 

 

package com.example.sampleproject;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.sampleproject.Norang.NorangActivity;

import java.util.ArrayList;
import java.util.List;


//https://lktprogrammer.tistory.com/155
public class DialogActivity extends NorangActivity {

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

    private void InitializeCommonDataBind() {

    }

    private void InitializeControl() {
        toolbar = findViewById(R.id.tbToolBar);
        setToolbar("");
    }


    public void btnButtonOnClick(View view)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("sample button add");
        builder.setMessage("set message");

        builder.setPositiveButton("button 1", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int id)
            {
                Toast.makeText(getApplicationContext(), "button1", Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("button 2", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int id)
            {
                Toast.makeText(getApplicationContext(), "button2", Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNeutralButton("button 3", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int id)
            {
                Toast.makeText(getApplicationContext(), "button3", Toast.LENGTH_SHORT).show();
            }
        });

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


    public void btnDlgResStringOnClick(View view)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Alert List");

        //res/values/strings/alertList 목록을 가져옴.
        builder.setItems(R.array.alertList, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int pos) {
                String [] items = getResources().getStringArray(R.array.alertList);
                Toast.makeText(getApplicationContext(), items[pos], Toast.LENGTH_LONG).show();
            }
        });
        //builder.setCancelable(false);    // 뒤로 가기 버튼 사용 금지
        AlertDialog alert = builder.create();
        alert.show();
    }

    public void btnDlgListAddOnClick(View view)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Alert List");

        List lst = new ArrayList();
        lst.add("test1");
        lst.add("test2");
        lst.add("test3");
        lst.add("test4");

        final CharSequence[] items = lst.toArray(new CharSequence[lst.size()]);

        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int pos) {
                //String [] items = getResources().getStringArray(R.array.alertList);
                Toast.makeText(getApplicationContext(), items[pos], Toast.LENGTH_LONG).show();
            }
        });
        //builder.setCancelable(false);    // 뒤로 가기 버튼 사용 금지
        AlertDialog alert = builder.create();
        alert.show();
    }

    public void btnMultiChkOnClick(View view)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Multi check List");

        final ArrayList lstSelectedItems = new ArrayList();
        final String[] items = getResources().getStringArray(R.array.alertList);


        builder.setMultiChoiceItems(R.array.alertList, null, new DialogInterface.OnMultiChoiceClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int pos, boolean isChecked)
            {
                if(isChecked == true)
                    lstSelectedItems.add(items[pos]);
                else
                    lstSelectedItems.remove(pos);
            }
        });

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int pos)
            {
                String seletedItems = "";

                for(int i =0; i<lstSelectedItems.size(); i++)
                    seletedItems += (seletedItems.equals("") ? "" : ",")  + lstSelectedItems.get(i);

                Toast toast = Toast.makeText(getApplicationContext(), "Selected Item :" + seletedItems, Toast.LENGTH_LONG);
                //toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        });


        //builder.setCancelable(false);    // 뒤로 가기 버튼 사용 금지
        AlertDialog alert = builder.create();
        alert.show();
    }


    public void btnSingleSelectOnClick(View view)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Multi check List");

        final String[] items = getResources().getStringArray(R.array.alertList);
        final String[] selected = {""};


        builder.setSingleChoiceItems(R.array.alertList, 0, new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int pos)
            {
                selected[0] = items[pos];
            }
        });

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int pos)
            {
                Toast toast = Toast.makeText(getApplicationContext(), "Selected Item : " + selected[0], Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        });


        //builder.setCancelable(false);    // 뒤로 가기 버튼 사용 금지
        AlertDialog alert = builder.create();
        alert.show();
    }

    public void btnCustomDlgOnClick(View view)
    {
        View dialogView = getLayoutInflater().inflate(R.layout.activity_dialog_sub, null);

        final EditText etTxt1 = (EditText)dialogView.findViewById(R.id.etTxt1);
        final EditText etTxt2 = (EditText)dialogView.findViewById(R.id.etTxt2);

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

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int pos)
            {
                Toast.makeText(getApplicationContext(),etTxt1.getText() + "\n" + etTxt2.getText(), Toast.LENGTH_LONG).show();
            }
        });

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

반응형

댓글