IT/Android

android - 체크박스 이벤트 처리, 라디오 버튼 선택 후, 버튼 클릭하면 이미지뷰에 이미지 나오게 하기 (TextView, CheckBox, RadioGroup, RadioButton, Button, ImageView, setOnCheckedChangeListener, setOnClickListener)

노마드오브 2018. 9. 6. 22:36

파일명 : 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:padding="20dp" >

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

<CheckBox
android:id="@+id/ChkAgree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="시작함" />

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

<RadioGroup
android:id="@+id/Rgroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible" >

<RadioButton
android:id="@+id/RdoDog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="강아지" />

<RadioButton
android:id="@+id/RdoCat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="고양이" />

<RadioButton
android:id="@+id/RdoRabbit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="토끼" />
</RadioGroup>

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

<ImageView
android:id="@+id/ImgPet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible" />

</LinearLayout>


파일명 : MainActivity.java

package com.example.it.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

TextView text1, text2;
CheckBox chkAgree;
RadioGroup rGroup1;
RadioButton rdoDog, rdoCat, rdoRabbit;
Button btnOK;
ImageView imgPet;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("애완동물 사진 보기");

// 위젯을 변수에 대입
text1 = (TextView) findViewById(R.id.Text1);
chkAgree = (CheckBox) findViewById(R.id.ChkAgree);

text2 = (TextView) findViewById(R.id.Text2);
rGroup1 = (RadioGroup) findViewById(R.id.Rgroup1);
rdoDog = (RadioButton) findViewById(R.id.RdoDog);
rdoCat = (RadioButton) findViewById(R.id.RdoCat);
rdoRabbit = (RadioButton) findViewById(R.id.RdoRabbit);

btnOK = (Button) findViewById(R.id.BtnOK);
imgPet = (ImageView) findViewById(R.id.ImgPet);

// 체크박스의 체크가 변경되면
chkAgree.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// 체크되면 모두 보이도록 설정
if (chkAgree.isChecked() == true) {
text2.setVisibility(android.view.View.VISIBLE);
rGroup1.setVisibility(android.view.View.VISIBLE);
btnOK.setVisibility(android.view.View.VISIBLE);
imgPet.setVisibility(android.view.View.VISIBLE);
} else {
text2.setVisibility(android.view.View.INVISIBLE);
rGroup1.setVisibility(android.view.View.INVISIBLE);
btnOK.setVisibility(android.view.View.INVISIBLE);
imgPet.setVisibility(android.view.View.INVISIBLE);
}
}
});

// 선택확인 버튼을 클릭하면
btnOK.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {

switch (rGroup1.getCheckedRadioButtonId()) {
case R.id.RdoDog:
imgPet.setImageResource(R.drawable.dog);
break;
case R.id.RdoCat:
imgPet.setImageResource(R.drawable.cat);
break;
case R.id.RdoRabbit:
imgPet.setImageResource(R.drawable.rabbit);
break;
default:
Toast.makeText(getApplicationContext(), "동물 먼저 선택하세요", Toast.LENGTH_SHORT)
.show();
}
}
});

}
}


실행화면