2011-06-26 101 views
0

在我的程序中,我只有1個按鈕。首先按程序會輸出一個隨機字符串。如果用戶再次按下它停止,我的程序將隨機(延遲)相同的老虎機。我該怎麼做?如何隨機字符串看起來像老虎機

我的代碼

package com.Randomsentence; 

import java.util.Random; 
import android.app.Activity; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class Randomsentence extends Activity { 

    boolean showRandom = false; 
    TextView txt; 
    int time = 30; 
    int random; 
    public String[] myString; 
    Button bt1; 
    boolean check = false; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     txt = (TextView) findViewById(R.id.txt); 
     bt1 = (Button) findViewById(R.id.bt1); 
     Medaiplayer mp = new Medaiplayer(); 
     Mediaplayer mp2 = new Mediaplayer(); 
     mp = MediaPlayer.create(getApplicationContext(), R.raw.AudioFile1); 
     mp2 = MediaPlayer.create(getApplicationContext(), R.raw.AudioFile2); 
     bt1.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       showRandom = !showRandom; 
       t = new Thread() { 

        public void run() { 
         try { 
          while (showRandom) { 
           mp.start(); 
           mp2.reset(); 
           mp2.prepare(); 
           sleep(1000); 
           handler.sendMessage(handler.obtainMessage()); 
          } 
          mp.reset(); 
          mp.prepare(); 
          mp2.start(); 

         } catch (Exception ex) { 
          ex.printStackTrace(); 
         } 
        } 
       }; 
       t.start(); 
      } 
     }); 
    } 
    // our handler 
    Handler handler = new Handler() { 

     public void handleMessage(Message msg) {//display each item in a single line 
      { 
       Random rgenerator = new Random(); 
       Resources res = getResources(); 
       myString = res.getStringArray(R.array.myArray); 
       String q = myString[rgenerator.nextInt(myString.length)]; 
       txt.setText(q); 

      } 
     } 
    }; 
} 
+2

'慢隨機(延遲)相同lottery'沒有意義。 – Atreys

+0

這意味着當我再次按下按鈕時,字符串不會立即停止。但它會減緩停止類似的電影 – user814154

+2

你是什麼意思的「彩票」?據我從代碼可以看出,這與彩票無關。 –

回答

0

忽略MediaPlayer的一部分,我想應該是這樣的:

package com.Randomsentence; 

import java.util.Random; 
import android.app.Activity; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.Randomsentence.R; 

public class Randomsentence extends Activity { 
    TextView txt; 
    int random; 
    public String[] myStrings; 
    Button bt1; 

    private static final int MESSAGE_RANDOM = 1; 
    private static final long MIN_INTERVAL = 500; 
    private static final long MAX_INTERVAL = 1500; 
    private static final long STEP = 60; 

    private long mInterval = 0; 
    private boolean mStarted = false; 

    private Random mRandom = new Random(); 

    private Handler mHandler = new Handler() { 

     @Override 
     public void handleMessage (Message msg) { 
      if(msg.what == MESSAGE_RANDOM) { 
       showRandomString(); 
       if(mStarted) { 
        this.sendEmptyMessageDelayed(MESSAGE_RANDOM, mInterval); 
       } else { 
        if(mInterval <= MAX_INTERVAL) { 
         this.sendEmptyMessageDelayed(MESSAGE_RANDOM, mInterval); 
         mInterval += STEP; 
        } else { 
         this.removeMessages(MESSAGE_RANDOM); 
         Toast.makeText(Randomsentence.this, "Stopped!", Toast.LENGTH_SHORT).show(); 
        } 
       } 
      } 
     } 
    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     txt = (TextView) findViewById(R.id.txt); 
     bt1 = (Button) findViewById(R.id.bt1); 
     bt1.setOnClickListener(mBt1OnClick); 
     myStrings = getResources().getStringArray(R.array.myArray); 
    } 

    private void showRandomString() { 
     final int index = mRandom.nextInt(myStrings.length - 1); 
     txt.setText(myStrings[index]); 
    } 

    private OnClickListener mBt1OnClick = new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      if(!mStarted) { 
       // Start 
       Toast.makeText(Randomsentence.this, "Started!", Toast.LENGTH_SHORT).show(); 
       mStarted = true; 
       mInterval = MIN_INTERVAL; 
       mHandler.removeMessages(MESSAGE_RANDOM); 
       mHandler.sendEmptyMessage(MESSAGE_RANDOM); 
      } else { 
       // Stop 
       mStarted = false; 
       Toast.makeText(Randomsentence.this, "Stoping...", Toast.LENGTH_SHORT).show(); 
      } 
     } 

    }; 

} 
+0

但我也想要聲音。當第一個按鈕聲音將激活,並且當第二個按鈕時,另一個聲音將激活。我可以如何添加它 – user814154

+0

只需將onClick中的2個Toast替換爲MediaPlayer的東西即可。 –

+0

我已經編輯。可以嗎? – user814154