| |
---|
| | |
---|
| | import android.app.AlertDialog; |
---|
| | import android.app.Dialog; |
---|
| | import android.content.DialogInterface; |
---|
| | import android.graphics.Color; |
---|
| | import android.graphics.drawable.ColorDrawable; |
---|
| | import android.os.Bundle; |
---|
| | import androidx.annotation.NonNull; |
---|
| | import androidx.annotation.Nullable; |
---|
| | import androidx.fragment.app.DialogFragment; |
---|
| | import androidx.fragment.app.Fragment; |
---|
| | |
---|
| | import android.util.Log; |
---|
| | import android.view.LayoutInflater; |
---|
| |
---|
| | import android.widget.RadioButton; |
---|
| | import android.widget.RadioGroup; |
---|
| | import android.widget.Toast; |
---|
| | |
---|
| | import com.example.citrusclient.Citrus; |
---|
| | import com.example.citrusclient.R; |
---|
| | |
---|
| | /** |
---|
| | * A simple {@link Fragment} subclass. |
---|
| | * Use the {@link BookColorDialogFragment#newInstance} factory method to |
---|
| | * create an instance of this fragment. |
---|
| | */ |
---|
| | public class BookColorDialogFragment extends Fragment { |
---|
| | private RadioGroup radioGroupColors; |
---|
| | public class BookColorDialogFragment extends DialogFragment { |
---|
| | private Citrus citrus; |
---|
| | |
---|
| | // TODO: Rename parameter arguments, choose names that match |
---|
| | // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER |
---|
| | private static final String ARG_PARAM1 = "param1"; |
---|
| | private static final String ARG_PARAM2 = "param2"; |
---|
| | CreateBookFragment target; |
---|
| | |
---|
| | // TODO: Rename and change types of parameters |
---|
| | private String mParam1; |
---|
| | private String mParam2; |
---|
| | BookColorDialogFragment(CreateBookFragment fr){ |
---|
| | target=fr; |
---|
| | } |
---|
| | @NonNull |
---|
| | @Override |
---|
| | public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { |
---|
| | // ダイアログを生成 |
---|
| | Dialog dialog = new Dialog(requireContext()); |
---|
| | dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); |
---|
| | dialog.setContentView(R.layout.fragment_book_color_dialog); |
---|
| | |
---|
| | public BookColorDialogFragment() { |
---|
| | // Required empty public constructor |
---|
| | } |
---|
| | RadioGroup rg = dialog.findViewById(R.id.radioGroupColors); |
---|
| | |
---|
| | /** |
---|
| | * Use this factory method to create a new instance of |
---|
| | * this fragment using the provided parameters. |
---|
| | * |
---|
| | * @param param1 Parameter 1. |
---|
| | * @param param2 Parameter 2. |
---|
| | * @return A new instance of fragment BookColorDialogFragment. |
---|
| | */ |
---|
| | // TODO: Rename and change types and number of parameters |
---|
| | public static BookColorDialogFragment newInstance(String param1, String param2) { |
---|
| | BookColorDialogFragment fragment = new BookColorDialogFragment(); |
---|
| | Bundle args = new Bundle(); |
---|
| | args.putString(ARG_PARAM1, param1); |
---|
| | args.putString(ARG_PARAM2, param2); |
---|
| | fragment.setArguments(args); |
---|
| | return fragment; |
---|
| | } |
---|
| | |
---|
| | @Override |
---|
| | public void onCreate(Bundle savedInstanceState) { |
---|
| | super.onCreate(savedInstanceState); |
---|
| | if (getArguments() != null) { |
---|
| | mParam1 = getArguments().getString(ARG_PARAM1); |
---|
| | mParam2 = getArguments().getString(ARG_PARAM2); |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | @Override |
---|
| | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
---|
| | Bundle savedInstanceState) { |
---|
| | // Inflate the layout for this fragment |
---|
| | return inflater.inflate(R.layout.fragment_book_color_dialog, container, false); |
---|
| | } |
---|
| | |
---|
| | int selected = 0; |
---|
| | // @Override |
---|
| | |
---|
| | @Override |
---|
| | public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { |
---|
| | super.onViewCreated(view, savedInstanceState); //親クラスである Fragment の onViewCreated() メソッドを呼び出す |
---|
| | view.findViewById(R.id.buttonCancel).setOnClickListener(new View.OnClickListener() { |
---|
| | @Override |
---|
| | public void onClick(View view) { |
---|
| | ((MainActivity) getActivity()).showFragment(new CreateBookFragment()); |
---|
| | } |
---|
| | }); |
---|
| | |
---|
| | radioGroupColors = view.findViewById(R.id.radioGroupColors); |
---|
| | radioGroupColors.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { |
---|
| | // ラジオグループのチェック状態変更リスナーを設定 |
---|
| | rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { |
---|
| | @Override |
---|
| | public void onCheckedChanged(RadioGroup group, int checkedId) { |
---|
| | // ラジオボタンが選択された時の処理 |
---|
| | RadioButton checkedRadioButton = group.findViewById(checkedId); |
---|
| | if (checkedRadioButton != null && checkedRadioButton.isChecked()) { |
---|
| | String selectedColor = checkedRadioButton.getText().toString(); |
---|
| | radioGroupColors.findViewById(R.id.radioButtonSprout).setOnClickListener(new View.OnClickListener() { // |
---|
| | @Override |
---|
| | public void onClick(View view) { |
---|
| | ((MainActivity) getActivity()).showFragment(new CreateBookFragment()); |
---|
| | } |
---|
| | }); |
---|
| | // 選択された色に応じた処理をここに記述する |
---|
| | // 例えば、選択された色をログに出力する |
---|
| | Log.d("ColorSelection", "Selected color: " + selectedColor); |
---|
| | // チェックされたラジオボタンのIDを取得 |
---|
| | RadioButton radioButton = group.findViewById(checkedId); |
---|
| | if (radioButton != null) { |
---|
| | int selectedId = radioButton.getId(); |
---|
| | // ここで選択されたラジオボタンのID(selectedId)を使用して処理を行う |
---|
| | if (selectedId == R.id.radioButtonShadow) { |
---|
| | target.returnValue("#A5A5A5"); |
---|
| | } else if (selectedId == R.id.radioButtonSprout) { |
---|
| | target.returnValue("#A2FA83"); |
---|
| | } else if (selectedId == R.id.radioButtonAqua) { |
---|
| | target.returnValue("#A3F4FF"); |
---|
| | } else if (selectedId == R.id.radioButtonLavender) { |
---|
| | target.returnValue("#B994FF"); |
---|
| | } else if (selectedId == R.id.radioButtonJasmine) { |
---|
| | target.returnValue("#FFFFFF"); |
---|
| | } else if (selectedId == R.id.radioButtonCitrus) { |
---|
| | target.returnValue("#F6C755"); |
---|
| | } else if (selectedId == R.id.radioButtonSakura) { |
---|
| | target.returnValue("#F6C755"); |
---|
| | } |
---|
| | |
---|
| | } |
---|
| | } |
---|
| | }); |
---|
| | |
---|
| | view.findViewById(R.id.buttonOk).setOnClickListener(new View.OnClickListener() { |
---|
| | @Override |
---|
| | public void onClick(View view) { |
---|
| | ((MainActivity) getActivity()).showFragment(new CreateBookFragment()); |
---|
| | dismiss(); |
---|
| | } |
---|
| | }); |
---|
| | |
---|
| | |
---|
| | return dialog; |
---|
| | // RadioGroup rg = (RadioGroup) view.findViewById(R.id.radioGroupColors); |
---|
| | // |
---|
| | // // ラジオグループのチェック状態変更イベントを登録 |
---|
| | // rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { |
---|
| | // |
---|
| | // // チェック状態変更時に呼び出されるメソッド |
---|
| | // public void onCheckedChanged(RadioGroup group, int checkedId) { |
---|
| | // // チェック状態時の処理を記述 |
---|
| | // // チェックされたラジオボタンオブジェクトを取得 |
---|
| | // RadioButton radioButton = (RadioButton) view.findViewById(checkedId); |
---|
| | // } |
---|
| | // }); |
---|
| | // |
---|
| | // Dialog dialog = new Dialog(requireContext()); |
---|
| | // dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); |
---|
| | // |
---|
| | // dialog.setContentView(R.layout.fragment_book_color_dialog); |
---|
| | // |
---|
| | // |
---|
| | // return dialog; |
---|
| | } |
---|
| | |
---|
| | public Dialog onCreatedDialog(Bundle savedInstanceState){ |
---|
| | final String[] items = {"citrus","sakura","gray"}; |
---|
| | AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |
---|
| | return builder.setTitle("色の種類") |
---|
| | .setSingleChoiceItems(items, selected, new DialogInterface.OnClickListener() { |
---|
| | @Override |
---|
| | public void onClick(DialogInterface dialogInterface, int which) { |
---|
| | selected = which; |
---|
| | } |
---|
| | } |
---|
| | ) |
---|
| | .setPositiveButton("OK", new DialogInterface.OnClickListener() { |
---|
| | @Override |
---|
| | public void onClick(DialogInterface dialogInterface, int i) { |
---|
| | Toast.makeText(getActivity(),String.format("[%s]が選択されました",items[selected]), |
---|
| | Toast.LENGTH_SHORT).show(); |
---|
| | } |
---|
| | } |
---|
| | ) |
---|
| | .create(); |
---|
| | } |
---|
| | |
---|
| | // final String[] items = {"citrus","sakura","gray"}; |
---|
| | // AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |
---|
| | // return builder.setTitle("色の種類") |
---|
| | // .setSingleChoiceItems(items, selected, new DialogInterface.OnClickListener() { |
---|
| | // @Override |
---|
| | // public void onClick(DialogInterface dialogInterface, int which) { |
---|
| | // selected = which; |
---|
| | // } |
---|
| | // } |
---|
| | // ) |
---|
| | // .setPositiveButton("OK", new DialogInterface.OnClickListener() { |
---|
| | // @Override |
---|
| | // public void onClick(DialogInterface dialogInterface, int i) { |
---|
| | // Toast.makeText(getActivity(),String.format("[%s]が選択されました",items[selected]), |
---|
| | // Toast.LENGTH_SHORT).show(); |
---|
| | // } |
---|
| | // } |
---|
| | // ) |
---|
| | // .create(); |
---|
| | |
---|
| | } |
---|
| | |