我有活动A,B和C.
在活动B中,我有随机数生成器和Collections.shuffle()。 这给了我下一个活动C中的随机数。
然后我使用按钮返回活动B并重新启动整个过程, 但是它给了我C中的重复项,因为整个Collections.shuffle()重启了我猜。
当我从B到C时,我需要得到没有重复的随机数,反复这样做。
活动B:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Button a = (Button) findViewById(R.id.button1); // Here the R.id.button1 is the button from you design
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
ArrayList<Integer> randomNumber = new ArrayList<Integer>();
for (int i = 1; i <= 17; ++i) randomNumber.add(i);
Collections.shuffle(randomNumber);
int random = randomNumber.get(0);
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("Value",random);
startActivity(intent);
}
});
Button b = (Button) findViewById(R.id.button2); // Here the R.id.button1 is the button from you design
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
ArrayList<Integer> randomNumber = new ArrayList<Integer>();
for (int i = 18; i <= 35; ++i) randomNumber.add(i);
Collections.shuffle(randomNumber);
int random = randomNumber.get(0);
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("Value",random);
startActivity(intent);
}
});
Button c = (Button) findViewById(R.id.button3); // Here the R.id.button1 is the button from you design
c.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
ArrayList<Integer> randomNumber = new ArrayList<Integer>();
for (int i = 36; i <= 50; ++i) randomNumber.add(i);
Collections.shuffle(randomNumber);
int random = randomNumber.get(0);
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("Value",random);
startActivity(intent);
}
});
ACTIVITY C:
public class Main2Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main2);
Button back = (Button) findViewById(R.id.buttonback); // Here the R.id.button1 is the button from you design
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
odstevalnik.cancel();
Intent i = new Intent(Main2Activity.this, MainActivity.class);
startActivity(i);
}
});
Bundle bundle = getIntent().getExtras();
int random = bundle.getInt("Value");
TextView text = (TextView) findViewById(R.id.textView1);
if (random==1) {
text.setText("blabla");
image.setImageResource(R.drawable.img2);
stopnja.setImageResource(R.drawable.stopnja1);
Toast.makeText(getApplicationContext(), "blabla",
Toast.LENGTH_LONG).show();
}
.....
答案 0 :(得分:1)
在活动的OnCreate
中创建随机数的集合B.发送以这种方式不会重新开始收集。从列表中发送号码
int random = randomNumber.get(i++);