2015-01-05 39 views
-4

基本上,用户安装应用程序,第一次进入,并单击按钮[例如] 37次 - >现在这个数字必须保存为一个新的记录,因为37大于0.第二次用户点击和他使[例如] 88次点击。现在我想要保存88并通过将它放入PopupDialog中使其可以随时查看。我从哪说起呢?如何保存一个按钮的点击次数?

这是我的代码:

public class MainActivity extends ActionBarActivity { 

private TextView txtCount, textViewTimer; 
private Button btnCount; 
int count = 0; 
boolean[] timerProcessing = { false }; 
boolean[] timerStarts = { false }; 
private MyCount timer; 

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

    TextView digital= (TextView) findViewById(R.id.textView2); 
    txtCount = (TextView) findViewById(R.id.textView1); 
    txtCount.setText(String.valueOf(count)); 
    btnCount = (Button) findViewById(R.id.button1); 
    Button btnRestart = (Button) findViewById(R.id.button2); 
    Button btnRecord = (Button)findViewById(R.id.button_record); 
    textViewTimer = (TextView) findViewById(R.id.textView2); 

    timer = new MyCount(10000, 1); 

    btnCount.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View arg0) { 
      // start timer once when button first click 
      if (!timerStarts[0]) { 
       timer.start(); 
       timerStarts[0] = true; 
       timerProcessing[0] = true; 
      } 

      if (timerProcessing[0]) { 
       count++; 
       txtCount.setText(String.valueOf(count)); 
      } 
     } 
    }); 
    btnRestart.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      timerStarts[0] = false; 
      timerProcessing[0] = true; 
      count = 0; 
      txtCount.setText(String.valueOf(count)); 
      timer.cancel(); 
      textViewTimer.setText("10:000"); 

      if (btnCount.isPressed()) { 
       timer.start(); 

      } 

     } 

    }); 
    btnRestart.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      Vibrator vb = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
      vb.vibrate(1); 
      return false; 
     } 
    }); 

} 

public class MyCount extends CountDownTimer { 
    public MyCount(long millisInFuture, long countDownInterval) { 
     super(millisInFuture, countDownInterval); 
    } 

    @Override 
    public void onFinish() { 
     textViewTimer.setText("0:000"); 
     timerProcessing[0] = false; 

    } 

    @Override 
    public void onTick(long millisUntilFinished) { 
     textViewTimer.setText("" + millisUntilFinished/1000 + ":" 
       + millisUntilFinished % 1000); 

    } 

} 

我不知道如何做到这一点,你可以帮我吗?

+2

我对你的问题感到困惑。你在谈论哪部分代码?你想要保存什么,你想怎么做。似乎你可以在一些类级变量中存储点击次数,除非你希望将这个计数保存在状态变化上,比如方向变化或暂停/恢复,在这种情况下,你需要将变量保存在一个包中,或者一些其他的持久性机制,如sharedPreferences。 – user1023110

+0

** txtCount **显示您使用按钮执行的点击次数,每次用户分数高于以前的时间时,我想保存此编号。 – Pier

回答

0

看起来您使用类实例变量count来保存您感兴趣的计数。 由于您的活动已处置并重新创建,因此您需要将此变量保存在onSaveInstanceState生命周期事件处理函数中并将其恢复到onCreate中。这将是这个样子:

@Override 
public void onSaveInstanceState(Bundle savedInstanceState) { 
    // Save the current game state 
    savedInstanceState.putInt("SavedCount", count); 

    //Cll the superclass 
    super.onSaveInstanceState(savedInstanceState); 
} 

恢复该变量在onCreate方法如下:

@Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); // Always call the superclass first 
    // Check whether we're recreating a previously destroyed instance 
    if (savedInstanceState != null) { 
    // Restore value of members from saved state 
     count = savedInstanceState.getInt("SavedCount"); 
    } else { 
     count = 0;// Probably initialize members with default values for a new instance 
    } 
    //...rest of oncreate method 
    } 
+0

因此,我可以在某处保存用户点击次数? – Pier

+0

是的,如果您已定义要在您感兴趣的onClick事件中进行更新的计数,这两种方法将保存您在生命周期事件之间为变量指定的值(如方向更改时或用户返回时主屏幕等) – user1023110

+0

但我不明白我如何才能永久保存记录,直到另一个完成。我不知道如何解释它... 因此:用户安装应用程序并单击按钮[例如] 37次>现在这个数字必须保存为新记录,因为37大于0.第二个用户点击按钮时,他会让[例如] 88次点击。现在我想要保存88并通过将它放入PopupDialog中使其可以随时查看。 – Pier

相关问题