728x90

# 실행 화면

# 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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="선택을 시작하겠습니까?" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="시작함"
        android:onClick="onCheckboxClicked"
        tools:layout_editor_absoluteX="220dp"
        tools:layout_editor_absoluteY="30dp" />

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

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

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

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

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

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

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            app:srcCompat="@drawable/dog" />
    </RadioGroup>

</LinearLayout>

# MainActivity.java

package kr.ac.skuniv.choicetest;

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.TextView;


public class MainActivity extends AppCompatActivity {
    TextView tView;
    RadioButton rButton1, rButton2, rButton3;
    Button btn;
    ImageView image;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("I Love Pet (전유정)");

        tView = (TextView) findViewById(R.id.textView2);
        rButton1 = (RadioButton) findViewById(R.id.radio_cat);
        rButton2 = (RadioButton) findViewById(R.id.radio_dog);
        rButton3 = (RadioButton) findViewById(R.id.radio_duck);
        image = (ImageView) findViewById(R.id.imageView);

        btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                image.setVisibility(View.VISIBLE);
            }
        });
    }
    public void onCheckboxClicked(View view){
        boolean checked = ((CheckBox) view).isChecked();

        if (checked){
            tView.setVisibility(View.VISIBLE);
            rButton1.setVisibility(View.VISIBLE);
            rButton2.setVisibility(View.VISIBLE);
            rButton3.setVisibility(View.VISIBLE);
            btn.setVisibility(View.VISIBLE);
        }
    }
    public void onRadioButtonClicked(View view){
        boolean checked = ((RadioButton) view).isChecked();
        switch(view.getId()){
            case R.id.radio_dog:
                if(checked)
                    image.setImageResource(R.drawable.dog);
                break;
            case R.id.radio_cat:
                if(checked)
                    image.setImageResource(R.drawable.cat);
                break;
            case R.id.radio_duck:
                if(checked)
                    image.setImageResource(R.drawable.duck);
                break;
        }
    }
}
반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기