我的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);
}
};
答案 0 :(得分:0)
您应该使用处理程序但是为了取消超时,您必须从点击侦听器代码中删除处理程序中的延迟消息。
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
}
}