0

当我点击生成按钮时,我希望我的程序每 30 秒自动生成一次随机数,问题是如何在我的代码中执行此操作?

public class MainActivity extends Activity {

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

        final Random myRandom = new Random();


        Button buttonGenerate = (Button)findViewById(R.id.generateme);
        final TextView textGenerateNumber = (TextView)findViewById(R.id.generatenumber);

        buttonGenerate.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                ArrayList<Object> Arry1 = new ArrayList<Object>();
                for(int i=0;i<1;i++){
                    ArrayList<Integer> Arry = new ArrayList<Integer>();
                    for(int k=0;k<7;k++){
                        Arry.add(myRandom.nextInt(10));
                    }
                    Arry1.add(Arry);
                }
                textGenerateNumber.setText(String.valueOf(Arry1));
            }
        });  
    }
}
4

1 回答 1

0

设置一个 Timer 并为其提供您自己的 TimerTask 扩展。当你按下按钮时激活定时器,你应该很好。

http://enos.itcollege.ee/~jpoial/docs/tutorial/essential/threads/timer.html

http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html

这是一个基于您的代码的示例,可帮助您入门。请注意,它会因为活动生命周期而崩溃:

package com.example.tmp;

import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {
    Timer timeoutTimer;
    final Random myRandom = new Random();
    GenerateTask genTask = new GenerateTask();
    final ArrayList<Object> arry1 = new ArrayList<Object>();

    class GenerateTask extends TimerTask {
        boolean started = false;
        @Override
        public void run() {
            if (started) {
                System.out.println("generating");
                final TextView textGenerateNumber = (TextView)findViewById(R.id.generatenumber);
                arry1.clear();
                for(int i=0;i<1;i++){
                    ArrayList<Integer> Arry = new ArrayList<Integer>();
                    for(int k=0;k<7;k++){
                        Arry.add(myRandom.nextInt(10));
                    }
                    arry1.add(Arry);
                }
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        textGenerateNumber.setText(String.valueOf(arry1));
                    }
                });
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);        
        Button buttonGenerate = (Button)findViewById(R.id.generateme);

        buttonGenerate.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                System.out.println("click");
                if (!genTask.started) {
                    genTask.started=true;
                    timeoutTimer = new Timer();
                    timeoutTimer.scheduleAtFixedRate(genTask, 0, 30000);
                } else {
                    genTask.started=false;
                    timeoutTimer.cancel();
                }
            }
        });  
    }
}
于 2013-03-12T13:08:13.977 回答