英文:
how to set handler to show imageview in android studio
问题
我想在点击按钮后延迟一段时间后显示一些视图,但它不起作用,我找不到问题,或者可能是这段代码不起作用。我将图像设置为不可见,并希望在点击按钮后的26秒后显示它,有人能帮帮我吗?
这是活动的完整代码:
@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);
}
}
这是我的 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" />
我顺便使用了 MotionLayout,它有3个约束。在第一个约束中,我将按钮设置为不可见,在第二个约束中,它将可见。点击按钮后,它进入第三个约束,视频将播放,按钮将不可见,我希望在点击按钮后的一段时间内显示一张图像和两个按钮。
英文:
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?
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
<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
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
答案1
得分: 1
要运行一个长时间延迟的任务,使用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();
}
}
英文:
To run a long-delayed job, use a Timer
instead of 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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论