모바일/android

[안드로이드프로그래밍 6판] 2장 직접 풀어보기 2-3, 연습 문제 7

hatch 2021. 10. 15. 17:53
728x90
반응형
직접 풀어보기 2-3

정답지 아닙니다.

블로그 주인이 생각한 코드라서

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

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

 

 

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

    <Button
        android:id="@+id/site"
        android:text="네이트 홈페이지 열기"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#A6A6A6"/>
    <Button
        android:id="@+id/call"
        android:text="911 응급전화 걸기"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#008F00"/>
    <Button
        android:id="@+id/gallery"
        android:text="갤러리 열기"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FF0000"/>
    <Button
        android:id="@+id/end"
        android:text="끝내기"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFE500"/>

</LinearLayout>

<java 코드>

package com.cookandroid.myself2_3;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

        Button Site, Call, Gallery, End;

        Site = findViewById(R.id.site);
        Call = findViewById(R.id.call);
        Gallery = findViewById(R.id.gallery);
        End = findViewById(R.id.end);

        Site.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.nate.com"));
                startActivity(mIntent);
            }
        });

        Call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:/911"));
                startActivity(mIntent);
            }
        });

        Gallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/internal/images/media"));
                startActivity(mIntent);
            }
        });
        End.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}

 

 

연습 문제 7

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

    <EditText
        android:id="@+id/Site"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/BtnShow"
        android:text="글자 나타내기"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/BtnOpen"
        android:text="홈페이지 열기"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/Q"
            android:text="Q(10)"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <RadioButton
            android:id="@+id/R"
            android:text="R(11)"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </RadioGroup>

    <ImageView
        android:id="@+id/View"
        android:layout_gravity="center"
        android:layout_width="300dp"
        android:layout_height="300dp"/>
</LinearLayout>

<java 코드>

package com.cookandroid.pr2_7;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("좀 그럴듯한 앱");

        EditText site;
        Button btnShow, btnOpen;
        RadioButton q, r;
        ImageView view;

        site = findViewById(R.id.Site);
        btnShow = findViewById(R.id.BtnShow);
        btnOpen = findViewById(R.id.BtnOpen);
        q = findViewById(R.id.Q);
        r = findViewById(R.id.R);
        view = findViewById(R.id.View);

        btnShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), site.getText(), Toast.LENGTH_SHORT).show();
            }
        });
        btnOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent a = new Intent(Intent.ACTION_VIEW, Uri.parse(String.valueOf(site.getText())));
                startActivity(a);
            }
        });

        q.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b==true) {
                    view.setImageResource(R.drawable.q10);
                }
            }
        });

        r.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b==true) {
                    view.setImageResource(R.drawable.r11);
                }
            }
        });
    }
}