Android:我遇到了Back Button的问题

时间:2015-06-15 13:41:41

标签: android

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

 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)

如果我们正确地缩进它,我们就会得到这个;

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();
                    }
                }
            }

您好像正在检查v == btn_back声明中是否v == btn_next。您需要关闭v == btn_next语句,如下所示:

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();
                }
            }
        }