简体   繁体   English

出现五个随机数后如何让我的屏幕变黑?

[英]How to make my screen go blank after I have five random numbers appear?

I'm new to coding and need all the help I can get.我是编码新手,需要我能得到的所有帮助。 I'm creating an app that displays a random number every 472ms, five times and then will clear the screen after all five numbers have appeared.我正在创建一个应用程序,每 472 毫秒显示一个随机数,五次,然后在所有五个数字出现后清除屏幕。 When you press the screen, it'll repeat that action.当你按下屏幕时,它会重复那个动作。 I've got the numbers to appear randomly and on time but I don't know how to clear the screen after the numbers have displayed each time.我已经让数字随机和准时出现,但我不知道如何在每次显示数字后清除屏幕。

This is what I mean:这就是我的意思:

Press screen -> a random appears every 472ms until five numbers have been displayed -> screen goes blank -> press screen -> order repeats按屏幕 -> 每 472 毫秒随机出现一次,直到显示五个数字 -> 屏幕变黑 -> 按屏幕 -> 订单重复

How would I make the screen go blank after those five numbers appear?出现这五个数字后,如何让屏幕变黑?

My XML:我的 XML:

<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="0dp"
android:layout_height="0dp"
android:clickable="true"
android:onClick="perform_action"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_marginBottom="75dp"
        android:gravity="center"
        android:textSize="400sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent" />

    </RelativeLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>

And my Java:还有我的 Java:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;


public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void perform_action(View view) {
    final List<Integer> randomNumbers = new ArrayList<>();
    Observable.intervalRange(0, 5, 0, 472, TimeUnit.MILLISECONDS)
            .map(aLong -> {
                int previous = randomNumbers.size() > 0 ? randomNumbers.get(
                        randomNumbers.size() - 1) : -1;
                int randomNumber = generateRandomNumber();
     while (randomNumber == previous) randomNumber = generateRandomNumber();
                randomNumbers.add(randomNumber);
                return randomNumber;
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(integer -> {
                TextView tv = (TextView) findViewById(R.id.textView1);
                tv.setText(String.valueOf(integer));
            });
}

private int generateRandomNumber() {
        Random r = new Random();
        int minNumber = 1;
        int maxNumber = 9;
        return r.nextInt((maxNumber - minNumber) + 1) + minNumber;
}
    }

You pass a Consumer to onNext in .subscribe, also add one to onComplete:你在 .subscribe 中将一个Consumer传递给 onNext,同时向 onComplete 添加一个:

.subscribe(integer -> {
    TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setText(String.valueOf(integer));
}, error -> {}, () -> {
    TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setText("");

} }

This would happen directly after the fifth one appeared.这将在第五个出现后直接发生。 As a workaround you can change the intervalRange to 6 times.作为解决方法,您可以将 intervalRange 更改为 6 次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让我的图像以随机位置显示? - How can I make my image appear in a random position? 如何使随机生成的对象出现在屏幕上? - How do I make Random generating objects come on my Screen? 让我的圈子随机消失,并在随机秒后再次出现 - Make my circles randomly disappear and after random seconds again appear 如何在Java中获得要打印的五个随机数而不重复的列表? - How do I get a list of five random numbers to be printed without duplicates in Java? 如何使EditText具有一定的最大宽度,但不离开屏幕? - How do I make an EditText have a certain maximum width, but not go off the screen? 如何使通知全天随机出现? - How do i make a notification appear at random times throughout the day? 如何在一行中显示前五个数字,在另一行中显示后五个数字? - How do I display the first five numbers on a single line and the second five numbers on another line? 如何添加片段,以便我的应用程序不会在屏幕上显示为空白 - How to add Fragment so that my app don't appear blank on screen 如何使鼠标按下时屏幕出现在屏幕上? - How to make the screen appear with mousePressed stay on the screen? 如何使我的程序每五秒钟重复一次声音? - How do I make my program repeat the sound every five seconds?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM