gpt4 book ai didi

Android - 定时器功能和 alertDialog

转载 作者:行者123 更新时间:2023-11-30 03:58:18 27 4
gpt4 key购买 nike

我创建了一个带计时器的游戏,我试图让当计时器结束时,玩家将看到警告弹出窗口或任何类型的弹出窗口,上面写着“关卡完成”,你的分数是 xxx 和下一个级别的按钮.我尝试了一些但时间结束但没有弹出窗口。有什么想法吗?

时间类:工作正常。

公开课时间{

private String time;
private boolean isDone;

public Time() {
super();
isDone=false;
}

CountDownTimer count = new CountDownTimer(5000, 1000) {

public void onTick(long millisUntilFinished) {

int seconds = (int) (millisUntilFinished / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
String tempSec=Integer.toString(seconds);
if (tempSec.length()==1){
tempSec="0"+tempSec;
}
time="Time Left: " + minutes + ":"+tempSec;
}

public void onFinish() {
setDone(true);
}

}.start();

这是 Activity 类:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
requestWindowFeature(Window.FEATURE_NO_TITLE);

club=new Club();
clubView = new ClubView(this, club);
mole=new Mole();
stageView=new StageView(this);
moleView=new MoleView(this,mole);
pointsView=new PointsView(this);

time=new Time();
timerView=new TimerView(this, time);

allViews=new AllViews(this);
allViews.setViews(stageView, moleView, pointsView, timerView,clubView);

setContentView(allViews);
allViews.setOnTouchListener((View.OnTouchListener)this);

if (timerView.getTime().isDone()){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Level Complete");
builder.setMessage("your score is"+pointsView.getPoint());
AlertDialog dialog = builder.create();
dialog.show();
}

}

最佳答案

重点是计时器需要一段时间才能用完,如果计时器已完成,您只需检查一次:

if (timerView.getTime().isDone()){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Level Complete");
builder.setMessage("your score is"+pointsView.getPoint());
AlertDialog dialog = builder.create();
dialog.show();
}

更好的选择是创建某种循环,但这是禁止的!因为你会阻塞主线程。

您的下一个选择是创建某种监听器。监听器将对您的 Activity 进行某种回调以告知“我完成了”。这通常是使用接口(interface)完成的,

小例子:

public class Time {

private String time;
private boolean isDone;
private TimerCallback timerCallback;

public Time(TimerCallback t) {
this.timerCallback = t;
isDone = false;
}

public interface TimerCallback {
abstract void onTimerDone();
}

CountDownTimer count = new CountDownTimer(5000, 1000) {

public void onTick(long millisUntilFinished) {

int seconds = (int) (millisUntilFinished / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
String tempSec = Integer.toString(seconds);
if (tempSec.length() == 1) {
tempSec = "0" + tempSec;
}
time = "Time Left: " + minutes + ":" + tempSec;
}

public void onFinish() {
setDone(true);
timerCallback.onTimerDone();
}

}.start();

}

你的 Activity 看起来像这样:

public class myActivity extends Activity implements TimerCallback {
//I have now clue how your activity is named but it's just an example!

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
requestWindowFeature(Window.FEATURE_NO_TITLE);

club = new Club();
clubView = new ClubView(this, club);
mole = new Mole();
stageView = new StageView(this);
moleView = new MoleView(this, mole);
pointsView = new PointsView(this);

//Because we are implementing the TimerCallback interface "this" is a valid argument
//this can be cast to TimerCallback: "(TimerCallback) this"
time = new Time(this);
timerView = new TimerView(this, time);

allViews = new AllViews(this);
allViews.setViews(stageView, moleView, pointsView, timerView, clubView);

setContentView(allViews);
allViews.setOnTouchListener((View.OnTouchListener) this);


}

//We must add this method, because we have implemented the TimerCallback interface!
public void onTimerDone(){
//You could remove the isDone check because it is not really necessary
if (timerView.getTime().isDone()) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Level Complete");
builder.setMessage("your score is" + pointsView.getPoint());
AlertDialog dialog = builder.create();
dialog.show();
}
}

}

我在这里发布了一个非常相似的答案,但在这个问题中没有计时器,而是某种“游戏结束”事件。 How do I perform a continuous check on andorid of the returned value of another class?

还有关于监听器模式或观察者模式的 wiki 链接 http://en.wikipedia.org/wiki/Observer_pattern

罗尔夫

关于Android - 定时器功能和 alertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12986879/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com