【问题标题】:How to make a Button clickable only once in a day如何让按钮一天只能点击一次
【发布时间】:2014-06-30 09:13:49
【问题描述】:

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

我不知道如何实现这个功能

我知道我可以将其设置为 View.gone,但我希望它在第二天再次可见。单击按钮时应该出现什么以获得一天的时间

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

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

        break;
    }
    }
}

帮助将不胜感激。

【问题讨论】:

    标签: android button click


    【解决方案1】:

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

    【讨论】:

    • 除此之外的任何其他选项
    • 如果您不希望人们只是更改手机的日期,请验证服务器端。 @Dude 为什么,这是一个非常好的方法。
    • 你能发一个例子来说明如何做到这一点
    • 我知道这是一个好方法,但我还没有使用偏好
    • 我如何在点击按钮时写上首选项,因为当点击按钮时它会再次写入
    【解决方案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);
    
          }
    

    这只是从头开始,目前尚未测试,但应该让您了解如何做到这一点。

    【讨论】:

    • 知道了,但在我的偏好中设置初始日期的位置
    • 我做了一些解决方法。放一些布尔值来检查这是否是第一次使用该应用程序。第一次,sharedPrefs 中没有保存任何值,所以你会得到默认的 true(参见 boolean firstTimeUsed)。如果为真,则设置按钮直接可见。在按钮单击时,将值始终设置为 false,因此在第一次单击按钮后您永远不会获得值 true(如果用户有 root,则希望用户删除了应用程序或 sharedPreferences)
    • 为什么不只存储currentTimeMillis() 并检查大于86400000 的差异,如果是.. 启用按钮并重置。最初,当活动首次启动时,默认值为 0,如 getLong(<token>, 0)
    • 原因是,因为如果我只使用currentTimeInMillis,如果我检查大于86400000,则buttonClick是时间相关的。例如,用户在下午12:13点击,按钮将在12点之后才可以点击:第二天下午13点。我不认为这是目标
    猜你喜欢
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多