简体   繁体   English

单击后检查 SharedPref

[英]SharedPref Check After Click

I am trying to make a system that, when a user downloads my app, the user earns coins.我正在尝试制作一个系统,当用户下载我的应用程序时,用户可以赚取金币。 Users must press the verify button after installation.安装后用户必须按下验证按钮。 Yes, he can earn a coin.是的,他可以赚到硬币。 But I am trying to do a disable verify button after one click because after app installs users can press a lot of times and earn a lot of coins.但是我试图在单击后禁用验证按钮,因为在应用安装后,用户可以按很多次并赚取很多硬币。 (verify button: check app installation and give a coin to the user) When user clicks verify button sharedprefs value go False (验证按钮:检查应用程序安装并给用户一个硬币)当用户单击验证按钮 sharedprefs 值 go False

But I don't understand that it is not working.但我不明白它不起作用。 I tried to implement code in different positions and try to insert new boolean for holding "canearn" data but it doesn't work.我试图在不同的位置实现代码并尝试插入新的 boolean 来保存“canearn”数据,但它不起作用。 Thanks for your help.谢谢你的帮助。 (Sorry for bad syntax:( ) (抱歉语法错误:()

  private SharedPreferences coins;
  private SharedPreferences sharedPref ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    coins = getSharedPreferences("Rewards", MODE_PRIVATE);
    sharedPref = getSharedPreferences("canearn",MODE_PRIVATE);

    bttn3 = (Button)findViewById(R.id.veribut1);
     bttn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         //   SharedPreferences getter = getSharedPreferences("canearn", Activity.MODE_PRIVATE);
         //   boolean test = sharedPref.getBoolean("canearn",true);

        if(sharedPref.getBoolean("canearn",true) == true) {

                final List<String> installedPackages = getInstalledAppsPackageNameList();
                String packageNameC = "com.********";


                    if (installedPackages.contains(packageNameC)) {

                    int coinCount = Integer.parseInt(coins.getString("Coins", "0"));

                    SharedPreferences.Editor editor = sharedPref.edit();
                    editor.putBoolean("canearn", false); //add boolean
                    editor.commit(); //save
                    coinCount = coinCount + 500;
                    SharedPreferences.Editor coinsEdit = coins.edit();
                    coinsEdit.putString("Coins", String.valueOf(coinCount));
                    coinsEdit.apply();
                    Toast.makeText(getApplication(), "Coin Earned", Toast.LENGTH_LONG).show();



                }    else {
                    Toast.makeText(getApplication(), "Install and Open App first", Toast.LENGTH_LONG).show();
                }


            }
            else
            {             
       Log.i("app","dontcount");
            }
        }
    });

Well,there are many ways to achieve your goal but if you want to disable the button.好吧,有很多方法可以实现您的目标,但如果您想禁用该按钮。 You can just use你可以使用

bttn3.setEnabled(false);

at the end of your onClick() function.onClick() function 的末尾。 No need to make preferences false You should also setVisibility() as per your question to make the button invisible as you won't be needing it.无需将首选项设为 false 您还应该根据您的问题 setVisibility() 以使按钮不可见,因为您不需要它。

Do one thing, inside the onCreate() method, check the SharedPreferences value for the coin.做一件事,在 onCreate() 方法中检查硬币的 SharedPreferences 值。 If the user has received a coin,如果用户收到硬币,

bttn3.setClickable(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM