Java 随机字符串如何看起来像吃角子老虎机
在我的程序中,我只有一个按钮。并首先按下该程序将输出一个随机字符串。如果用户再次按下停止键,我的程序将随机减慢(延迟)同一台老虎机。我怎么做 我的代码Java 随机字符串如何看起来像吃角子老虎机,java,android,string,random,audio,Java,Android,String,Random,Audio,在我的程序中,我只有一个按钮。并首先按下该程序将输出一个随机字符串。如果用户再次按下停止键,我的程序将随机减慢(延迟)同一台老虎机。我怎么做 我的代码 package com.Randomsentence; import java.util.Random; import android.app.Activity; import android.content.res.Resources; import android.os.Bundle; import android.os.Handler;
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);
}
}
};
}
忽略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();
}
}
};
}
package.com.com;
导入java.util.Random;
导入android.app.Activity;
导入android.content.res.Resources;
导入android.os.Bundle;
导入android.os.Handler;
导入android.os.Message;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.randomstation.R;
公共课堂活动{
文本视图;
int随机;
公共字符串[]myStrings;
按钮bt1;
私有静态最终int消息_RANDOM=1;
专用静态最终长最小间隔=500;
专用静态最终最大长间隔=1500;
专用静态最终长步长=60;
专用长mInterval=0;
私有布尔值mStarted=false;
private Random mRandom=new Random();
私有处理程序mHandler=新处理程序(){
@凌驾
公共无效handleMessage(消息消息消息){
if(msg.what==MESSAGE\u RANDOM){
showtrandomstring();
如果(mStarted){
this.sendEmptyMessageDelayed(MESSAGE_RANDOM,mInterval);
}否则{
如果(mIntervalslow random(delay)相同的抽签没有意义。这意味着当我再次按下按钮时,字符串不会立即停止。但停止类似的抽签会变慢。你说的“抽签”是什么意思?据我从代码中所知,这与彩票无关。看起来像是我刚才回答的关于循环声音的问题中的代码!可能是“彩票”是指吃角子老虎机/扑克机?@user814154:你可以编辑你的问题让它更清晰。但我也想要声音。当第一次按下按钮声音会激活,当第二次按下按钮另一个声音会激活。我如何添加它只需将onClick中的两个祝酒词替换到你的MediaPlayer中即可。