恢复和暂停机制

时间:2014-12-21 01:46:54

标签: java android

我正在为Android应用程序工作,它被称为Button Chaser。我必须设置暂停和恢复机制。我创建了暂停机制,它工作得很好,但我在创建简历时遇到了问题。如何制作简历机制?这是我的代码:

package com.example.buttonchaser;

import java.util.Random;
import java.util.Timer;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import android.graphics.Point;
import android.view.View.OnClickListener;


public class ButtonChaserActivity extends ActionBarActivity implements OnClickListener{
private Button btn;

private TextView lblScore;
private Timer timer;
private int x, y, w, h, score;
ButtonChaserTimerTask task;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_button_chaser);
    //Inflate UI
    btn = (Button)findViewById(R.id.btn);
    lblScore = (TextView)findViewById(R.id.lblScore);

    btn.setOnClickListener(this);
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    w = size.x - btn.getWidth() - 48;
    h = size.y - btn.getHeight() - 48;
    timer = new Timer();
    task = new ButtonChaserTimerTask(this);
    timer.schedule(task, 0, 1500);

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.button_chaser, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch(item.getItemId()){
        case R.id.mnuEasy:
            newGame();
            return true;
        case R.id.mnuMed:
            medGame();
            return true;
        case R.id.mnuHard:
            hardGame();
            return true;
        case R.id.mnuNew:
        newGame();
        return true;
    case R.id.mnuQuit:
        quit();
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

public void newGame(){
    score = 0;
    lblScore.setText("Score: " + score);
    timer = new Timer();
    task = new ButtonChaserTimerTask(this);
    timer.schedule(task, 0, 1500);
}
public void medGame(){
    score = 0;
    lblScore.setText("Score: " + score);
    timer = new Timer();
    task = new ButtonChaserTimerTask(this);
    timer.schedule(task, 0, 500);
}
public void hardGame(){
    score = 0;
    lblScore.setText("Score: " + score);
    timer = new Timer();
    task = new ButtonChaserTimerTask(this);
    timer.schedule(task, 0, 250);
}

public void quit(){
    timer.cancel();
    finish();
}

public void moveButton(){
    Random generator = new Random();
    generator.setSeed(System.currentTimeMillis());
    float f = generator.nextFloat();
    x = (int)(f*w)%w;
    f = generator.nextFloat();
    y = (int)(f*h)%h;
    btn.setX(x);
    btn.setY(y);
}

public void pauseOnClick(View view) {
    Button pause = (Button) view;
    ((Button)view).setText("Resume");
        timer.cancel();}

        //((Button)view).setText("Pause");
        //moveButton();





@Override
public void onClick(View v) {
    score +=1;
    lblScore.setText("Score: " + score);

}
}

1 个答案:

答案 0 :(得分:-3)

您可以添加onPause和onResume方法:

@Override
public void onPause() {

}

@Override
public void onResume() {

}

然后将暂停代码和恢复代码放在适当的部分。