mojo's Blog

안드로이드 프로그래밍 제 2장 연습문제 (7번) 본문

Android

안드로이드 프로그래밍 제 2장 연습문제 (7번)

_mojo_ 2021. 8. 26. 13:32

XML Code

 

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

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edit1"></EditText>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:text="@string/strBtn1"
        ></Button>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:text="@string/strBtn2"
        ></Button>

    <RadioGroup
        android:id="@+id/rGroup1"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
        <RadioButton
            android:text="오레오"
            android:id="@+id/ra1"
            android:layout_height="wrap_content"
            android:layout_width="match_parent" />
        <RadioButton
            android:text="파이"
            android:id="@+id/ra2"
            android:layout_height="wrap_content"
            android:layout_width="match_parent" />
    </RadioGroup>

    <ImageView
        android:id="@+id/img1"
        android:layout_width="300dp"
        android:layout_height="100dp"></ImageView>

</LinearLayout>

 

strings.xml Code

 

<resources>
    <string name="app_name">HelloAndroid</string>
    <string name="strBtn1">글자 나타내기</string>
    <string name="strBtn2">홈페이지 열기</string>
</resources>

 

Java Code

 

package com.example.helloandroid;

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.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText et;
    Button btn1, btn2;
    RadioGroup rg;
    RadioButton ra1,ra2;
    ImageView img;

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

        et=(EditText)findViewById(R.id.edit1);

        btn1 = (Button)findViewById(R.id.button1);
        btn1.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                String url = et.getText().toString();
                Toast.makeText(getApplicationContext(), url, Toast.LENGTH_SHORT).show();
            }
        });

        btn2 = (Button)findViewById(R.id.button2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url = et.getText().toString();
                Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(mIntent);
            }
        });

        ra1 = (RadioButton)findViewById(R.id.ra1);
        ra2 = (RadioButton)findViewById(R.id.ra2);
        img = (ImageView)findViewById(R.id.img1);

        ra1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(ra1.isChecked() == true){
                    img.setImageResource(R.drawable.oreo);
                }
            }
        });

        ra2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(ra2.isChecked() == true){
                    img.setImageResource(R.drawable.pie);
                }
            }
        });

    }
}

 

 

 

'Android' 카테고리의 다른 글

기본 위젯 활용하기  (0) 2021.08.26
기본 위젯 다뤄보기  (0) 2021.08.26
View 그리고 View 클래스의 XML 속성  (0) 2021.08.26
기본적인 애플리케이션 작성하기  (0) 2021.08.26
Android Project 생성  (0) 2021.08.26
Comments