简体   繁体   English

Java如何显示然后隐藏(GONE)视图-Android

[英]Java How to Show then Hide (GONE) View-Android

I am developing an APP in Android Studio (cause I'm not a Java pro-just learning...) I am not sure if my code is not working-which it looks like it is not to me. 我正在Android Studio中开发一个APP(因为我不是Java专业人士……)我不确定我的代码是否不起作用-好像对我来说不行。 Can anyone lend a hand? 谁能伸出援手?

I have a music file that when it plays I won't to show the progress bar-THEN hide it with visibility (GONE). 我有一个音乐文件,播放时我不会显示进度条,直到隐藏可见为止(消失)。 Everything works just fine, except I cant' get the VISIBILITY to change if (!mediaPlayer.isPlaying)... 一切正常,除非((mediaPlayer.isPlaying)...我无法获得更改的可见性...

Basically after the 10-seconds of audio plays the progress bar needs to change the view back to gone. 基本上,在播放10秒钟的音频后,进度条需要将视图变回消失。

   final ProgressBar Pbar;
   Pbar = (ProgressBar) findViewById(R.id.progressBar);

   final MediaPlayer mediaPlayer = MediaPlayer.create(this, 
   R.raw.soundttt);
    Button playsound = (Button) this.findViewById(R.id.play_sound_ten);
    playsound.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            mediaPlayer.start();
            if (mediaPlayer.isPlaying()) {
                Pbar.setVisibility(View.VISIBLE);
            }
            else if (!mediaPlayer.isPlaying()){
                Pbar.setVisibility(View.GONE);
            }
        }

    });

I have been trying for three days, have done my fair share of reading including the numerous threads on MediaPlayer. 我已经尝试了三天,做了相当多的阅读,包括MediaPlayer上的众多线程。

StackOverFlow 1 Any help would be appreciated! StackOverFlow 1任何帮助将不胜感激! Thank you in advance! 先感谢您!

else if (!mediaPlayer.isPlaying()){
    Pbar.setVisibility(View.GONE);
}

Above code will be executed immediately after media player has started playing. 上面的代码将在媒体播放器开始播放后立即执行。 This condition will be false, as a result view's visibility is not impacted. 此条件将为假,因为视图的可见性不会受到影响。

If you wan't to toggle the Visibility after 10 seconds, do it using Handler as follows: 如果您不想在10秒后切换可见性,请使用Handler如下操作:

 new Handler().postDelayed(new Runnable() {
     @Override
     public void run() {
         Pbar.setVisibility(View.GONE);
     }
 },10000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM