2014-03-30 29 views
0
public class Tab1 extends Activity { 
int count = 0; 
int count1 = 0; 
int count3; 
private static final int MILLIS_PER_SECOND = 1000; 
    private static final int SECONDS_TO_COUNTDOWN = 11; 

    private Handler mHandler = new Handler(); 
    private CountDownTimer timer; 

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

    final TextView t11 = (TextView) findViewById(R.id.t11); 
    final TextView t12 = (TextView) findViewById(R.id.t12); 

    t11.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      t11.setText("10 seconds"); 
      count = 0; 
      mHandler.postDelayed(mUpdateTimeTask, 3000); 

     } 
    }); 

    Button butb1 =(Button)findViewById(R.id.butb1); 
    butb1.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 

      t12.setText("" + ++count); 

     } 
    }); 
} 

@Override 
protected void onDestroy() { 

    super.onDestroy(); } 

private void showTimer(int countdownMillis) { 

     timer = new CountDownTimer(countdownMillis, MILLIS_PER_SECOND) { 
     @Override 
     public void onTick(long millisUntilFinished) { 
      final TextView t11 = (TextView) findViewById(R.id.t11); 
     t11.setText(" " + 
     millisUntilFinished/MILLIS_PER_SECOND + "SECONDS"); 
     } 
     @Override 
     public void onFinish() { 

      Button butb1 =(Button)findViewById(R.id.butb1); 
      butb1.setVisibility(View.INVISIBLE); 
      timer.cancel(); 
      final TextView t11 = (TextView) findViewById(R.id.t11); 
      if (count>count1) 
      { 
       t11.setTextSize(60); 
       t11.setText("GREAT!"); 

      } 
      else { 

       t11.setText("BAD!");} 

     } 
     }.start(); 
    } 

private Runnable mUpdateTimeTask = new Runnable() { 
    public void run() { 
     Button butb1 =(Button)findViewById(R.id.butb1); 
     butb1.setVisibility(View.VISIBLE); 
     try { 
       showTimer(SECONDS_TO_COUNTDOWN * MILLIS_PER_SECOND); 
      } catch (NumberFormatException e) { 

      } 
    } 
    }; 


} 

这是我的代码。我需要做的是记住'count'的值并将其传递到'count1',以便在第二次计时器开始时比较count和count1,因此如果count> count1,则发布“Great”,如果不是 - “坏”。我不知道如何做到这一点,我猜在这种情况下需要第三个int,这就是为什么我加了count3,但我不知道如何使用它。比较两个整数的困难。如何记住结果?

回答

0

OK,我找到了解决办法

if (count>count1) 
     { 
     count1 = count;   
    t21.setText("GREAT!"); }