«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Tags
more
Archives
Today
Total
관리 메뉴

ITGenerations

안드로이드 - 버튼 클릭 + 결과 클릭 = 사진보여주기 본문

프로그래밍/안드로이드특강

안드로이드 - 버튼 클릭 + 결과 클릭 = 사진보여주기

ITGenerations 2018. 1. 19. 16:59

<Youtube>



< xml>

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context="com.example.ryan.myapplication.MainActivity">


    <RadioGroup

        android:id="@+id/radiogroup1"

        android:layout_width="match_parent"

        android:layout_height="66dp">


        <RadioButton

            android:id="@+id/button1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="Cat1" />


        <RadioButton

            android:id="@+id/button2"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="Dog1" />


        <RadioButton

            android:id="@+id/button3"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="Dog2" />

    </RadioGroup>


    <Button

        android:id="@+id/button4"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:onClick="onRadioButtonClicked"

        android:text="Display Image"

        android:textAlignment="center" />


    <ImageView

        android:id="@+id/imageView"

        android:layout_width="match_parent"

        android:layout_height="203dp"

        app:srcCompat="@drawable/cat" />


</LinearLayout>


<java>

package com.example.ryan.myapplication;


import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.ImageView;

import android.widget.RadioGroup;


public class MainActivity extends AppCompatActivity {

    ImageView image;

    RadioGroup group;


    //

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        image=(ImageView)findViewById(R.id.imageView);

        group=(RadioGroup)findViewById(R.id.radiogroup1);


    }

    //


    //

    public void onRadioButtonClicked(View view) {


     //

        switch (group.getCheckedRadioButtonId()) {

            case R.id.button1:

                    image.setImageResource(R.drawable.cat);

                break;


            case R.id.button2:

                    image.setImageResource(R.drawable.dog1);

                break;


            case R.id.button3:

                    image.setImageResource(R.drawable.dog2);

                break;

        }///

    }

    ///



}







<picture>