使用按钮单击Android连续更改图像

时间:2013-06-13 07:58:50

标签: android imageview gallery slideshow

我在此感谢所有帮助我的人。

我正在尝试制作一张有10张照片的应用, 主布局上只有一个图像视图和一个按钮。

当我按下按钮时,图像变为下一个。当我再次按下按钮时,它会变为另一个按钮。

我好几天都做不到。我正在尝试这个;

Main.java;

package com.example.denemeemre;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;


public class Main extends Activity {
private ImageView hImageViewSemafor;
public Button hButton;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        hImageViewSemafor = (ImageView)findViewById(R.id.idImageViewSemafor);
        hButton = (Button) findViewById(R.id.idBtnChangeImage);
        hButton.setOnClickListener(aButtonChangeImageListener);

    }
        View.OnClickListener aButtonChangeImageListener = new OnClickListener() {

        public void onClick(View v) {
                // setImageResource will change image in ImageView
            hImageViewSemafor.setImageResource(R.drawable.r1);

        }
    };

    public void onCreate1(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        hImageViewSemafor = (ImageView)findViewById(R.id.idImageViewSemafor);
        hButton = (Button) findViewById(R.id.idBtnChangeImage);
        hButton.setOnClickListener(bButtonChangeImageListener);


    }

    View.OnClickListener bButtonChangeImageListener = new OnClickListener() {
        public void onClick(View v) {
                // setImageResource will change image in ImageView
            hImageViewSemafor.setImageResource(R.drawable.r2);

        }
    };

    View.OnClickListener cButtonChangeImageListener = new OnClickListener() {
        public void onClick(View v) {
                // setImageResource will change image in ImageView
            hImageViewSemafor.setImageResource(R.drawable.r3);

        }
    };

    View.OnClickListener dButtonChangeImageListener = new OnClickListener() {
        public void onClick(View v) {
                // setImageResource will change image in ImageView
            hImageViewSemafor.setImageResource(R.drawable.r4);

        }
    };    

    View.OnClickListener eButtonChangeImageListener = new OnClickListener() {
        public void onClick(View v) {
                // setImageResource will change image in ImageView
            hImageViewSemafor.setImageResource(R.drawable.r5);

        }
    };   

}

和main.xml;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/idImageViewSemafor"
        android:layout_width="match_parent"
        android:layout_height="342dp"
        android:layout_weight="1.04"
        android:adjustViewBounds="true"
        android:background="#66FFFFFF"
        android:maxHeight="91dip"
        android:maxWidth="47dip"
        android:padding="10dip"
        android:src="@drawable/r0" />

    <Button
        android:id="@+id/idBtnChangeImage"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Sonraki" />

</LinearLayout>

UPDATE;

现在就像这样,从第一张照片中倒退就崩溃了;

代码;

package com.galerionsekiz;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;


public class Main extends Activity {

    private ImageView hImageViewPic;
    private Button iButton, gButton;

    private int currentImage = 0;
    int[] images = { R.drawable.r1, R.drawable.r2, R.drawable.r3, R.drawable.r4, R.drawable.r5, R.drawable.r6, R.drawable.r7, R.drawable.r8, R.drawable.r9, R.drawable.r10 }; 

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        hImageViewPic = (ImageView)findViewById(R.id.idImageViewPic);
        iButton = (Button) findViewById(R.id.bIleri);
        gButton = (Button) findViewById(R.id.bGeri);
        //Just set one Click listener for the image

        iButton.setOnClickListener(iButtonChangeImageListener);
        gButton.setOnClickListener(gButtonChangeImageListener);
    }
    View.OnClickListener iButtonChangeImageListener = new OnClickListener() {

        public void onClick(View v) {
            //Increase Counter to move to next Image
            currentImage++;
            currentImage = currentImage % images.length;

            hImageViewPic.setImageResource(images[currentImage]);

        }
    };

    View.OnClickListener gButtonChangeImageListener = new OnClickListener() {

        public void onClick(View v) {
            //Increase Counter to move to next Image
            currentImage--;
            currentImage = currentImage % images.length;

            hImageViewPic.setImageResource(images[currentImage]);

        }
    };
}

主要;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="fill"
    android:orientation="vertical"
    android:weightSum="100" >

    <ImageView
        android:id="@+id/idImageViewPic"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="100"
        android:adjustViewBounds="true"
        android:background="#66FFFFFF"
        android:maxHeight="91dip"
        android:maxWidth="47dip"
        android:padding="10dip"
        android:src="@drawable/r0" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/bGeri"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Önceki" >
        </Button>

        <Button
            android:id="@+id/bIleri"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Sonraki" >
        </Button>
    </LinearLayout>

</LinearLayout>

有什么建议吗?

2 个答案:

答案 0 :(得分:4)

您不希望为每个不同的图像设置多个onClickListener。您只需要跟踪当前图像,这样当您单击按钮时它就会知道下一张图像是哪一个。

此代码应该为您提供我的意思。

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;


public class Main extends Activity {

    private ImageView hImageViewSemafor;
    private Button hButton;

