Android更改播放按钮停止和停止播放按钮

时间:2014-05-19 19:13:31

标签: java android xml imageview

我正在使用此代码,以便当我按下播放按钮时,它将变为停止按钮。当我按下它时显示停止按钮时如何将其更改回播放按钮?

 final ImageView Play_button = (ImageView)findViewById(R.id.playbutton);



            Play_button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

              Play_button.setImageResource(R.drawable.stopicon);

             }
            });

3 个答案:

答案 0 :(得分:2)

在这种情况下您需要使用if ... else条件,我还建议您使用boolean变量来检查它是否正在播放(如果您正在使用媒体播放器,那么你也可以使用媒体播放器类的isPlaying()方法。但为了方便起见,我会建议采用以下技术。

final ImageView Play_button = (ImageView)findViewById(R.id.playbutton);
boolean isPlaying = false;


        Play_button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
          if(isPlaying){

               //You can add you code to change the icon to stop mode
               //and finally set the flag to false as it has stopped now
               isPlaying = false;

          }else {
               //Add you code here to change the icon back to play mode
               //and finally set the flag to true as it will be playing now.
               isPlaying = true;
          }
         }
        });

答案 1 :(得分:2)

只需使用这样的标志:

final ImageView Play_button = (ImageView)findViewById(R.id.playbutton);
boolean isPlayIcon = true;

        Play_button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
          if(isPlayIcon){
              Play_button.setImageResource(R.drawable.stopicon);
              isPlayIcon = false;
          }esle{
            Play_button.setImageResource(R.drawable.playicon);
            isPlayIcon = true;
          }

         }
        });

答案 2 :(得分:1)

这是一件非常简单的事情。将状态存储在某处:

boolean isPlay = true;

final ImageView playButton = (ImageView) findViewById(R.id.playbutton);
playButton.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        if(isPlay)
            playButton.setImageResource(R.drawable.stopicon);
        else
            playButton.setImageResource(R.drawable.playicon);
        isPlay = !isPlay;
    }

});
相关问题