我正在使用android演播室玩一个21点游戏,在运行睡眠方法时遇到了一个问题。我希望当玩家按下看台按钮时,它将执行2项任务:
代码:
stand.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//task 1
ImageView iv; // declaritaion
int i = 0;
iv = new ImageView(MainActivity.this); // make the imageview for the card i want to flip
iv.setTranslationX(400); // set all attributes
iv.setTranslationY(1000);
iv.setLayoutParams(layoutParams);
String s = r.getdHand().getQ().get(0).getImg(); // get the image name of the card I want to flip (I already have the card stored in dealer hand)
int imagekey = getResources().getIdentifier(s, "drawable", getPackageName());
iv.setImageResource(imagekey);
dealerPoints.setText(Integer.toString(r.getdHand().sum)); // change the text view that displays the dealer points
main.addView(iv); // add card
// here i want a delay of 1 second
// task 2
while (r.getdHand().sum < 17) {
ImageView add; // declariation
int index = 2;
cards c = r.d.dealNextCard(); // add card to dealer hand
r.getdHand().add(c);
add = new ImageView(MainActivity.this); // make a new image view
add.setTranslationX(400 + 190 * index);
add.setTranslationY(1000);
add.setLayoutParams(layoutParams);
s = r.getdHand().getQ().get(index).getImg(); // get the image name
imagekey = getResources().getIdentifier(s, "drawable", getPackageName());
add.setImageResource(imagekey);
main.addView(add);
dealerPoints.setText(Integer.toString(r.getdHand().sum)); // change the dealer points in the text view as before
index++;
// here i want another delay between every card creation
}
}
});发布于 2022-02-07 22:34:35
你可以用它来睡上一段时间
TimeUnit.SECONDS.sleep(1)把它放在你想要延迟的地方
https://stackoverflow.com/questions/71026186
复制相似问题