简体   繁体   English

如何设置处理程序以在 android 工作室中显示 imageview

[英]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.

相关问题 如何在android studio中设置ImageView的拖放? - how to set drag and drop for ImageView in android studio? 如何在 ImageView Android Studio 中设置过滤器属性 - How to set filter property in ImageView Android studio 如何在 android 工作室中的 imageview 上添加半透明覆盖并正确显示 - how to add semi transparent overlay on imageview in android studio and show it properlay Android Studio:如何将外部文件(来自相机的照片)设置为imageview - Android Studio : How to set an external file(photo from the camera) to an imageview Android Studio和Firebase:全屏显示ImageView - Android Studio & Firebase: Show ImageView on Full Screen 将 ImageView 设置为 Android Studio 中的另一个活动 - Set ImageView to another activity in Android Studio 如何在Android Studio中初始化处理程序? - How to initialize a handler in Android Studio? 使用随机模式时,每个ImageView如何显示不同的图片? Android Studio - How can each ImageView show a different picture when using random? Android Studio 在 Imageview 上动态设置处理程序 Postdelayed - Set Handler Postdelayed on Imageview dynamically 如何在 Android Studio 中运行 JavaFX 并制作 ImageView - How to run JavaFX in Android Studio and make ImageView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM