왜 버튼을 한번 더 눌러야 실행될까요?

조회수 1631회
<?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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="65dp"
        android:layout_marginTop="5dp"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/name_0"
            android:layout_weight="0.8">

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.5">

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <ImageButton
                android:id="@+id/button042"
                android:onClick="onButton042Clicked"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/n_arrow" />

        </LinearLayout>


    </LinearLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:layout_weight="1">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:id="@+id/mini003"
            android:src="@drawable/dt_3"
            android:visibility="invisible"
            />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:id="@+id/mini002"
            android:src="@drawable/dt_2"
            android:visibility="invisible"
            />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:id="@+id/mini001"
            android:src="@drawable/dt_1"
            android:visibility="visible"
            />

    </FrameLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="65dp"
        android:layout_marginBottom="5dp"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.5">

            <ImageButton
                android:id="@+id/button043"
                android:onClick="onButton043Clicked"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/tube_1" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.2">

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.8">

            <ImageButton
                android:id="@+id/button040"
                android:onClick="onButton040Clicked"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/back_1" />

        </LinearLayout>


    </LinearLayout>


</LinearLayout>

제 layout코드 입니다... 딴건 별로 중요한거 없고...

상단에 다음으로 넘어가는 이미지 버튼을 넣구요...

넘길 이미지 3개를 넣어놨습니다...

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ViewFlipper;
import android.widget.Button;
import android.widget.Toast;

public class DetailActivity extends AppCompatActivity {

    ImageView mini001;
    ImageView mini002;
    ImageView mini003;

    int imageIndex = 0;

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

        mini001 = (ImageView) findViewById(R.id.mini001);
        mini002 = (ImageView) findViewById(R.id.mini002);
        mini003 = (ImageView) findViewById(R.id.mini003);

    }


    public void onButton042Clicked(View v) {
        changeImage1();
    }

    private void changeImage1() {
        if (imageIndex == 0) {
            mini001.setVisibility(View.VISIBLE);
            mini002.setVisibility(View.INVISIBLE);
            mini003.setVisibility(View.INVISIBLE);

            imageIndex = 1;
        } else if (imageIndex == 1)

        {
            mini001.setVisibility(View.INVISIBLE);
            mini002.setVisibility(View.VISIBLE);
            mini003.setVisibility(View.INVISIBLE);

            imageIndex = 2;

        } else if (imageIndex == 2)

        {
            mini001.setVisibility(View.INVISIBLE);
            mini002.setVisibility(View.INVISIBLE);
            mini003.setVisibility(View.VISIBLE);

            imageIndex = 0;
        }
    }

    public void onButton043Clicked(View v) {
        Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/watch?v=7DzwSecg2zA"));
        startActivity(myIntent);
    }


    public void onButton040Clicked(View v) {
        finish();
    }

    }

이게 제 자바 파일입니다...

다음버튼을 누를때마다 하나씩 visible로 바꿔 뜨게 하고 있습니다...

뭐 기능은 잘 작동하는데...

이상하게 최초 다음사진으로 넘기는 버튼만 스킵이 되네요...

말이 좀 애매한데...

처음 다음사진 누르면 아무일 없고... 두번째부터 누를때마다 다음사진으로 넘어갑니다...

큰 문제는 안되지만 이게 왜 2번째 눌렀을때부터 동작하는지 알수 있을까요??

1 답변

  • 현재 imageIndex에 의해 제어되는 세가지의 상태가 있는데요.편의상 0, 1, 2로 부르겠습니다. 그리고 XML에 UI의 디폴트 상태는 0번입니다. 최초 버튼을 누르면, imageIndex가 0이기 때문에 changeImage1()의 첫번째 if문이 수행되는데요. 이 부분이 0번의 상태를 나타내기 때문에 0 > 0 > 1 > 2 ... 의 순으로 변하게 되는 것입니다.

    간단한 수정 방법은 imageIndex의 초기 값을 0이 아닌 1로 설정해보시기 바랍니다.

    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)