본문 바로가기
DEVELOPMENT

[Android Studio] Chapter 4 연습문제

by 200% 2021. 6. 9.

[연습문제]

4-6

 

4-7

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


    <CheckBox
        android:id="@+id/enableCheckBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Enable 속성" />

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

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

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:enabled="false"
        android:layout_marginTop="130dp"/>


</LinearLayout>

 

MainActivity.java

더보기
package com.cookandroid.a4_6;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class MainActivity extends AppCompatActivity {
    CheckBox check1, check2, check3;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("연습문제 4-7");

        check1 = (CheckBox) findViewById(R.id.enableCheckBox);
        check2 = (CheckBox) findViewById(R.id.clickableCheckBox);
        check3 = (CheckBox) findViewById(R.id.rotationCheckBox);
        btn = (Button) findViewById(R.id.btn);

        check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                btn.setEnabled(check1.isChecked());
            }
        });

        check2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                btn.setClickable(check2.isChecked());
            }
        });

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

    }
}

 

4-8

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


    <EditText
        android:id="@+id/et1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="text"
        android:text="입력하세요" />
</LinearLayout>

 

MainActivity.java

더보기
package com.cookandroid.a4_8;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText et1;
    String wrote;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("연습문제 4-8");

        et1 =(EditText) findViewById(R.id.et1);


        et1.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                wrote=et1.getText().toString();
                Toast.makeText(getApplicationContext(), wrote, Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }
}

 

 

4-9

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


    <Button
        android:id="@+id/btnRotate"
        android:layout_width="146dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="100dp"
        android:drawableLeft="@drawable/drawable_btn_img"
        android:drawableRight="@drawable/drawable_btn_img"
        android:text="회전하기"/>

    <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/cat2" />


</LinearLayout>

 

drawable_btn_imag.xml(이미지 크기조절용. res/drawable에 뒀음)

더보기
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/cat"
        android:width="15dp"
        android:height="15dp"
        />

</layer-list>

 

MainActivity.java

더보기
package com.cookandroid.a4_9;

import androidx.appcompat.app.AppCompatActivity;

import android.media.Image;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    Button rotate;
    ImageView img;
    int degree=0;

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

        rotate= (Button) findViewById(R.id.btnRotate);
        img = (ImageView) findViewById(R.id.img);

        rotate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                degree += 10;
                img.setRotation(degree);
            }
        });
    }
}