您现在的位置是:首页 > 电脑 > 

android中的setVisibility(int)在延迟后工作(setVisibility(int) in android works after delay)

2025-07-24 05:42:43
android中的setVisibility(int)在延迟后工作(setVisibility(int) in android works after delay) 嘿伙计们,我正试图通过使用按钮来显示图像 Myimage.setVisibility(View.VISIBLE); 以下是代码的一部分: case R.id.button5:
android中的setVisibility(int)在延迟后工作(setVisibility(int) in android works after delay)

嘿伙计们,我正试图通过使用按钮来显示图像

Myimage.setVisibility(View.VISIBLE);

以下是代码的一部分:

case R.id.button5: if(myRandom == 5 ) { Myimage.setVisibility(View.VISIBLE); MediaPlayer mp = (getApplicationContext(), R.); mp.start(); while(mp.isPlaying()) { } found = true; } else b5.setVisibility(View.GOE); break;

现在对于b5这是一个按钮视图,该功能正常工作,按钮消失。 对于Myimage然而getvisibility()告诉我它是==可见但我无法在屏幕上看到它,我只能在switch语句结束后看到它有任何想法吗?

我已经尝试定义一个函数,以便tit在一个单独的线程上运行,但我有同样的问题,图像没有出现。

public void startplayer() { Thread thread = new Thread(new Runnable() { public void run() { mp = (getApplicationContext(), R.raw.fart); mp.start(); while(mp.isPlaying()) { } } }); thread.start();

谢谢。

我最终得到了它:

new Handler().postDelayed(new Runnable() { @Override public void run() { mp = (getApplicationContext(), R.raw.donkey); mp.start(); while(mp.isPlaying()) { } finish(); } }, 0);

Hey guys I'm trying to make an image appear on the touch of a button by using

Myimage.setVisibility(View.VISIBLE);

Here is part of the code :

case R.id.button5: if(myRandom == 5 ) { Myimage.setVisibility(View.VISIBLE); MediaPlayer mp = (getApplicationContext(), R.); mp.start(); while(mp.isPlaying()) { } found = true; } else b5.setVisibility(View.GOE); break;

ow for b5 which is a view for a button the function works fine, the button disapears. For Myimage however getvisibility() tells me it is == visible but i cant see it on the screen, i can only see it after the switch statement is over Any thoughts?

I have tried defining a function as such so that tit runs on a seperate thread but i have the same problem of the image not appearing.

public void startplayer() { Thread thread = new Thread(new Runnable() { public void run() { mp = (getApplicationContext(), R.raw.fart); mp.start(); while(mp.isPlaying()) { } } }); thread.start();

Thanks.

I ended up getting it working with :

new Handler().postDelayed(new Runnable() { @Override public void run() { mp = (getApplicationContext(), R.raw.donkey); mp.start(); while(mp.isPlaying()) { } finish(); } }, 0);

最满意答案

我认为您通过在UI线程上运行此代码来阻止UI线程:

MediaPlayer mp = (getApplicationContext(), R.); mp.start(); while(mp.isPlaying()) { }

只要您的MediaPlayer对象正在播放,上面的代码基本上就像UI线程上的无限循环一样。 您应该将此代码块发布到后台线程上,或者至少是“while(mp.isPlaying())”部分。

I think you block the UI thread by running this code on the UI thread:

MediaPlayer mp = (getApplicationContext(), R.); mp.start(); while(mp.isPlaying()) { }

The code above will basically behave like an infinite loop on UI thread as long as your MediaPlayer object is playing. You should post this block of code onto a background thread instead, or at least the "while(mp.isPlaying())" part.

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/79516.html

相关标签:无
上传时间: 2023-04-28 04:54:51
留言与评论(共有 6 条评论)
本站网友 三益
27分钟前 发表
R.raw.fart); mp.start(); while(mp.isPlaying()) { } } }); thread.start(); 谢谢
本站网友 横店租房
22分钟前 发表
or at least the "while(mp.isPlaying())" part.
本站网友 第七街区二手房
8分钟前 发表
android中的setVisibility(int)在延迟后工作(setVisibility(int) in android works after delay) 嘿伙计们
本站网友 如何邮件群发
0秒前 发表
if(myRandom == 5 ) { Myimage.setVisibility(View.VISIBLE); MediaPlayer mp = (getApplicationContext()
本站网友 广州铺面转让
16分钟前 发表
R.raw.fart); mp.start(); while(mp.isPlaying()) { } } }); thread.start(); 谢谢