모바일/android

[안드로이드프로그래밍6판] 4장 연습 문제 7, 8

hatch 2021. 10. 16. 01:38
728x90
반응형

정답지아닙니다.

블로그 주인이 쓴 답지이므로

오타, 오류 있을 수 있습니다.

(댓글로 알려주시면 정정하겠습니다!)

 

연습 문제 7
AVD 화면

<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/CEnable"
        android:text="Enabled 속성"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <CheckBox
        android:id="@+id/CClickable"
        android:text="Clickable 속성"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <CheckBox
        android:id="@+id/Cturn"
        android:text="45도 회전"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/Btn"
        android:text="Button"
        android:enabled="false"
        android:clickable="false"
        android:textAllCaps="false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

<java 코드>

package com.cookandroid.pr4_7;

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 cEnable, cClickable, cTurn;
    Button btn;

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

        cEnable = findViewById(R.id.CEnable);
        cClickable = findViewById(R.id.CClickable);
        cTurn = findViewById(R.id.Cturn);
        btn = findViewById(R.id.Btn);

        cEnable.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {btn.setEnabled(true);
                } else {btn.setEnabled(false);}
            }
        });
        cClickable.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {btn.setClickable(true);
                } else {btn.setClickable(false);}
            }
        });
        cTurn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {btn.setRotation(45);
                } else {btn.setRotation(0);}
            }
        });
    }
}

 

 

연습 문제 8

 

AVD 화면

 

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

    <EditText
        android:id="@+id/Edit"
        android:hint="여기에 작성하시오."
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

<java 코드>

package com.cookandroid.pr4_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 edit;
    String msg;

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

        edit = findViewById(R.id.Edit);
        edit.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                msg = edit.getText().toString();
                Toast.makeText(getApplicationContext(),msg, Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }
}

▶ 복습 개념

- EditText에서 입력 받은 문자를 Toast로 출력하려면?

String 변수 따로 선언, EditText.getText().toString();