Android:如何在按钮点击时循环显示不同的图片?

huangapple go评论89阅读模式
英文:

Android: How to cycle through different images with button click?

问题

以下是您提供的内容的翻译部分:

在我的安卓程序中,我想要在点击按钮时循环显示不同的交通灯图片。每当应用程序加载时,它始终显示红灯的图片,当我点击它时,我希望它切换到绿灯,再点击切换到黄灯。以下是我在Java文件中的代码:

package com.example.trafficsimulator;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

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

    public void stopButton(View view){
        final Button button = findViewById(R.id.button);
        ImageView image = findViewById(R.id.redLightImage);
        button.setBackgroundColor(getResources().getColor(R.color.yellowlight));
        button.setText("Go");
        image.setImageResource(R.drawable.yellowlight);
    }
}

以及XML文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/redLightImage"
        android:layout_width="217dp"
        android:layout_height="372dp"
        android:layout_marginStart="97dp"
        android:layout_marginTop="61dp"
        android:layout_marginEnd="97dp"
        android:layout_marginBottom="298dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/redlight" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="156dp"
        android:layout_marginTop="76dp"
        android:layout_marginEnd="167dp"
        android:layout_marginBottom="173dp"
        android:background="#BA1C1C"
        android:onClick="stopButton"
        android:text="@string/stop"
        android:textColor="@android:color/white"
        android:textColorHint="#FFFFFF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/redLightImage"
        app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
英文:

In my android program I am wanting to cycle through different images of traffic lights on the click of a button. Whenever the app loads it starts off with an image of a red light, and when I click it I want it to change the green light, and the another click to a yellow light. This is what I have in my Java file

package com.example.trafficsimulator;

import androidx.appcompat.app.AppCompatActivity;

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


public class MainActivity extends AppCompatActivity {

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

    }


    public void stopButton(View view){
        final Button button = findViewById(R.id.button);
        ImageView image = findViewById(R.id.redLightImage);
        button.setBackgroundColor(getResources().getColor(R.color.yellowlight));

        button.setText(&quot;Go&quot;);
        image.setImageResource(R.drawable.yellowlight);


    }

and XML file

   &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot;&gt;

    &lt;ImageView
        android:id=&quot;@+id/redLightImage&quot;
        android:layout_width=&quot;217dp&quot;
        android:layout_height=&quot;372dp&quot;
        android:layout_marginStart=&quot;97dp&quot;
        android:layout_marginTop=&quot;61dp&quot;
        android:layout_marginEnd=&quot;97dp&quot;
        android:layout_marginBottom=&quot;298dp&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        app:srcCompat=&quot;@drawable/redlight&quot; /&gt;

    &lt;Button
        android:id=&quot;@+id/button&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;156dp&quot;
        android:layout_marginTop=&quot;76dp&quot;
        android:layout_marginEnd=&quot;167dp&quot;
        android:layout_marginBottom=&quot;173dp&quot;
        android:background=&quot;#BA1C1C&quot;
        android:onClick=&quot;stopButton&quot;
        android:text=&quot;@string/stop&quot;
        android:textColor=&quot;@android:color/white&quot;
        android:textColorHint=&quot;#FFFFFF&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintHorizontal_bias=&quot;1.0&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toBottomOf=&quot;@+id/redLightImage&quot;
        app:layout_constraintVertical_bias=&quot;0.0&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

答案1

得分: 1

您可以像这样实现您的目标

**主活动**

        Button buttonChangeLight;
        ImageView imageLight;
        int counter = 0;
        
        public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            buttonChangeLight = findViewById(R.id.button);
            imageLight = findViewById(R.id.redLightImage);
    
    
            //更改灯光
            changeLight();
        }
    
        //更改您的灯光
        public void changeLight(){
            buttonChangeLight.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(counter == 2){
               counter = 0;
               image.setImageResource(R.drawable.redLight);
            }else if(counter == 1){
               counter++;
               image.setImageResource(R.drawable.yellowLight);
            }else if(counter == 2){
               counter++;
               image.setImageResource(R.drawable.greenLight);
            }
          }
        });
      }
英文:

You can achieve your goal like this

Main Activity

    Button buttonChangeLight;
    ImageView imageLight;
    int counter = 0;
    
    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        buttonChangeLight = findViewById(R.id.button);
        imageLight = findViewById(R.id.redLightImage);


        //to change lights
        changeLight();
    }

    //change you light
    public void changeLight(){
        buttonChangeLight.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(counter == 2){
           counter = 0;
           image.setImageResource(R.drawable.redLight);
        }else if(counter == 1){
           counter++;
           image.setImageResource(R.drawable.yellowLight);
        }else if(counter == 2){
           counter++;
           image.setImageResource(R.drawable.greenLight);
        }
      }
    });
  }

答案2

得分: 0

你好(我无法理解你的问题,但如果这个方法不起作用,我已经上传了解决方案,如果不起作用,请告诉我,我会尝试给出更好的答案)。您可以在这里执行以下操作:

最初,将所有图像(红色、黄色、绿色)同时添加到应用程序的 XML 文件中,放在同一个位置,然后将两个图像的 alpha 设置为 '0',属性除外红色图像部分,然后在每次单击按钮时,将当前图像的 alpha 设置为零,第二个图像的 alpha 设置为零... 依此类推,
因此代码将类似于此:

boolean isred = true; //用于红色图像
boolean isyellow = false; //用于黄色图像
boolean isgreen = false; // 用于绿色图像

//按钮点击监听器
public void ChangeImage(View view){

  if(isred == true){
        RedImageView.animate().alpha(0).setDuration(500); //500 毫秒将会
        //产生动画效果
       YellowImageView.animate().alpha(1).setDuration(500);
       isyellow = true;
       isred = false;
   }
   
  if(isyellow == true){
      YellowImageView.animate().alpha(0).setDuration(500);
      GreenImageView.animate().alpha(1).setDuration(500);
      isyellow = false;
      isgreen = true;
   }
   
  if(isgreen == true){
     GreenImageView.animate().alpha(0).setDuration(500);
     isgreen = false;
     // 现在,如果您想继续相同的操作,那么您必须添加以下代码
     RedImageView.animate().alpha(1).setDuration(500);
     isred = true;
  }
}
英文:

Hello (I was unable understand your problem but I uploaded the solution if this doesnt work let me know I'll try give better answer) You can do one thing here,

Initially add all the images(red,yellow,green) into the applications xml file all of them at the same place and then set the alpha of two of the images and as '0' from the attributes except the red image section then on every click of the button you set the alpha of the current image as zero and alpha of the second image as zero... so on and so forth
so the code will be something like this

boolean isred =true; //for red image
boolean isyellow = false; //for yellow image
boolean isgreen = false; // for green image
//button onclick listener
public void ChangeImage(View view){

  if(isred == true){
        RedImageView.animate().alpha(0).setduration(500); //500 milli-seconds it will 
        //give an animation effect
       YellowImageView.animate().alpha(1).setduration(500);
       isyellow = true;
       isred = false;
   }
if(isyellow == true){

    YellowImageView.animate().alpha(0).setduration(500);
    GreenImageView.animate().alpha(1).setduration(500);
    isyellow = false;
    isgreen = true;
 }
if(isgreen == true){
     GreenImageView.animate().alpha(0).setduration(500);
     isgreen = false;
     // now if you want to continue the same thing then you must add the below code
     RedImageView.animate().alpha(1).setduration(500);
     isred = true;
}
   
}

huangapple
  • 本文由 发表于 2020年9月14日 02:58:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63874465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定