Android后退计时器按钮

时间:2013-01-01 19:37:49

标签: android text timer

我想制作一个60秒的简单计时器。当我点击按钮时,我的计时器开始。当time = 0时,将文本设置为某个字符串。帮帮我怎样才能在android里面做Activity:

package com.example.gameprocess;


public class GameProcess extends Activity {

private static int rez = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gameprocess);


 final TextView Textkolvo = (TextView)findViewById(R.id.kolvo);
 final TextView Texttime = (TextView)findViewById(R.id.texttime);
 final Button Butklik = (Button)findViewById(R.id.klik);

当我按下此按钮时,我的时间必须开始......

 Butklik.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //Textkolvo.setText(String.valueOf(rez));
    }
});
}
}

2 个答案:

答案 0 :(得分:0)

为秒定义成员变量

int secondsLeft = 0;

并将其放入onCreate

final Handler handler = new Handler();

final Runnable updateTimeTask = new Runnable() {
    public void run() {
        secondsLeft--;
        textTime.setText(String.valueOf(secondsLeft));
        if (secondsLeft == 0) {
            textKolvo.setText("Game over.");
        } else {
            handler.postDelayed(this, 1000);
        }
    }
};

butKlik.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        if (secondsLeft == 0) {
            secondsLeft = 60;
            textTime.setText(String.valueOf(secondsLeft));
            handler.postDelayed(updateTimeTask, 1000);
        }
    }
});

答案 1 :(得分:-1)

你可以做什么使得一个扩展BroadCastRecevier并能够接收这些意图的TimerReceiver接收:

清单文件中将其定义为接收以下意图

<receiver android:name=".TimerReceiver" exported="false" >

    <intent-filter >
        <action android:name="com.yourpackagename.starttimer"/>
        <action android:name="com.yourpackagename.endtimer"/>
        <action android:name="com.yourpackagename.restarttimer"/>
    </intent-filter>

</receiver>

在应用程序中编写BroadcastReceiver所需的内容

public class TimerReceiver extends BroadcastReceiver {

public static final String START_TIMER  =  "com.tcs.starttimer";
public static final String END_SESSION  =  "com.tcs.endsession";
public static final String RESTART_SESSION="com.tcs.restartsession";

private int sessionTimer=10*60*1000; //your time of counter in miliseconds
private int tickTime=1000; //tick time to decrease the counter in one milisecond


@Override
public void onReceive(Context contextdata, Intent intent) {

String actionString=intent.getAction();

    if(actionString.equals(TimerReceiver .START_TIMER)){

     timerStart();
}
    } else if(actionString.equals(TimerReceiver .END_SESSION)){

     //write what you want on ending the session

    }

 }


  void timerStart(){

     try{
         cTimer.cancel();
     }catch(Exception e){

     }

     cTimer=new CountDownTimer(sessionTimer,tickTime) {

    @Override
    public void onTick(long millisUntilFinished) {
    counter++;

    }

      @Override
      public void onFinish() {
        //Log.d("counter","finish.counter: "+counter);
        int timeout=(sessionTimer/1000)-(tickTime/1000);
        if(counter==timeout){
        Toast.makeText(context, "Session Timeout",Toast.LENGTH_LONG).show();            
        context.sendBroadcast(new Intent(END_SESSION));
        }

      }
       }.start();
    }

 }

如果需要,您可以再添加一个操作,例如重新启动计时器。

点击按钮,您的活动需要做什么:

    context.sendBroadcast(new Intent((TimerReceiver .START_TIMER));

在我的情况下,我正在增加你可以反向做。喜欢为我工作,希望它会帮助你。