Android [ Java, Kotlin ]

안드로이드 프로그래밍 과제(Java) - Radio버튼 & AlertDialog

Moonsu99 2023. 11. 22. 13:24

환경

  • OS - Mac OS 13.5.2
  • Tools - Android Studio Iguana | 2023.2.1 Canary 5
  • Language - Java
  • android version - 12
  • tartgetSDK - 33
  • minSDK - 28

 

조건

1. 동물과 색상을 각각 선택하는 두 그룹의 RadioButton을 그림1과 같이 배치한다.

2. 그림2와 같이 RadioButton을 선택하고 그림보기Button을 클릭하면 그림3과 같이 AlertDialog가 표시되는데, 선택한 동물의 사진이 표시되고 그 앞에 동물 이름이 선택한 색상으로 겹쳐 표시된다. 

3. 제목 부분에도 동물 이름이 표시된다. 

4. 그림4 고양이 “RED”를 선택한 경우의 화면이다. “닫기 Button을 누르면 RadioButton이 선택이 해제되고 그림1로 돌아간다.

 

힌트

1. 화면은 수직 및 수평방향 LinearLayout을 중첩하여 구성할 수 있다. 

2. 대화상자에서 그림과 텍스트를 겹쳐 표시하기 위해 FrameLayout을 사용하면 된다. 

3. RadioButton의 선택을 해제하기 위해서는 RadioGroup에 clearCheck() 메소드를 사용하면 된다.

순서대로 그림 1,2,3
그림4

 

 

풀이

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    <RadioGroup
        android:id="@+id/animalRadioGroup"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/dogRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="강아지" />
        <RadioButton
            android:id="@+id/catRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="고양이" />
        <RadioButton
            android:id="@+id/rabbitRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="토끼" />
        <RadioButton
            android:id="@+id/horseRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="말" />
    </RadioGroup>

        <RadioGroup
            android:id="@+id/colorRadioGroup"
            android:layout_weight="1"

            android:layout_width="0dp"
            android:layout_height="wrap_content">
            <RadioButton
                android:id="@+id/redRadioButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#FF0000"
                android:text="RED" />
            <RadioButton
                android:id="@+id/greenRadioButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#1DDB16"
                android:text="GREEN" />
            <RadioButton
                android:id="@+id/blueRadioButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#0054FF"
                android:text="BLUE" />
        </RadioGroup>
    </LinearLayout>

    <Button
        android:id="@+id/showPictureButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="그림보기" />

</LinearLayout>

 

alertdialog.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:padding="30dp"
    android:layout_height="400dp">

    <ImageView
        android:id="@+id/animalImageView"
        android:layout_width="match_parent"
        android:layout_height="330dp" />

    <TextView
        android:id="@+id/animalNameTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="강아지"
        android:textColor="#FF0000"
        android:textSize="50sp"
        android:gravity="center"
        android:layout_gravity="bottom"
        android:padding="16dp"
        />


</FrameLayout>

 

 

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private RadioGroup animalRadioGroup;
    private RadioGroup colorRadioGroup;
    private Button showPictureButton;

    private TextView animalNameTextView;

    private ImageView animalImageView;

    private String selectedAnimalText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("HWork04");
        animalRadioGroup = findViewById(R.id.animalRadioGroup);
        colorRadioGroup = findViewById(R.id.colorRadioGroup);
        showPictureButton = findViewById(R.id.showPictureButton);

        showPictureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int selectedAnimalId = animalRadioGroup.getCheckedRadioButtonId();
                int selectedColorId = colorRadioGroup.getCheckedRadioButtonId();

                if (selectedAnimalId != -1 && selectedColorId != -1) {
                    RadioButton selectedAnimalRadioButton = findViewById(selectedAnimalId);
                    RadioButton selectedColorRadioButton = findViewById(selectedColorId);

                    String animalName = selectedAnimalRadioButton.getText().toString();
                    String selectedColor = selectedColorRadioButton.getText().toString();

                    showCustomAlertDialog(animalName, selectedColor);
                }
            }
        });
    }

    private void showCustomAlertDialog(String animalName, String selectedColor) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        LayoutInflater inflater = getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.alertdialog, null);
        builder.setView(dialogView);

        animalImageView = dialogView.findViewById(R.id.animalImageView);

        if (animalName.equals("강아지")) {
            animalImageView.setImageResource(R.drawable.dog);
        } else if (animalName.equals("고양이")) {
            animalImageView.setImageResource(R.drawable.cat);
        } else if (animalName.equals("토끼")) {
            animalImageView.setImageResource(R.drawable.rabbit);
        } else if (animalName.equals("말")) {
            animalImageView.setImageResource(R.drawable.horse);
        }

        animalNameTextView = dialogView.findViewById(R.id.animalNameTextView);
        animalNameTextView.setText(animalName);
        animalNameTextView.setTextColor(Color.parseColor(selectedColor));

        selectedAnimalText = "선택한 동물 : " + animalName;
        builder.setTitle(selectedAnimalText);

        builder.setPositiveButton("닫기", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                animalRadioGroup.clearCheck();
                colorRadioGroup.clearCheck();
            }
        });

        builder.show();
    }

}