    private int currentImage = 0;
    private int numImages = 10;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        hImageViewSemafor = (ImageView)findViewById(R.id.idImageViewSemafor);
        hButton = (Button) findViewById(R.id.idBtnChangeImage);
        //Just set one Click listener for the image
        hButton.setOnClickListener(aButtonChangeImageListener);

    }
    View.OnClickListener aButtonChangeImageListener = new OnClickListener() {

        public void onClick(View v) {
            //Increase Counter to move to next Image
            currentImage++;
            currentImage = currentImage % numImages

            //Set the image depending on the counter.
            switch (currentImage) {
            case 0:  hImageViewSemafor.setImageResource(R.drawable.r1);
                     break;
            case 1:  hImageViewSemafor.setImageResource(R.drawable.r2);
            break;
            case 2:  hImageViewSemafor.setImageResource(R.drawable.r3);
            break;
            case 3:  hImageViewSemafor.setImageResource(R.drawable.r4);
            break;
            case 4:  hImageViewSemafor.setImageResource(R.drawable.r5);
            break;
            case 5:  hImageViewSemafor.setImageResource(R.drawable.r6);
            break;
            case 6:  hImageViewSemafor.setImageResource(R.drawable.r7);
            break;
            case 7:  hImageViewSemafor.setImageResource(R.drawable.r8);
            break;
            case 8:  hImageViewSemafor.setImageResource(R.drawable.r9);
            break;
            case 9:  hImageViewSemafor.setImageResource(R.drawable.r10);
            break;
            default: hImageViewSemafor.setImageResource(R.drawable.r1);     
            }

        }
    };
}

希望这会有效,但我没有测试过。

更新

删除了大开关块。

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;


public class Main extends Activity {

    private ImageView hImageViewSemafor;
    private Button hButton;

    private int currentImage = 0;
    int[] images = { R.drawable.r1, R.drawable.r2, R.drawable.r3, R.drawable.r4, R.drawable.r5, R.drawable.r6, R.drawable.r7, R.drawable.r8, R.drawable.r9, R.drawable.r10 }; 

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        hImageViewSemafor = (ImageView)findViewById(R.id.idImageViewSemafor);
        hButton = (Button) findViewById(R.id.idBtnChangeImage);
        //Just set one Click listener for the image
        hButton.setOnClickListener(aButtonChangeImageListener);

    }
    View.OnClickListener aButtonChangeImageListener = new OnClickListener() {

        public void onClick(View v) {
            //Increase Counter to move to next Image
            currentImage++;
            currentImage = currentImage % images.length;

            hImageViewSemafor.setImageResource(images[currentImage]);

        }
    };
}

更新2

问题在于

currentImage--;
currentImage = currentImage % images.length;

因为,当从第一个图像前进图像时,currentImage变得小于0.并且MOD(%)不可能是负数。这将导致您的错误。 所以把它改成这个。

currentImage--;
currentImage = (currentImage + images.length) % images.length;

我们通过将图像总数添加到currentImage数字来解决它变得小于0的问题。

以下代码应为固定代码。

package com.galerionsekiz;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;


public class Main extends Activity {

    private ImageView hImageViewPic;
    private Button iButton, gButton;

    private int currentImage = 0;
    int[] images = { R.drawable.r1, R.drawable.r2, R.drawable.r3, R.drawable.r4, R.drawable.r5, R.drawable.r6, R.drawable.r7, R.drawable.r8, R.drawable.r9, R.drawable.r10 }; 

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        hImageViewPic = (ImageView)findViewById(R.id.idImageViewPic);
        iButton = (Button) findViewById(R.id.bIleri);
        gButton = (Button) findViewById(R.id.bGeri);
        //Just set one Click listener for the image

        iButton.setOnClickListener(iButtonChangeImageListener);
        gButton.setOnClickListener(gButtonChangeImageListener);
    }
    View.OnClickListener iButtonChangeImageListener = new OnClickListener() {

        public void onClick(View v) {
            //Increase Counter to move to next Image
            currentImage++;
            currentImage = currentImage % images.length;

            hImageViewPic.setImageResource(images[currentImage]);

        }
    };

    View.OnClickListener gButtonChangeImageListener = new OnClickListener() {

        public void onClick(View v) {
            //Increase Counter to move to next Image
            currentImage--;
            currentImage = (currentImage + images.length) % images.length;

            hImageViewPic.setImageResource(images[currentImage]);

        }
    };
}

答案 1 :(得分:-1)

嘿朋友们试试我的逻辑。它适用于任何数量的图像。 您的主要活动代码 -

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

public class MainActivity extends Activity {
ImageView imageView1;
Button button1;
int len=images.length-1;
static int curr=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView1=(ImageView)findViewById(R.id.imageView1);
    button1=(Button)findViewById(R.id.button1);
}

private static final int[] images=new int[]     {R.drawable.i1,R.drawable.i2,R.drawable.i3,R.drawable.i4,R.drawable.i5,R.drawable.i6};
public void onClick(View view)
{   
    if(curr<6){
    imageView1.setImageResource(images[curr]);
    }else
    {
        curr=-1;
    }
    curr++;
    }
}