본문 바로가기
학교 강의/안드로이드 교양

4주차

by hoshi03 2023. 10. 3.

android, iphone, windowphone;

실습 1 - 체크박스를 체크하면 토스트 메세지를 띄우는 실습

CompoundButton - 체크박스, 라디오버튼, 스위치, 토글버튼의 상위 클래스

onCheckedChanged 메서드 사용할때 호출한다

체크박스 - 여러개를 체크 가능한 체크박스 UI

 

activity_main.xml로 가서 레이아웃을 LineayLayout으로 변경하고 윈도우폰,애플,안드로이드에 해당하는 체크박스를 만든 후 id를 할당해서 버튼 이벤트가 가능하게 한다

 

radio - 그룹 내의 라디오버튼은 중복으로 선택 x

switch on/off 두가지 가능한 스위치

togglebutton - 밀어서 두가지중에 하나고르는 느낌의 버튼

 

<?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/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="37dp"
        android:text="checkbox배우기"/>

    <CheckBox
        android:id="@+id/android"
        android:text="android"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <CheckBox
        android:id="@+id/iphone"
        android:text="iphone"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <CheckBox
        android:id="@+id/windowphone"
        android:text="windowphone"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Switch
        android:id="@+id/switch1"
        android:text="on/off"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
        <ToggleButton
            android:id="@+id/toggle1"
            android:text="toggle"
            android:textSize="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
<RadioGroup
    android:id="@+id/group"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <RadioButton
        android:id="@+id/rdobtn1"
        android:text="man"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <RadioButton
        android:id="@+id/rdobtn2"
        android:text="woman"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RadioGroup>
</LinearLayout>

 MainActivity.java에서는 CheckBox 변수로 만들어둔 android, iphone, windowphone을  id를 찾아서 가져오고

onCheckedChanged 메서드로 해당 체크박스를 클릭하면 토스트 메세지가 뜨는 기능을 구현한다

package com.example.AAP;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    //체크박스 가져오기
    CheckBox android, iphone, windowphone;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        android = findViewById(R.id.android);
        iphone = findViewById(R.id.iphone);
        windowphone = findViewById(R.id.windowphone);

        //온클릭 이벤트 리스너로 이벤트 실행
        android.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                //팝업 위치,텍스트,팝업 유지 시간을 받아서 팝업창을 띄우는 함수 makeText
                Toast.makeText(MainActivity.this, "android good", Toast.LENGTH_LONG).show();
            }
        });

        iphone.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Toast.makeText(MainActivity.this, "iphone good", Toast.LENGTH_LONG).show();
            }
        });

        windowphone.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Toast.makeText(MainActivity.this, "windowphone good", Toast.LENGTH_LONG).show();
            }
        });
    }
}

실행 화면

 

실습 2 - 체크한 라디오 버튼 텍스트에 맞는 동물 이미지를 띄우는 것 같은데 녹화가 10분만 되고 끊어진 것 같습니다

이미지 넣을땐 drawble이나 mipmap 폴더에 넣기

라디오 버튼, 라디오 그룹 - 라디오 버튼은 같은 그룹안에 잇으면 하나씩만 선택 가능

선택한 동물의 이미지가 팝업되는거 같은 2번과제에서는 체크박스보다는 라디오 버튼이 적절해서 라디오 버튼을 사용한 것 같습니다.

 

 

activity_main.xml은 상단에 텍스트 뷰와 체크 박스로 애완동물 선택을 시작하는지 여부를 묻고

선택을 시작하면 숨겨뒀던 ui들을 보이게 한 후

버튼 온클릭 이벤트로 해당하는 버튼을 클릭하면 이미지뷰테서 동물 사진을 띄워주는 것으로 이해했습니다

<?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/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="애완동물 선택을 시작할까요?"/>
    <CheckBox
        android:id="@+id/chkAgree"
        android:text="start"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        android:text="Which animal do you like?"
        android:textSize="30dp"/>
    <RadioGroup
        android:id="@+id/group"
        android:orientation="vertical"
        android:visibility="invisible"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/rdodog"
            android:text="dog"
            android:textSize="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <RadioButton
            android:id="@+id/rdocat"
            android:text="cat"
            android:textSize="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <RadioButton
            android:id="@+id/rdorabbit"
            android:text="rabbit"
            android:textSize="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </RadioGroup>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btndog"
            android:visibility="invisible"
            android:text="dog"
            android:textSize="20dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/btncat"
            android:visibility="invisible"
            android:text="cat"
            android:textSize="20dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/btnrabbit"
            android:visibility="invisible"
            android:text="rabbit"
            android:textSize="20dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <ImageView
        android:src="@mipmap/ic_launcher"
        android:visibility="invisible"
        android:layout_gravity="center"
        android:id="@+id/imgPet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

 

package com.example.AAP;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
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 text;
    CheckBox chkAgree;
    Button btndog, btncat, btnrabbit;
    ImageView imgPet;
    RadioButton rdodog, rdocat, rdorabbit;
    RadioGroup radioGroup;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = findViewById(R.id.text);
        chkAgree = findViewById(R.id.chkAgree);
        radioGroup = findViewById(R.id.group);
        btndog = findViewById(R.id.btndog);
        btncat = findViewById(R.id.btncat);
        btnrabbit = findViewById(R.id.btnrabbit);
        imgPet = findViewById(R.id.imgPet);

        rdodog = findViewById(R.id.rdodog);
        rdocat = findViewById(R.id.rdocat);
        rdorabbit = findViewById(R.id.rdorabbit);

        chkAgree.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (chkAgree.isChecked()){
                    text.setVisibility(View.VISIBLE);
                    radioGroup.setVisibility(View.VISIBLE);
                    btncat.setVisibility(View.VISIBLE);
                    btndog.setVisibility(View.VISIBLE);
                    btnrabbit.setVisibility(View.VISIBLE);
                    imgPet.setVisibility(View.VISIBLE);
                }
                else {
                    text.setVisibility(View.INVISIBLE);
                    radioGroup.setVisibility(View.INVISIBLE);
                    btndog.setVisibility(View.INVISIBLE);
                    btncat.setVisibility(View.INVISIBLE);
                    btnrabbit.setVisibility(View.INVISIBLE);
                    imgPet.setVisibility(View.INVISIBLE);
                }
            }
        });
        btndog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imgPet.setImageDrawable(getDrawable(R.drawable.dog));
            }
        });
        btncat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imgPet.setImageDrawable(getDrawable(R.drawable.cat));
            }
        });
        btnrabbit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imgPet.setImageDrawable(getDrawable(R.drawable.rabbit));
            }
        });
    }
}

실행 사진

'학교 강의 > 안드로이드 교양' 카테고리의 다른 글

안드교양 10주차  (0) 2023.11.09
시험대비  (1) 2023.10.25
안드교양 7주차 레이아웃, 시험범위  (0) 2023.10.19
7주차, 시험  (0) 2023.10.12
안드로이드 교양 2,3주차  (0) 2023.09.21