java - ImageButton 每天应仅允许 10 次点击,第二天再点击 10 次

标签 java android multithreading

我创建了一个图像按钮,每天只允许点击 10 次。现在 我想使用线程让图像按钮 hibernate 24 小时,之后应该允许第二天(24 小时后)再次点击 10 次。我应该如何使用线程让按钮单击 hibernate 24 小时。请建议我如何解决这个问题。

ActivityTwo.java:

public class ActivityTwo extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_two);
    ImageButton playbtn1 = (ImageButton) findViewById(R.id.playButton1);
    playbtn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), ActivityTwo.class);
            startActivity(intent);
            intent.putExtra("scoretwo",1);
            setResult(RESULT_OK,intent);
        }
    });

MainActivity.java:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final int RC_ACT_TWO = 1;
    TextView scoreCount = (TextView) findViewById(R.id.score_points);
    String score = scoreCount.getText().toString();
    int scorevalue = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageButton viewbtn1 = (ImageButton) findViewById(R.id.view1);
        viewbtn1.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {
                Intent intent1 = new
                Intent(getApplicationContext(), ActivityTwo.class);
                startActivity(intent1);
                startActivityForResult(intent1, RC_ACT_TWO);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_ACT_TWO) {
            if (resultCode == RESULT_OK) {
                int incre = 0;
                int add = data.getIntExtra("scoretwo", 0);
                scorevalue = Integer.parseInt(score);
                if (add != 0) {
                    incre = incre + add;
                    if (incre > 0 && incre < 11) {
                        scorevalue = scorevalue + incre;
                        scoreCount.setText(scorevalue);
                    }
                    else {}
                }
                else {
                    scoreCount.setText("-1");
                }
            }
        }
    }

最佳答案

这是一种可能性:您可以使用 Android 的 SharedPreferences 来实现此目的。保存第一次点击的时间戳,以及迄今为止的点击量..

单击按钮或启动 Activity 时,检查保存的点击量。如果达到 10,则禁用该按钮。

在 Activity 开始时,还要检查保存的时间戳。如果超过 24 小时,请再次重新启用该按钮并重置首选项中保存的点击量。

有关更多信息,请查看this link .

关于java - ImageButton 每天应仅允许 10 次点击,第二天再点击 10 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41847111/

相关文章:

java - 如何查找机器上安装的 JVM 在 Windows 操作系统上是 HotSpot 还是 JRockit

java - 开源线程池库

java - 组织.hibernate.QueryException : could not resolve property with column name already defined

android - 我在哪里可以访问 Eclipse 中的数据库以进行 Android 开发?

android - 考虑为 Firebase 添加 .indexOn

java - 线程在倒计时时卡住 UI

java - 如何从组合框中检索隐藏字段?

android - wifiManager.startScan 不返回任何结果(请需要一些指导)

multithreading - 在Grand Central Dispatch中使用 “queues”, “multicore”和 “threads”

multithreading - 计算 900,000 个字符串之间的 Dice 系数的有效方法是什么?