Programming/Android

안드로이드 프로그래밍 기본위젯 예제

fishersheep 2021. 8. 16. 17:39
반응형

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.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() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if(chkAgree.isChecked()==true) {
                    text2.setVisibility(View.VISIBLE);
                    rGroup1.setVisibility(View.VISIBLE);
                    btnOK.setVisibility(View.VISIBLE);
                    imgPet.setVisibility(View.VISIBLE);
                }
                else
                    {
                        text2.setVisibility(View.INVISIBLE);
                        rGroup1.setVisibility(View.INVISIBLE);
                        btnOK.setVisibility(View.INVISIBLE);
                        imgPet.setVisibility(View.INVISIBLE);
                    }
            }
        });

        btnOK.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                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();
                }
            }
        });
    }
}

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"/>

    <CheckBox
        android:id="@+id/ChkAgree"
        android:layout_width="match_parent"
        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="강아지"/>
    <RadioButton
        android:layout_margin="10dp"
        android:id="@+id/Rdocat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="고양이"/>
    <RadioButton
        android:layout_margin="10dp"
        android:id="@+id/Rdorabbit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="토끼"/>

    </RadioGroup>

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

    <ImageView
        android:id="@+id/Imgpet"
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="invisible"/>


</LinearLayout>

기본위젯 예제

반응형