Why do I have to press the button one more time?

Asked 2 years ago, Updated 2 years ago, 139 views

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

This is my layout code... Nothing else matters.

Put the image button at the top to move on...

I put in 3 images to flip...

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();
    }

    }

This is my Java file...

Whenever I press the next button, I change it to visible one by one...

The function is working well...

Strangely, only the button to turn it over to the next picture is skipped.

I don't know what to say.

If you press the first and next picture, nothing will happen... Every time you press it from the second time, it moves on to the next picture...

It's not a big problem, but can you tell me why it works from the second press?

android delay button listview application

2022-09-22 21:30

1 Answers

There are currently three states that are controlled by imageIndex.For convenience, I'll call it 0, 1, 2. And the default state of the UI in XML is 0. When you press the first button, the first if statement of changeImage1() is performed because the imageIndex is 0. 0 > 0 > 1 > 2 ... because this part represents the status of 0 > 0 > 0 > 2 ... It changes in order.

For a simple modification, try setting the initial value of imageIndex to 1 instead of 0.


2022-09-22 21:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.