본문 바로가기
Android/그림으로 쉽게 설명하는 안드로이드 프로그래밍 개정6판

[Android/그림으로 쉽게 설명하는 안드로이드 프로그래밍 개정6판]그림으로 쉽게 설명하는 안드로이드 프로그래밍(개정6판) 연습문제 5장

by starfish22 2022. 9. 24.
728x90

정답이 아닐 수 있습니다. 오류가 있으면 알려주세요ㅎ

 

1. 2번 <CheckBox>

 

2. 4번 <RadioButton>

 

3. 3번 numStarts

 

4. 3번 onTouchEvent()

 

5. 3번 ACTION_PRESS

 

6.

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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="어떤 애완동물을 보고 싶은가요?"
        android:textColor="#666666"
        android:textSize="25dp" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

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

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

        <RadioButton
            android:id="@+id/bird"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="새" />
    </RadioGroup>

    <Button
        android:id="@+id/btn"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="선택" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/dog" />

</LinearLayout>

MainActivity.java

package kr.co.company.ch5;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    Button btn;
    ImageView img;
    RadioButton dog, cat, bird;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = findViewById(R.id.btn);
        img = findViewById(R.id.image);
        dog = findViewById(R.id.dog);
        cat = findViewById(R.id.cat);
        bird = findViewById(R.id.bird);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (dog.isChecked()) img.setImageResource(R.drawable.dog);
                else if (cat.isChecked()) img.setImageResource(R.drawable.cat);
                else if (bird.isChecked()) img.setImageResource(R.drawable.bird);
            }
        });
    }
}

결과

 

7.

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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Languages"
        android:textColor="#666666"
        android:textSize="25dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/programmer" />

    <CheckBox
        android:id="@+id/php"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onCheckBoxClicked"
        android:text="PHP"
        android:textSize="25dp" />

    <CheckBox
        android:id="@+id/java"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onCheckBoxClicked"
        android:text="JAVA"
        android:textSize="25dp" />

    <CheckBox
        android:id="@+id/c"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onCheckBoxClicked"
        android:text="C"
        android:textSize="25dp" />

    <CheckBox
        android:id="@+id/cpp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onCheckBoxClicked"
        android:text="C++"
        android:textSize="25dp" />

    <CheckBox
        android:id="@+id/python"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onCheckBoxClicked"
        android:text="PYTHON"
        android:textSize="25dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:backgroundTint="#6633CC"
            android:onClick="done"
            android:text="DONE" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:backgroundTint="#FF9900"
            android:onClick="cancel"
            android:text="CANCEL" />
    </LinearLayout>

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#666666"
        android:textSize="25dp" />

</LinearLayout>

MainActivity.java

package kr.co.company.ch5;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private CheckBox php, java, c, cpp, python;
    private CheckBox[] checkBox;
    private TextView text;
    ArrayList<String> language;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        php = (CheckBox) findViewById(R.id.php);
        java = (CheckBox) findViewById(R.id.java);
        c = (CheckBox) findViewById(R.id.c);
        cpp = (CheckBox) findViewById(R.id.cpp);
        python = (CheckBox) findViewById(R.id.python);

        checkBox = new CheckBox[]{php, java, c, cpp, python};
        text = findViewById(R.id.txt);
        language = new ArrayList<>();
    }

    public void onCheckBoxClicked(View view) {
        boolean checked = ((CheckBox) view).isChecked();
        switch (view.getId()) {
            case R.id.php:
                if (checked) language.add("PHP");
                else language.remove("PHP");
                break;
            case R.id.java:
                if (checked) language.add("JAVA");
                else language.remove("JAVA");
                break;
            case R.id.c:
                if (checked) language.add("C");
                else language.remove("C");
                break;
            case R.id.cpp:
                if (checked) language.add("C++");
                else language.remove("C++");
                break;
            case R.id.python:
                if (checked) language.add("PYTHON");
                else language.remove("PYTHON");
                break;
        }
    }

    public void done(View view) {
        if (language.size() > 0) text.setText("Languages : " + language.toString());
        else text.setText("");
    }

    public void cancel(View view) {
        language = new ArrayList<>();
        for (int i = 0; i < checkBox.length; i++) {
            checkBox[i].setChecked(false);
        }
        text.setText("");
    }
}

결과

728x90

댓글