如何使Button按一天只能点击一次

时间:2014-06-30 09:13:50

标签: android button click

我有一个按钮,客户可以通过该按钮领取他的每日奖励。我希望每天只点击一次该按钮。我怎么能这样做?

我不知道如何实现此功能

我知道我可以将它设置为View.gone但我希望它能在第二天再次显示。点击按钮可以获得一天的时间

点击我的按钮,我该怎么办?

@Override
public void onClick(View v) {
    super.onClick(v);
    switch (v.getId()) {
    case R.id.creditClaimBT: {

        break;
    }
    }
}

帮助将不胜感激。

2 个答案:

答案 0 :(得分:6)

单击按钮后,将事件的日期写入SharedPreferences。如果有人单击该按钮,请检查当前日期是否已保存在SharedPreferences中。完成。

答案 1 :(得分:2)

我认为最好的方法是使用像ZerO这样的sharedPreferences。例如,每次用户打开按钮所在的活动时,请检查sharedPrefs,例如,保存日期。单击按钮后,保存当前日期。每次打开活动并且保存的日期等于当前日期时,不要单击按钮(可以使用View.GONE或setEnabled(false)执行此操作):

  private boolean firstTimeUsed = false;
  private String firstTimeUsedKey="FIRST_TIME";

  private String sharedPreferencesKey = "MY_PREF";
  private String buttonClickedKey = "BUTTON_CLICKED;
  private SharedPreferences mPrefs;
  private long savedDate=0;

初始化这些以在onCreate()中设置全局变量:

  mPrefs = getSharedPreferences(sharedPreferencesKey, Context.MODE_PRIVATE);
  savedDate = mPrefs.getLong(buttonClickedKey,0);
  firstTimeUsed = mPrefs.getBoolean(firstTimeUsedKey,true);//default is true if no value is saved
  checkPrefs();

  mButton.setOnClickListener(new OnClickListener(){

     @Override
     public void OnClick(View v){

           //things You want to do, after that:

          saveClickedTime();
     }
  });

做你的方法来检查时间:

  private void checkPrefs(){


      if(firstTimeUsed==false){
      if(savedDate>0){

      //create two instances of Calendar and set minute,hour,second and millis
      //to 1, to be sure that the date differs only from day,month and year

      Calendar currentCal = Calendar.getInstance();
      currentCal.set(Calendar.MINUTE,1);
      currentCal.set(Calendar.HOUR,1);
      currentCal.set(Calendar.SECOND,1);
      currentCal.set(Calendar.MILLISECOND,1);

      Calendar savedCal = Calendar.getInstance();
      savedCal.setTimeInMillis(savedDate); //set the time in millis from saved in sharedPrefs
      savedCal.set(Calendar.MINUTE,1);
      savedCal.set(Calendar.HOUR,1);
      savedCal.set(Calendar.SECOND,1);
      savedCal.set(Calendar.MILLISECOND,1);

      if(currentCal.getTime().after(savedCal.getTime()){

        mButton.setVisibility(View.VISIBLE);
      }else if(currentCal.getTime().equals(savedCal.getTime()){

          mButton.setVisibility(View.GONE);
     }

   }
 }else{

     //just set the button visible if app is used the first time

      mButton.setVisibility(View.VISIBLE); 

  }

}

每次按钮可用并点击时,请将日期保存回sharedPrefs:

       private void saveClickedTime(){

       Editor mEditor = mPrefs.edit();
       Calendar cal = Calendar.getInstance(); 
       long millis = cal.getTimeInMillis();
       mEditor.putLong(buttonClickedKey,millis);
       mEditor.putBoolean(firstTimeUsedKey,false); //the button is clicked first time, so set the boolean to false.
       mEditor.commit();

       //hide the button after clicked
       mButton.setVisibility(View.GONE);

      }

这只是从零开始,现在没有测试过,但应该让你知道如何做到这一点。