CountDownTimer暂停

时间:2014-10-26 07:19:08

标签: android countdowntimer

我的应用中有一个倒计时器。每当我点击图像视图时,它应该暂停10秒并在10秒后自动启动。我使用下面的代码。它不工作!我怎样才能成为可能?

MainActivity.java:



package app.thesis.boogleit;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


@TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi")
public class MainActivity extends Activity {

	//VARIABLES by AL
	DBAdapter dbHelper;
	EditText words, scores, search;
	ImageView show_addWord;
	TextView score, tv2, tv3;
	//GAMEPLAY by LOVELY
	protected static final String TAG = null;
	String generatedString = " ";
	AutoCompleteTextView text;
	TextView timer;
	ImageView searchWord;
	ImageView blizzard, bomb, searchword, lineone;
	ImageButton image1, image2, image3, image4, image5, image6, image7, image8,
	image9, image10, image11, image12, image13, image14, image15, image16;
	ListView wordList;
	private CountDownTimer countDownTimer;
	private boolean timeHasStarted = false;
	
	
	
    @SuppressLint("CutPasteId") @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        dbHelper =new DBAdapter(this);
        search = (EditText) findViewById(R.id.textHere);
        words = (EditText) findViewById(R.id.edWord);
        scores = (EditText) findViewById(R.id.edScore);
        show_addWord = (ImageView) findViewById(R.id.show_addWord);
                
        show_addWord.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//buttonSound.start();
				startActivity(new Intent("app.thesis.boggleit.ADDWORD"));	
			}
		});
        
        
        blizzard = (ImageView) findViewById(R.id.blizzard);
        bomb = (ImageView) findViewById(R.id.bomb);
        searchWord = (ImageView) findViewById(R.id.searchWord);
        wordList = (ListView) findViewById(R.id.wordList);
        image1 = (ImageButton) findViewById(R.id.Button1);
		image2 = (ImageButton) findViewById(R.id.Button2);
		image3 = (ImageButton) findViewById(R.id.Button3);
		image4 = (ImageButton) findViewById(R.id.Button4);
		image5 = (ImageButton) findViewById(R.id.Button5);
		image6 = (ImageButton) findViewById(R.id.Button6);
		image7 = (ImageButton) findViewById(R.id.Button7);
		image8 = (ImageButton) findViewById(R.id.Button8);
		image9 = (ImageButton) findViewById(R.id.Button9);
		image10 = (ImageButton) findViewById(R.id.Button10);
		image11 = (ImageButton) findViewById(R.id.Button11);
		image12 = (ImageButton) findViewById(R.id.Button12);
		image13 = (ImageButton) findViewById(R.id.Button13);
		image14 = (ImageButton) findViewById(R.id.Button14);
		image15 = (ImageButton) findViewById(R.id.Button15);
		image16 = (ImageButton) findViewById(R.id.Button16);

		timer = (TextView) this.findViewById(R.id.timer);
		timer.setText("00:00:30");
		countDownTimer = new MyCountDownTimer(30000,1000);
		
		final AutoCompleteTextView text = (AutoCompleteTextView) findViewById(R.id.textHere);
		
		final ImageButton image1 = (ImageButton) findViewById(R.id.Button1);
		
	//array of images	 
		final int[] myPics = { };
		//random images 
		
		//pause time and resume time automatically
		blizzard.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if (!timeHasStarted) {
					countDownTimer.cancel();
					timeHasStarted = false;	
					   
					   }
				else {
					countDownTimer.start();
					   timeHasStarted = true;
						   }
				
			
			}
		});
		
        
		bomb.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
		//random image	
        }
		});
		
		
		//click imgbuttons> display text in txtview > start countdounttimer
		
		OnClickListener myCommoClickListner = new OnClickListener(){

			@Override
			public void onClick(View arg0) {
			    Log.i(TAG,"arg0.getId() = " + arg0.getId());
                
                if(arg0.getId()==R.drawable.a){
                    Log.i(TAG,"arg0.getId()="+arg0.getId());
                    generatedString=generatedString+"a"; //[PLACEE RESPACTIVE CHARACTEER HERE]
                    text.setText(generatedString);
                    
        			
        				if (!timeHasStarted) {
        					   countDownTimer.start();
        					   timeHasStarted = true;
        					   }
        	

			}
			
		};

		
		
		image1.setOnClickListener(myCommoClickListner);
    }
    

//Countdowntimer
    @TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") public class MyCountDownTimer extends CountDownTimer{
		public MyCountDownTimer(long millisInFuture, long countDownInterval){
			super(millisInFuture, countDownInterval);
		}

		@TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") @Override
		public void onTick(long millisUntilFinished) {
			// TODO Auto-generated method stub
			long millis = millisUntilFinished;  
            String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),  
                TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),  
                TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));  
            System.out.println(hms);  
            timer.setText(hms);  
			
		}

		@Override
		public void onFinish() {
			// TODO Auto-generated method stub
			timer.setText("Time's Up!");
		}
	}
    
    
// search word on db > display score in 2 textview    
    private int optionTxtView = 0  ;
    

public void viewWord(View view)
    {
//    	String data= dbHelper.getAllData();
  //  	Message.message(this, data);
    	score = (TextView)findViewById(R.id.yourScore);
    	tv2 = (TextView)findViewById(R.id.tv2);
        tv3 = (TextView)findViewById(R.id.tv3);
        
        searchWord = (ImageView) findViewById(R.id.searchWord);
        wordList = (ListView) findViewById(R.id.wordList);
        text = (AutoCompleteTextView) findViewById(R.id.textHere);
        wordList = (ListView) findViewById(R.id.wordList);
    	
    	String s1= search.getText().toString();
        String s2= dbHelper.getData(s1);
        //Message.message(this, s2); 
        
      
    	if(optionTxtView == 0){
            //display the score on textview1
    		score.setText(s2);
    		tv2.setText(s2);
    		optionTxtView = 1;
      }else{
            //display the score on textview2    
            tv3.setText(s2);
            optionTxtView = 0;
      }
    	
    	ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
       	wordList.setAdapter(adapter);   	
    	adapter.add(text.getText().toString());
    	adapter.notifyDataSetChanged();
        text.setText("");
    	
        
        }
}
	
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您可以轻松使用HandlerRunnable

声明如下:

private static Handler handler = new Handler();
private int totalTimeToRun = 30;//in seconds
private Runnable timerHandler = new Runnable() {
    public int runnableCount = 0;

    @Override
    public void run() {

       handler.removeCallbacks(this);
       //do what the timer is supposed to do when it runs
       if(runnableCount == 30) {
          //30 seconds are up
          //reset
          runnableCount = 0;//so we can reuse it

       } else {    
          //update the time values
          //time left in seconds = 30-runnableCount       
          handler.postDelayed(this, 1000);//every second
       }
       runnableCount++;
    }
}

每当需要暂停&#39;计时器,只需调用以下内容:

handler.removeCallbacks(timerHandler);

到&#39;恢复&#39;计时器,只需调用以下内容:

handler.post(timerHandler);

希望你明白这个想法