与asynctask的老虎机游戏

时间:2017-11-20 12:22:40

标签: java android android-asynctask

我想用asynctask构建一个简单的老虎机游戏。我有以下代码。

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/tvNum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8"
            android:textSize="146sp"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="20dp"
            />

        <TextView
            android:id="@+id/tvNum2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8"
            android:textSize="146sp"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="50dp"
            />

        <TextView
            android:id="@+id/tvNum3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8"
            android:textSize="146sp"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="50dp"
            />
    </LinearLayout>

    <Button
        android:id="@+id/btStartStop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start/Stop"
        android:layout_gravity="center_horizontal"/>



</LinearLayout>

MainActivity

package com.android.iqbal.slotmachine;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btStartStop;
    private TextView tvNum;
    private TextView tvNum2;
    private TextView tvNum3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.tvNum = (TextView) findViewById(R.id.tvNum);
        this.tvNum2 = (TextView) findViewById(R.id.tvNum2);
        this.tvNum3 = (TextView) findViewById(R.id.tvNum3);
        this.btStartStop = (Button) findViewById(R.id.btStartStop);

        this.btStartStop.setOnClickListener(this);
    }

    RandomTask randomTask, randomTask2, randomTask3;
    @Override
    public void onClick(View view) {


        if (randomTask == null || randomTask.getStatus() == AsyncTask.Status.FINISHED){
            randomTask = new RandomTask(this.tvNum);
            randomTask.execute();
        } else {
            randomTask.cancel(true);
        }
        if (randomTask2 == null || randomTask2.getStatus() == AsyncTask.Status.FINISHED){
            randomTask2 = new RandomTask(this.tvNum2);
            randomTask2.execute();
        } else {
            randomTask2.cancel(true);
        }
        if (randomTask3 == null || randomTask3.getStatus() == AsyncTask.Status.FINISHED){
            randomTask3 = new RandomTask(this.tvNum3);
            randomTask3.execute();
        } else {
            randomTask3.cancel(true);
        }

    }
}

RandomTask

package com.android.iqbal.slotmachine;
import android.os.AsyncTask;
import android.widget.TextView;


public class RandomTask extends AsyncTask<Void, Integer, Integer> {

private TextView tvNum;
private int mNum;

public RandomTask(TextView tvNum){
    this.tvNum = tvNum;
}
@Override
protected Integer doInBackground(Void... voids) {
    try {
        while (true) {
            mNum = (int) (Math.random() * 10);
            publishProgress(mNum);
            Thread.sleep(100);
        }
    }catch (Exception ex){}
    return mNum;
}


@Override
protected void onProgressUpdate(Integer... values) {
    super.onProgressUpdate(values);
    mNum = values[0];
    this.tvNum.setText(mNum + "");
}

@Override
protected void onPostExecute(Integer integer) {
    super.onPostExecute(integer);
    this.tvNum.setText(mNum + "");
}
}

我打算做的是在点击开始时将所有三个textview随机。每次我停止时,一次停止一个文本视图。但问题是这个代码,只有第一个文本视图被洗牌。如何将所有三个textview洗牌并逐个停止?

1 个答案:

答案 0 :(得分:0)

好的,这是答案,我在代码中做了一些更改,很容易理解:

public class RandomTask extends AsyncTask<Void, Integer, Integer> {

private TextView tvNumReference;
private int mNum;
private boolean isRunning;

public RandomTask(TextView tvNum){
    this.tvNumReference = tvNum;
    isRunning = true;
}
@Override
protected Integer doInBackground(Void... voids) {
    try {
        while (isRunning) {
            mNum = (int) (Math.random() * 10);
            publishProgress(mNum);
            Thread.sleep(100);
        }
    }catch (Exception ex){}
    return mNum;
}


 public boolean isRunning(){
      return isRunning;
    }
public void stop(){
    isRunning = false;
}


@Override
protected void onProgressUpdate(Integer... values) {
    super.onProgressUpdate(values);
    mNum = values[0];
    this.tvNumReference.setText(mNum + "");
}

@Override
protected void onPostExecute(Integer integer) {
    super.onPostExecute(integer);
    this.tvNumReference.setText(mNum + "");
}
}

这是你的onclick方法

@Override
    public void onClick(View view) {

        if (randomTask == null || !randomTask.isRunning()){
            randomTask = new RandomTask(this.tvNum);
            randomTask.execute();
        } else {
            randomTask.stop();
        }

        if (randomTask2 == null || !randomTask2.isRunning()){
            randomTask2 = new RandomTask(this.tvNum2);
            randomTask2.execute();
        } else {
            randomTask2.stop();
        }

        if (randomTask3 == null || !randomTask3.isRunning()){
            randomTask3 = new RandomTask(this.tvNum3);
            randomTask3.execute();
        } else {
            randomTask3.stop();
        }

    }
}