【问题标题】:Limit button press to three times per week将按钮按下限制为每周 3 次
【发布时间】:2018-11-14 22:12:25
【问题描述】:

我有一个按钮,我想允许每周按 3 次。

我怎样才能跟踪它并确保一旦达到最大值就不会发生按钮按下?

这是我的按钮点击和计数的基本代码。

int count = 0;
limit_ButtonPress()

Button btn = findViewById(R.id.comp)
btn.setOnClickListener(new OnClickListener){
    (View v){
    count++
}
private void limit_ButtonPress(){
    if(count> 0  && count< 5){
    btn.setVisabilty(View.Invisable)
} 

我怎样才能限制每周三按?

【问题讨论】:

  • 从服务器获取数据并跟踪经过的时间
  • 我的应用已连接到 Firebase。你会如何建议我得到时间/日期然后跟踪所说的时间

标签: java android counter limit


【解决方案1】:

看看这个。您希望该按钮每周只能单击三次,并且不超过此。所以有这种类型的 Firebase 数据库结构:

"root/" : {
    "users" : {
        "user_01" : {
                //some profile data about the user like name email etc.
                "data" : "val"
                //this is important
                "signUpTime" : 1563245126 //this is the timestamp of when the user signed up
            },
        ...
    }
   "activity" : {
       "user_01" : {
            random_id_01 : {
                "clickTime" : 156845164 //timestamp of click
            },
            ...
       }
   }
}

它负责为用户定义对数据库的读写访问权限。

现在是 Java/Android 部分:

private void initUI(){
    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("activity").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
    Query query = databaseReference.orderByChild("clickTime").limitToLast(3);
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){
                //now count the children in dataSnapshot
                int totalChildren = (int) dataSnapshot.getChildrenCount();
                if(totalChildren > 2){
                    //now check for this person properly
                    int milliSecInAWeek = 7*24*60*60*1000; //perhaps I'm right ;)
                    //now check if the last of the three click was within this week 
                    for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                        //well this is actually a loop kind of thing
                        //but we are just concerned about the first most click so we will call return just on the first child iteration :)
                        UserActivity userActivity = snapshot.getValue(UserActivity.class);
                        
                        if(userActivity.getClickTime() - System.currentTimeMillis() < milliSecInAWeek){
                            //this person has clicked three times in this week disable the button
                        }else{
                            //this person has got chance(s) to click. let the button be enabled
                        }
                        return;
                    }
                }else{
                    //this user has clicked less then 3 times so the let the button be clickable for the user
                }
            }else{
                //there is no activity by this user let the button be clickable
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

private class UserActivity{
    
    private float clickTime;

    public UserActivity() {
        
    }

    public UserActivity(float clickTime) {
        this.clickTime = clickTime;
    }

    public float getClickTime() {
        return clickTime;
    }

    public void setClickTime(float clickTime) {
        this.clickTime = clickTime;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    相关资源
    最近更新 更多