Android - 切换按钮打开时是否可以迭代生成随机数?

时间:2016-01-26 12:30:30

标签: java android random toggle

我正在尝试在Android中创建一个应用程序,在切换按钮打开时重复生成数字。因此,当用户再次点击切换以将其关闭时,它将停止生成随机数。但我不确定它是否可能,因为我的代码由于无限循环而不断出现运行时错误。 切换按钮打开时是否可以迭代生成随机数?

MainActivity.java

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="dan.cielos.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

 </android.support.design.widget.AppBarLayout>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Number"
    android:id="@+id/random_number"
    android:textSize="80sp"
    android:layout_above="@+id/random_button"
    android:layout_centerHorizontal="true" />

<ToggleButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New ToggleButton"
    android:id="@+id/random_button"
    android:height="75dp"
    android:width="150dp"
    android:textOff="Generate"
    android:textOn="Stop"
    android:layout_marginBottom="58dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

<NumberPicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/numPick"
    android:layout_above="@id/random_number"
    android:layout_centerHorizontal="true" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

activity_random.xml

builder.append(" ")

1 个答案:

答案 0 :(得分:0)

无限循环很糟糕,因为您不希望程序在生成数字时停止。请记住 - Android需要运行其UI,以便您可以正确显示所做的更改。

相反,只要点击切换按钮,您希望每隔短时间间隔显示一个不同的随机数(假设每100毫秒?)。

以下是您如何实现这一目标:

private boolean isGeneratingRandomNum; // <-- this will prevent duplicate initiations of the "loop"
private boolean isToggleButtonOn; // <-- change this value to true / false depending on whether or not the button is on.

private void displayRandomNumber() {
    // if the toggle button is off, stop
    // if the loop iteration has already started, stop
    if (!isToggleButtonOn || isGeneratingRandomNum)
        return;

    // indicate that the loop iteration has begun
    isGeneratingRandomNum = true;

    // your code for setting a random number
    double ran = Math.random() * (max -min) + min;
    ran = Math.round(ran);
    rn.setText(Double.toString(Math.floor(ran)));

   // set up the next iteration to occur in 100 ms on the ui thread.
   postDelayed(new Runnable() {
       @Override
       public void run() {
           // indicate that the loop iteration is done, meaning we are ready to perform another iteration.
           isGeneratingRandomNum = false;

           // call the method again.
           displayRandomNumber();
       }
   }, 100);
}

// call this method from your toggleButton onClickListener
private void onToggleButtonChange() {
    isToggleButtonOn = !isToggleButtonOn;

    if (isToggleButtonOn)
       displayRandomNumber();
}