如何在Android Studio中设置处理程序以显示ImageView。

huangapple go评论80阅读模式
英文:

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(&quot;android.resource://&quot;+getPackageName()+&quot;/&quot;+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

&lt;VideoView
    android:id=&quot;@+id/videoView&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

&lt;Button
    android:id=&quot;@+id/Enter_Btn&quot;
    android:layout_width=&quot;300dp&quot;
    android:layout_height=&quot;90dp&quot;
    android:text=&quot;Im Ready To Enter\nCoffee Melody&quot;
    android:textSize=&quot;18sp&quot;
    app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintHorizontal_bias=&quot;0.497&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toTopOf=&quot;parent&quot;
    app:layout_constraintVertical_bias=&quot;0.499&quot; /&gt;

&lt;ImageView
    android:id=&quot;@+id/allRightRegistered&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_marginBottom=&quot;24dp&quot;
    app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    android:visibility=&quot;invisible&quot;
    app:srcCompat=&quot;@drawable/all_right_registered&quot; /&gt;

I using motion layout by the way
it has 3 constraints

如何在Android Studio中设置处理程序以显示ImageView。

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();
}
}

huangapple
  • 本文由 发表于 2020年7月28日 21:45:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63135592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定