[英]How can i code a time limit in my android game
我有一個有時間限制的Android問答游戲。 我想要的是有一個選擇按鈕,如果您單擊其中一個按鈕,您將自動進入下一課。但是,如果您未回答或單擊任何一個按鈕,則將進入另一個班級,這就是為什么游戲有時間限制。 我的問題是我不知道如何設定一個時限,如果您沒有單擊任何按鈕選擇,該時限將自動將您轉移或轉移到另一個班級。 我試過睡覺,但是發生的事情是,即使我已經單擊了正確的答案,並且在下一個級別的課程中,我也會進入我打算睡覺的課程。 請幫助我解決我的問題。 我也嘗試處理程序,但沒有工作
public class EasyOne extends Activity {
按鈕a,b,c; TextView計時器;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.easyone);
a = (Button) findViewById(R.id.btn_ea1);
b = (Button) findViewById(R.id.btn_eb1);
c = (Button) findViewById(R.id.btn_ec1);
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"CORRECT!",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),EasyTwo.class);
startActivity(intent);
}
});
}
private Runnable task = new Runnable() {
public void run() {
Handler handler = new Handler();
handler.postDelayed(task, 5000);
Intent intent = new Intent(getApplicationContext(),TimesUp.class);
startActivity(intent);
}
};
您應該使用處理程序,但是要取消超時,必須在單擊偵聽器代碼中從處理程序中刪除延遲的消息。
public class EasyOne extends Activity {
static private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 123) {
((EasyOne) msg.obj).onTimeout();
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.easyone);
a = (Button) findViewById(R.id.btn_ea1);
b = (Button) findViewById(R.id.btn_eb1);
c = (Button) findViewById(R.id.btn_ec1);
Message msg = mHandler.obtainMessage(123,this);
mHandler.sendMessageDelayed(msg,5000);
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"CORRECT!",
Toast.LENGTH_SHORT).show();
mHandler.removeMessages(123,this);
Intent intent = new Intent(getApplicationContext(),EasyTwo.class);
startActivity(intent);
}
});
}
private void onTimeout() {
//your code
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.