1

我有一个有时间限制的安卓问答游戏。我想要的是有一个选择按钮,如果您单击其中一个按钮,您将自动进入下一级班级,但如果您没有回答或单击任何按钮,您将进入另一个班级,这就是为什么游戏有时间限制。我的问题是我不知道如何设置一个时间限制,如果你没有点击任何按钮选项,它会自动将你转移到另一个班级。我试过睡觉,但发生的情况是,即使我已经点击了正确的答案,而且我在下一级课上,它会睡到我打算睡觉的课上。请帮我解决我的问题。我也尝试处理程序但没有工作

public class EasyOne extends Activity {

按钮 a、b、c;文本视图计时器;

@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);

    }
};
4

1 回答 1

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
}

}

于 2013-07-31T09:03:35.590 回答