0

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

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

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

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

主.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>

更新;

现在是这样,从第一张照片倒退;

编码;

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>

有什么建议么?

4

2 回答 2

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]);

        }
    };
}
于 2013-06-13T08:38:58.247 回答
-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++;
    }
}
于 2014-11-18T09:37:32.713 回答