单击按钮返回0索引时如何转到最大索引,然后按下按钮反转

时间:2015-06-15 10:39:45

标签: android

我在Android上创建了一个应用程序,当我选择下一个显示新图像的按钮和后退按钮显示上一个图像但按下下一个按钮时,用声音查看图像最大索引,我希望它显示从第一个图像索引开始,当按下后退按钮到最小索引时,我希望它显示从最大图像索引开始。我有问题,当我点击后退按钮转到0索引我希望它显示最终索引的图像,当我点击下一个按钮转到最终索引我希望它显示从0索引。 < strong>但是当我点击后退按钮时,没有任何事情发生我与后退按钮

的关系
 public void btn_click(View v){
     if (v == btn_next){
            if((IMAGE_IDS.length)> currentimageindex){
                try{
                imageview.setImageResource(IMAGE_IDS[currentimageindex]);
                   mp = MediaPlayer.create(this, myMusic[currentsoundindex]);
                   mp.start();
                  currentimageindex = (currentimageindex + 1) % IMAGE_IDS.length;
                  currentsoundindex = (currentsoundindex + 1) % myMusic.length;
                }
            catch(Exception e){
           Toast.makeText(getBaseContext(), e.getMessage(),  Toast.LENGTH_SHORT).show();
                }
               }
              if((IMAGE_IDS.length)== currentimageindex){
                    try{
                        imageview.setImageResource(IMAGE_IDS[currentimageindex=0]);
                           mp = MediaPlayer.create(this, myMusic[currentsoundindex=0]);
                           mp.start();
                           currentimageindex = (currentimageindex + 1) % IMAGE_IDS.length;
                           currentsoundindex = (currentsoundindex + 1) % myMusic.length;
                        }
                    catch(Exception e){
                   Toast.makeText(getBaseContext(), e.getMessage(),  Toast.LENGTH_SHORT).show();
                        }                 
              }
        if (v == btn_back) {
            if(currentimageindex!=0){
                try{
                imageview.setImageResource(IMAGE_IDS[currentimageindex]);
                   mp = MediaPlayer.create(this, myMusic[currentsoundindex]);
                   mp.start();
                      currentimageindex = (currentimageindex + IMAGE_IDS.length -1) % IMAGE_IDS.length;
                      currentsoundindex = (currentsoundindex + myMusic.length -1) % myMusic.length;
                }
            catch(Exception e){
           Toast.makeText(getBaseContext(), e.getMessage(),  Toast.LENGTH_SHORT).show();
                }
               }
            if((currentimageindex)==0){
                try{
                      imageview.setImageResource(IMAGE_IDS[currentimageindex]);
                       mp = MediaPlayer.create(this, myMusic[currentsoundindex]);
                       mp.start();                  
                      currentimageindex = (currentimageindex + IMAGE_IDS.length -1) % IMAGE_IDS.length;
                      currentsoundindex = (currentsoundindex + myMusic.length -1) % myMusic.length;
                    }
                catch(Exception e){
                   Toast.makeText(getBaseContext(), e.getMessage(),  Toast.LENGTH_SHORT).show();
                        }
                }
          }

1 个答案:

答案 0 :(得分:1)

用于后退按钮

      if((currentimageindex)==0){
               currentimageindex= IMAGE_IDS.length-1;
               currentsoundindex= myMusic.length-1;
              imageview.setImageResource(IMAGE_IDS[currentimageindex]);
               mp = MediaPlayer.create(this, myMusic[currentsoundindex]);
               mp.start();
      }

下一个按钮

      if((currentimageindex)==IMAGE_IDS.length){
               currentimageindex= 0;
               currentsoundindex= 0;
              imageview.setImageResource(IMAGE_IDS[currentimageindex]);
               mp = MediaPlayer.create(this, myMusic[currentsoundindex]);
               mp.start();
      }

希望这会有所帮助。