package jp.gihyo.mitoma.android.p022widgetstest;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

public class AlertDialogFragment extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Toast.makeText(getContext().getApplicationContext(), "onCreateDialog", Toast.LENGTH_LONG).show();
        //Toast.makeText(getContext(), "NOボタンON", Toast.LENGTH_LONG).show();
        AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
        builder.setTitle("登録の確認");
        builder.setMessage("登録を実行しますか?");
        builder.setPositiveButton("YES",new MyDialogYesNoListener());
        builder.setNegativeButton("NO",new MyDialogYesNoListener());
        //.setNeutralButton("CANCEL",new MyDialogYesNoListener());
        Dialog dialog = builder.create();
        return dialog;
    }
    private class MyDialogYesNoListener implements DialogInterface.OnClickListener{
        public void onClick(DialogInterface dialog,int buttonNo){
            String buttonName="";
            switch(buttonNo){
                case DialogInterface.BUTTON_POSITIVE:
                    Toast.makeText(requireContext(), "YESボタンON", Toast.LENGTH_LONG).show();
                    //yesNo = "YES";
                    break;
                case DialogInterface.BUTTON_NEGATIVE:
                    Toast.makeText(requireContext(), "NOボタンON", Toast.LENGTH_LONG).show();
                    //yesNo = "NO";
                    break;
                /*
                case DialogInterface.BUTTON_NEUTRAL:
                    buttonName = "CANCELボタン";
                    break;

                 */
                default:
                    break;
            }
            //Toast.makeText(requireActivity(), yesNo, Toast.LENGTH_LONG).show();
        }
    }

}
