【发布时间】:2023-03-08 14:22:01
【问题描述】:
我有活动 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);
}
});
活动 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();
}
.....
【问题讨论】:
-
你能通过例子说明你想要什么吗?当从 B 调用活动 C 并获得例如数字“6”时,您希望从任何未来的随机计数中获得“6”,对吗?
-
没错,当我从 B 反复去 C 时,我想要 6 个。