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

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

by starfish22 2022. 9. 11.
728x90

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

 

1. 3번 뷰그룹

 

2. 3번 View

 

3. 4번 setContentView()

 

4. 1번 findViewById()

 

5. 3번 match_parent

 

6. 2번 px

 

7. 3번 #FFFFFF

 

8. 2번 setText()

 

9.

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="회전 각도"
            android:textSize="30dp"
            android:textColor="#80000000"
            android:padding="30dp"
            android:layout_marginTop="100dp"/>
        <EditText
            android:id="@+id/edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"
            android:inputType="number"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="회전"
            android:backgroundTint="#00FF00"
            android:textColor="#FFFFFF"
            android:layout_marginLeft="180dp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/img"
            android:src="@drawable/image"
            android:scaleX="0.5"
            android:scaleY="0.5"/>
    </LinearLayout>

</LinearLayout>

MainActivity.java

package kr.co.company.ch3_9;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    Button button;
    EditText edittext;
    ImageView imageView;

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

        button=(Button) findViewById(R.id.btn);
        edittext=(EditText) findViewById(R.id.edit);
        imageView=(ImageView) findViewById(R.id.img);

        button.setOnClickListener(e->{
            imageView.setRotation(Float.parseFloat(edittext.getText().toString()));
        });
    }
}

결과

 

10.

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

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/image"/>
    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:inputType="number"/>
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="GUESS"/>
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="40dp"
        android:gravity="center"/>

</LinearLayout>

MainActivity.java

package kr.co.company.numberguess;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    Button button;
    EditText editText;
    TextView textView;

    int random=(int)(Math.random()*100)+1;

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

        button=(Button) findViewById(R.id.btn);
        editText=(EditText) findViewById(R.id.edit);
        textView=(TextView) findViewById(R.id.text);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(random>Integer.parseInt(editText.getText().toString())) textView.setText("Low!!");
                else if(random<Integer.parseInt(editText.getText().toString())) textView.setText("High!!");
                else textView.setText("Great!!!!");
            }
        });
    }
}

결과

728x90

댓글