[英]how to set handler to show imageview in android studio
I want to show some views after a delay after clicking the button but it doesn't work I can't find the problem or maybe this code not working我想在单击按钮后延迟显示一些视图,但它不起作用我找不到问题或者这段代码可能不起作用
I set the image to invisible and want to show it after 26s of clicking the button can anybody help me?我将图像设置为不可见,并希望在单击按钮 26 秒后显示它,有人可以帮助我吗?
this is a full code of activity这是完整的活动代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
setContentView(R.layout.activity_enter);
ImageView yourImage = findViewById(R.id.allRightRegistered);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
yourImage.setVisibility(View.VISIBLE);
}
}, 26000);
VideoView videoview = findViewById(R.id.videoView);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.intromovie);
videoview.setVideoURI(uri);
videoview.start();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
}
and its my xml code和我的 xml 代码
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/Enter_Btn"
android:layout_width="300dp"
android:layout_height="90dp"
android:text="Im Ready To Enter\nCoffee Melody"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
<ImageView
android:id="@+id/allRightRegistered"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:visibility="invisible"
app:srcCompat="@drawable/all_right_registered" />
I using motion layout by the way it has 3 constraints我通过它有 3 个约束的方式使用运动布局
this is my design in the first constraint, I set the button to invisible in the second one, it will be visible and after click the button it goes to the third one and the video will be play and button will be invisible and I want one image and two buttons to be displayed after some time of clicking the button这是我在第一个约束中的设计,我在第二个约束中将按钮设置为不可见,它将可见,单击按钮后它会转到第三个,视频将播放,按钮将不可见,我想要一个单击按钮一段时间后显示的图像和两个按钮
To run a long-delayed job, use a Timer
instead of Handler
:要运行长时间延迟的作业,请使用
Timer
而不是Handler
:
public class MainActivity extends AppCompatActivity {
private Button button;
private ImageView imageView1;
private Button button1;
private Button button2;
private TimerTask timerTask;
private Timer timer;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
imageView1 = findViewById(R.id.imageView1);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
imageView1.setVisibility(View.GONE);
button1.setVisibility(View.GONE);
button2.setVisibility(View.GONE);
timerTask = new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
imageView1.setVisibility(View.VISIBLE);
button1.setVisibility(View.VISIBLE);
button2.setVisibility(View.VISIBLE);
}
});
}
};
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (timer != null) {
timer.cancel();
}
timer = new Timer();
timer.schedule(timerTask, 26000);
}
});
}
@Override
protected void onStop() {
super.onStop();
timer.cancel();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.