gpt4 book ai didi

android - 如何在我的程序中包含计时器?

转载 作者:行者123 更新时间:2023-11-29 02:09:26 24 4
gpt4 key购买 nike

我需要在我的应用程序中包含计时器。我刚刚浏览了站点中给出的示例。并尝试在我的程序中实现它。但问题是我不知道如何使用计时器。在我的程序中我想要在按钮上启动计时器 click.timer 必须显示在屏幕上。但是屏幕是一个 Canvas (当我们点击按钮时它加载 Canvas )带有移动的物体。当两个物体碰撞时计时器必须停止并且必须显示消息框中的值。下面给出的是我的主要 Activity 代码。

public class MyActivity extends Activity {

private static final String TAG = MyActivity.class.getSimpleName();
public static Timer myTimer;
private Button startbtn;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
startbtn=(Button)findViewById(R.id.startbtn);
startbtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

// making it full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// set our MainGamePanel as the View
setContentView(new MainGamePanel(getApplicationContext()));
Log.d(TAG, "View added");

}
});

myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
TimerMethod();
}

}, 0, 1000);

}

@Override
protected void onDestroy() {
Log.d(TAG, "Destroying...");
super.onDestroy();
}

@Override
protected void onStop() {
Log.d(TAG, "Stopping...");
super.onStop();
}
private void TimerMethod()
{
//This method is called directly by the timer
//and runs in the same thread as the timer.

//We call the method that will work with the UI
//through the runOnUiThread method.
this.runOnUiThread(Timer_Tick);
}

private Runnable Timer_Tick = new Runnable() {
public void run() {

//This method runs in the same thread as the UI.

//Do something to the UI thread here

}
};

}

我知道如何检查碰撞。我只需要计时器部分,例如启动计时器,在屏幕上显示并从另一个类(class)停止它。有人能帮我吗……请……

最佳答案

使用此代码将在一秒后重复,计时器将运行 30 秒

new CountdownTimer(30000, 1000) {

public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}

public void onFinish() {
finish();
}
}.start();

关于android - 如何在我的程序中包含计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8337589/

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