RadioButtonTest2


RadioButtonTest2

문제

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_margin="10dp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="선택을 시작하겠습니까?"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:textSize="20sp"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onCheckboxClicked"
        android:text="시작함" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="좋아하는 애완동물은?"
        android:textSize="20sp"
        android:visibility="gone" />

    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="강아지"
            android:visibility="gone" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="고양이"
            android:visibility="gone" />

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="오리"
            android:visibility="gone" />
    </RadioGroup>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="선택 완료"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:visibility="gone" />

</LinearLayout>

MainActivity.java

package kr.ac.skuniv.a1006practice2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView textView;
    RadioButton dog,cat,duck;
    Button btn;
    ImageView image;
    RadioGroup radiogroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.textView);
        dog = (RadioButton) findViewById(R.id.radioButton);
        cat = (RadioButton) findViewById(R.id.radioButton2);
        duck = (RadioButton) findViewById(R.id.radioButton3);
        btn = (Button) findViewById(R.id.button);
        image = (ImageView) findViewById(R.id.imageView);
        radiogroup=(RadioGroup)findViewById(R.id.radiogroup);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch(radiogroup.getCheckedRadioButtonId()){
                    case R.id.radioButton:
                        image.setImageResource(R.drawable.dog);
                        break;
                    case R.id.radioButton2:
                        image.setImageResource(R.drawable.cat);
                        break;
                    case R.id.radioButton3:
                        image.setImageResource(R.drawable.duck);
                        break;
                }
                image.setVisibility(View.VISIBLE);
            }
        });
    }
    public void onCheckboxClicked(View view){
        boolean checked=((CheckBox)view).isChecked();
        if(checked){
            textView.setVisibility(View.VISIBLE);
            dog.setVisibility(View.VISIBLE);
            cat.setVisibility(View.VISIBLE);
            duck.setVisibility(View.VISIBLE);
            btn.setVisibility(View.VISIBLE);
        }
    }
    public void onRadioButtonClicked(View view){
        boolean checked =((RadioButton)view).isChecked();
    }
}






© 2021.07. by 전은성

Powered by 전은성