9
votes

Bouton Toggle Visibilité dans l'activité Android

J'ai une activité avec deux boutons de lecture et de pause (actuellement invisibles) et une barre de recherche. Lorsque j'appuie sur le bouton de lecture, le bouton PAUSE doit devenir visible, et lorsque j'appuie sur la touche PAUSE, il devrait devenir invisible.

Comment ferais-je cela? P>

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

        }
    }


0 commentaires

6 Réponses :


0
votes

Vous pouvez définir la visibilité de votre pauseMedia à invisible avec le code ci-dessous: xxx

ou pour la définir comme visible: xxx


0 commentaires

5
votes
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 will make the layout manager ignore the dimensions of the widget. INVISIBLE will make it so the widget is not visible, but the layout manager will still treat it as if it is there.If you are familiar with CSS,
setVisibility(View.INVISIBLE) is like CSS opacity: 0
setVisibility(View.GONE) is like CSS display: none
setVisibility(View.VISIBLE) is like CSS display: block

2 commentaires

Pour clarifier, le responsable de la mise en page ignore les dimensions du widget. Il n'y a donc pas d'espace vide. En comparaison, invisible rendra le gestionnaire de présentation prendre en compte le widget. Vous verrez donc un espace vide où le widget était utilisé.


En annulation publique onclick (View v)



0
votes

Peut-être essayez peut-être quelque chose comme ceci:

Dans le contexte de cette fonction p> xxx pré>

est la vue gonflée lorsque la classe a été instanciée p>

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


0 commentaires

22
votes

J'utilise pour n'importe quelle vue: xxx


0 commentaires

0
votes

Je pense que le code suivant peut fonctionner, xxx

pour désactiver le bouton de pause, xxx

Je pense que ce code peut fonctionner pour votre scénario .


0 commentaires

2
votes

Utilisez cette logique simple pour basculer la visibilité ...

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


0 commentaires