如何使用android中的按钮单击侦听器更改下一个和上一个图像?

时间:2014-10-10 04:32:26

标签: android button view

我有下一个previous button来更改图片。活动启动时,图片来自previous activity。我使用bundle对象来获取current activity上的图像。实际上有2个图像用于传递它在bundle(image_a_inner和image_a_outer)上。一个图像在第二个图像上重叠并在自定义视图上设置。现在我想当任何图像来自捆绑然后我按下一个按钮或前一个按钮然后根据位置图像将更改。例如,像A_Z字母表的图像。当我按下D图像然后它显示我的活动使用捆绑,当我按下一个按钮,然后E图像将显示或当我按下前一个按钮,然后C图像将显示。以下是我的代码。

private DrawingView mDrawingView;

Bundle extras = getIntent().getExtras();
    int imageRes1 = extras.getInt("picture1");
    int imageRes2 = extras.getInt("picture2");
    mDrawingView = (DrawingView) findViewById(R.id.drawing_view);
    mDrawingView.setShape(imageRes1, imageRes2);
    btn_next = (Button) findViewById(R.id.btn_next);
    //      btn_next.setOnClickListener(this);
            btn_next.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    initializeMP();
                    playsound.start();




                    if(imageRes1==R.drawable.img_a_inner||imageRes2==R.drawable.img_a){
                        mDrawingView.setShape(R.drawable.img_b_inner, R.drawable.img_b);
                        index++;
                    }
                    else if(imageRes1==R.drawable.img_b_inner||imageRes2==R.drawable.img_b){
                        mDrawingView.setShape(R.drawable.img_c_inner, R.drawable.img_c);
                        index++;
                    }
                    else if(imageRes1==R.drawable.img_c_inner||imageRes2==R.drawable.img_c){
                        mDrawingView.setShape(R.drawable.img_d_inner, R.drawable.img_d);
                        index++;
                    }
                    else if(imageRes1==R.drawable.img_d_inner||imageRes2==R.drawable.img_d){
                        mDrawingView.setShape(R.drawable.img_e_inner, R.drawable.img_e);
                        index++;
                    }
                }
            });


    btn_prev = (Button) findViewById(R.id.btn_prev);

    //      btn_prev.setOnClickListener(this);
            btn_prev.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    initializeMP();
                    playsound.start();


                    if(imageRes1==R.drawable.img_b_inner||imageRes2==R.drawable.img_b){
                        mDrawingView.setShape(R.drawable.img_a_inner, R.drawable.img_a);
                        index--;
                    }
                    else if(imageRes1==R.drawable.img_c_inner||imageRes2==R.drawable.img_c){
                        mDrawingView.setShape(R.drawable.img_b_inner, R.drawable.img_b);
                        index--;
                    }
                    else if(imageRes1==R.drawable.img_d_inner||imageRes2==R.drawable.img_d){
                        mDrawingView.setShape(R.drawable.img_c_inner, R.drawable.img_c);
                        index--;
                    }
            });

        }

1 个答案:

答案 0 :(得分:1)

如果您预先列出了所有图像,请将它们声明为数组。

否则,您可以使用带有getIntArray()和putIntArray()的bundle传递它们。

现在,你有这样的图像列表,

// These are can be declared as member or static variables.
int[] innerPictures = {R.drawable.image_a_inner, R.drawable.image_b_inner, ...}
int[] pictures = {R.drawable.image_a, R.drawable.image_b, ...}

int[] innerPictures = extras.getIntArray("innerPictures");
int[] pictures = extras.getIntArray("pictures");

你需要第一次显示图像索引,它也可以作为额外传递

int displayingIndex = extra.getInt("pictureIndex"); // it has to be member variable to use inside of listener

所以代码如下,



private DrawingView mDrawingView;

Bundle extras = getIntent().getExtras();

int[] innerPictures = ...
int[] pictures = ...

displayingIndex = extra.getInt("pictureIndex");

mDrawingView = (DrawingView) findViewById(R.id.drawing_view);
mDrawingView.setShape(innerPictures[displayingIndex], pictures[displayingIndex]);
btn_next = (Button) findViewById(R.id.btn_next);

            btn_next.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                 
                    initializeMP();
                    playsound.start();

                    if (displayingIndex + 1 == innerPictures.length) return;
                    displayingIndex++;
                    mDrawingView.setShape(innerPictures[displayingIndex], pictures[displayingIndex]);
                }
            });


btn_prev = (Button) findViewById(R.id.btn_prev);

            btn_prev.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    initializeMP();
                    playsound.start();

                    
                    if (displayingIndex == 0) return;
                    displayingIndex--;
                    mDrawingView.setShape(innerPictures[displayingIndex], pictures[displayingIndex]);
            });

        }




对不好的缩进感到抱歉。