본문 바로가기

App/Android Studio

Do it 안드앱 프로그래밍 도전3

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=".MainActivity">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="330dp">

<ImageView
android:id="@+id/imageViewF2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/cat"
android:visibility="visible"/>

<ImageView
android:id="@+id/imageViewT2"
android:layout_width="wrap_content"
android:layout_height="330dp"
app:srcCompat="@drawable/dog"
android:visibility="invisible"/>
</FrameLayout>

<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="onButton1Clicked"/>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="330dp">

<ImageView
android:id="@+id/imageViewF1"
android:layout_width="wrap_content"
android:layout_height="301dp"
app:srcCompat="@drawable/dog"
android:visibility="visible"/>

<ImageView
android:id="@+id/imageViewT1"
android:layout_width="wrap_content"
android:layout_height="301dp"
app:srcCompat="@drawable/cat"
android:visibility="invisible"/>

</FrameLayout>

</LinearLayout>

 

JAVA코드

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
ImageView imageViewT1;
ImageView imageViewT2;
ImageView imageViewF1;
ImageView imageViewF2;

int imageIndex=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageViewT1=findViewById(R.id.imageViewT1);
imageViewT2=findViewById(R.id.imageViewT2);
imageViewF1=findViewById(R.id.imageViewF1);
imageViewF2=findViewById(R.id.imageViewF2);
}
public void onButton1Clicked(View v){
changeImage();
}

private void changeImage(){
if(imageIndex==0){
imageViewF1.setVisibility(View.VISIBLE);
imageViewT1.setVisibility(View.INVISIBLE);
imageViewF2.setVisibility(View.VISIBLE);
imageViewT2.setVisibility(View.INVISIBLE);

imageIndex=1;
}else if(imageIndex==1){
imageViewF1.setVisibility(View.INVISIBLE);
imageViewT1.setVisibility(View.VISIBLE);
imageViewF2.setVisibility(View.INVISIBLE);
imageViewT2.setVisibility(View.VISIBLE);

imageIndex=0;
}
}

}

 

 

'App > Android Studio' 카테고리의 다른 글