Android, IOS/Android

[Android Sutdio를 활용한 안드로이드 프로그래밍] 연습문제 4-7

Emil :) 2019. 9. 21. 16:56
728x90
반응형

<문제>

체크박스를 선택할 때마다 버튼의 속성이 설정되도록 프로젝트를 작성하시오

결과화면

<xml 코드>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <CheckBox
        android:id="@+id/Checkbox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enabled 속성"/>

    <CheckBox
        android:id="@+id/Checkbox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clickable 속성"/>

    <CheckBox
        android:id="@+id/Checkbox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="45도 회전"/>

    <Button
        android:id="@+id/Button"
        android:layout_marginTop="200dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:rotation="0"
        android:enabled="false"
        android:clickable="false"
        android:text="Button" />
</LinearLayout>

 

<java 코드>

package com.example.homework1;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class MainActivity extends AppCompatActivity {

    CheckBox checkBox1, checkBox2, checkBox3;
    Button Button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        checkBox1 = findViewById(R.id.Checkbox1);
        checkBox2 = findViewById(R.id.Checkbox2);
        checkBox3 = findViewById(R.id.Checkbox3);

        Button = findViewById(R.id.Button);


        checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (checkBox1.isChecked() == true){
                    Button.setEnabled(true);
                } else {
                    Button.setEnabled(false);
                }
            }
        });

        checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (checkBox2.isChecked() == true){
                    Button.setClickable(true);
                } else {
                    Button.setClickable(false);
                }
            }
        });

        checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (checkBox3.isChecked() == true){
                    Button.setRotation(45);
                } else {
                    Button.setRotation(0);
                }
            }
        });
    }
}

<결과>

 

728x90
반응형