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