在android活动中切换按钮可见性

时间:2013-07-12 21:07:19

标签: android button playback visible

我有一个带有两个播放和暂停按钮(当前不可见)和一个搜索栏的活动。当我按下播放按钮时,暂停按钮应该可见,当我按下暂停按钮时,它应该变为不可见。

我该怎么做?

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;


    public class MainActivity extends Activity implements Runnable, OnClickListener, OnSeekBarChangeListener{
        private SeekBar seekBar;
        private Button startMedia;
        private Button pauseMedia;
        private MediaPlayer mp;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);             

            AudioControl();         

        }

        public void AudioControl(){
            seekBar = (SeekBar) findViewById(R.id.seekBar1);
            startMedia = (Button) findViewById(R.id.button1);
            pauseMedia = (Button) findViewById(R.id.button2);
            seekBar.setOnSeekBarChangeListener(this);
            startMedia.setOnClickListener(this);
            pauseMedia.setOnClickListener(this); 
        }



        public void run() {
            int currentPosition= 0;
            int total = mp.getDuration();
            while (mp!=null && currentPosition<total) {
                try {
                    Thread.sleep(1000);
                    currentPosition= mp.getCurrentPosition();
                } catch (InterruptedException e) {
                    return;
                } catch (Exception e) {
                    return;
                }            
                seekBar.setProgress(currentPosition);
            }
        }

        public void onClick(View v) {
            if (v.equals(startMedia)) {
                if (mp != null && mp.isPlaying()) return;
                if(seekBar.getProgress() > 0) {
                    mp.start();
                    return;
                }
                mp = MediaPlayer.create(MainActivity.this, R.raw.lone);
                mp.start();                     
                seekBar.setProgress(0);
                seekBar.setMax(mp.getDuration());
                new Thread(this).start();
            }

            if (v.equals(pauseMedia) && mp!=null) {
                mp.pause();
            }       

        }

        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            if(fromUser) mp.seekTo(progress);

        }
    }

我不知道我应该把吹码放在哪里。请帮帮我!

6 个答案:

答案 0 :(得分:21)

我用于任何视图:

 public void toggleView(View view){
 if(view.getVisibility()==View.GONE)
   view.setVisibility(View.VISIBLE);
 else if(view.getVisibility()==View.VISIBLE)
   view.setVisibility(View.GONE);
  }

答案 1 :(得分:3)

startMedia = (Button) findViewById(R.id.button1);
pauseMedia = (Button) findViewById(R.id.button2);

//Onclick Event ...

//Logic
if (flag === 'start') {
startMedia.setVisibility(View.VISIBLE);
pauseMedia .setVisibility(View.INVISIBLE);
} else {
startMedia.setVisibility(View.INVISIBLE);
pauseMedia .setVisibility(View.VISIBLE);
}

GONE将使布局管理器忽略小部件的尺寸。 INVISIBLE将使得窗口小部件不可见,但布局管理器仍会将其视为存在。

如果您熟悉CSS,

  • setVisibility(View.INVISIBLE)就像CSS不透明度:0
  • setVisibility(View.GONE)就像CSS display:none
  • setVisibility(View.VISIBLE)就像CSS display:block

答案 2 :(得分:0)

您可以使用以下代码将pauseMedia的可见性设置为不可见:

pauseMedia.setVisibility(View.INVISIBLE);

或将其设置为可见:

pauseMedia.setVisiblity(View.VISIBLE);

答案 3 :(得分:0)

也许尝试这样的事情:

在此功能的上下文中

controls

是在实例化类时膨胀的视图

private void toggleControls(){

    //private variable to store whether or not the view is showing
    if(controlsAreShowing){
       if(controls != null) {
           controls.setVisibility(View.INVISIBLE);
           controlsAreShowing = false;
       }
    }else{
        controls.setVisibility(View.VISIBLE);
        controlsAreShowing = true;
    }
}

答案 4 :(得分:0)

我认为以下代码可能有效,

-regex

用于禁用暂停按钮,

play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Play", Toast.LENGTH_SHORT).show();
            mediaPlayer.start();
            pause.setEnabled(true);
            play.setEnabled(false);
        }
    });

我认为此代码可能适用于您的方案。

答案 5 :(得分:0)

使用此简单逻辑切换可见性...

 if (rvLocation.getVisibility()==View.VISIBLE)
        rvLocation.setVisibility(View.GONE);
 else
        rvLocation.setVisibility(View.VISIBLE);