반응형
main.java
package com.cookandroid.ex99;
import androidx.appcompat.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.Switch;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView text1,text2;
Switch swiAgree;
RadioGroup rGroup1;
RadioButton rdoDog,rdoCat,rdoRabbit;
Button btnOK ,btn1,btn2;
ImageView imgPet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("안드로이드 사진 보기");
text1 = (TextView) findViewById(R.id.Text1);
swiAgree = (Switch) 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);
btn1 = (Button) findViewById(R.id.button);
btn2 = (Button) findViewById(R.id.button2);
imgPet = (ImageView) findViewById(R.id.Imgpet);
swiAgree.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(swiAgree.isChecked()==true) {
text2.setVisibility(View.VISIBLE);
rGroup1.setVisibility(View.VISIBLE);
imgPet.setVisibility(View.VISIBLE);
}
else
{
text2.setVisibility(View.INVISIBLE);
rGroup1.setVisibility(View.INVISIBLE);
imgPet.setVisibility(View.INVISIBLE);
}
}
});
rGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (rGroup1.getCheckedRadioButtonId()){
case R.id.RdoDog:
imgPet.setImageResource(R.drawable.nuga);
break;
case R.id.Rdocat:
imgPet.setImageResource(R.drawable.oreo);
break;
case R.id.Rdorabbit:
imgPet.setImageResource(R.drawable.pie);
break;
}
}
});
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
swiAgree.setChecked(false);
}
});
}
}
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"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="30dp">
<TextView
android:id="@+id/Text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="선택을 시작하겠습니까?"
android:layout_margin="10dp"/>
<Switch
android:id="@+id/ChkAgree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="시작함"/>
<TextView
android:id="@+id/Text2"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="좋아하는 안드로이드버전은?"
android:visibility="invisible"/>
<RadioGroup
android:layout_margin="10dp"
android:id="@+id/Rgroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible">
<RadioButton
android:layout_margin="10dp"
android:id="@+id/RdoDog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="누가7.0"/>
<RadioButton
android:layout_margin="10dp"
android:id="@+id/Rdocat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="오레오8.0"/>
<RadioButton
android:layout_margin="10dp"
android:id="@+id/Rdorabbit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="파이9.0"/>
</RadioGroup>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="종료" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="처음으로" />
<ImageView
android:id="@+id/Imgpet"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
</LinearLayout>
반응형
'Programming > Android' 카테고리의 다른 글
안드로이드 프로그래밍 4장 연습문제8 (안드로이드 예제) (0) | 2021.08.16 |
---|---|
안드로이드 프로그래밍 4장 연습문제7 (안드로이드 예제) (0) | 2021.08.16 |
안드로이드 프로그래밍 기본위젯 예제 (0) | 2021.08.16 |
안드로이드 프로그래밍 4장 초간단계산기 (0) | 2021.08.16 |
안드로이드 프로그래밍 2장 예제 (fourbutton,edittext,radiobutton,imageview) (0) | 2021.08.16 |