본문 바로가기

JAVA/안드로이드 프로그래밍

Android Studio를 활용한 안드로이드 프로그래밍 연습문제 197~198p

MainActivity.XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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/btn1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Enabled 속성"
       />

    <CheckBox
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clickabled 속성" />
    <CheckBox
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="회전"/>

    <Button
        android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Button"
        android:layout_marginTop="110dp"

        />
    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="입력"/>
  <Button
      android:id="@+id/imgBtn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="50dp"
      android:layout_gravity="center"
      android:text="회전하기"
      android:drawableLeft="@drawable/ic_launcher_foreground"
      android:drawableRight="@drawable/ic_launcher_foreground"
      />
 <ImageView
     android:id="@+id/image"
     android:layout_width="300dp"
     android:layout_height="100dp"
     android:layout_marginTop="50dp"
     android:layout_gravity="center"
     android:src="@drawable/logo_shrink"
     />


</LinearLayout>

JAVA

package com.example.a197p;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button btn4;
    CheckBox btn1,btn2,btn3;

    EditText edit;
    String result;

    Button imgBtn;
    ImageView image;

    int rotation = 0;

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

        btn4 = (Button) findViewById(R.id.btn4);
        btn1 = (CheckBox) findViewById(R.id.btn1);
        btn2 = (CheckBox) findViewById(R.id.btn2);
        btn3 = (CheckBox) findViewById(R.id.btn3);

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

        imgBtn = (Button) findViewById(R.id.imgBtn);
        image = (ImageView) findViewById(R.id.image);


        btn1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if(isChecked)
                {
                 btn4.setEnabled(false);
                }
                else
                {
                    btn4.setEnabled(true);
                }
            }
        });
        btn2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if(isChecked)
                {
                    btn4.setClickable(true);
                }
                else
                {
                    btn4.setClickable(false);
                }
            }
        });
        btn3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if(isChecked)
                {
                    btn4.setRotation(45);
                }
                else
                {
                    btn4.setRotation(0);
                }
            }
        });
        edit.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                result=edit.getText().toString();
                Toast.makeText(getApplicationContext(),result,result.length()).show();
                return  false;
            }
        });
        imgBtn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                rotation +=10;

                image.setRotation(rotation);
            }
        });

    }
}

연습 문제 기능들 모두 묶어서 넣었습니다. 개정판이라 그런지 책 말곤 참고할 곳이 적네요