package jp.gihyo.mitoma.android.s1201dialog.fragment;

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

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

import jp.gihyo.mitoma.android.s1201dialog.R;

public class MyDialogFragment extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        // ダイアログの生成
        AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
        builder.setTitle("ダイアログ表示");
        builder.setMessage("処理を終了しますか?");
        builder.setPositiveButton("YES",new MyDialogListener());
        builder.setNegativeButton("NO",new MyDialogListener());
        builder.setNeutralButton("CANCEL",new MyDialogListener());
        Dialog dialog = builder.create();
        // 生成したダイアログをリスナーに戻す
        return dialog;
    }
    private class MyDialogListener implements DialogInterface.OnClickListener{
        public void onClick(DialogInterface dialog,int buttonNo){
            EditText et = requireActivity().findViewById(R.id.etDialogData);
            String buttonName="";
            // ボタンによる振り分け
            switch(buttonNo){
                case DialogInterface.BUTTON_POSITIVE:
                    buttonName = "YESボタン";
                    break;
                case DialogInterface.BUTTON_NEGATIVE:
                    buttonName = "NOボタン";
                    break;
                case DialogInterface.BUTTON_NEUTRAL:
                    buttonName = "CANCELボタン";
                    break;
                default:
                    break;
            }
            // 結果の表示
            et.setText(buttonName);
        }
    }
}
