如何检查是否用户已经点击了一个按钮? [英] How to check if a user has already clicked a Button?

查看:321
本文介绍了如何检查是否用户已经点击了一个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查,如果用户已经点击了一个按钮...我有我的菜单,包括促销code里面......我需要检查,如果用户已经点击它,这样我可以告诉他(的一个按钮下一次他点击吧)你已经兑现了此促销$​​ C $ C!我该怎么办呢?我只需要一块code在那里我可以检查按钮点击。

PS:alredy搜索整个网络这个

感谢您!

编辑:我下面code。希望它会帮助别人...

  @覆盖
公共布尔onOptionsItemSelected(菜单项项){
    布尔点击= FALSE;
    开关(item.getItemId()){
        案例R.id.get code:
            共享preferences preF = getShared preferences(促销,MODE_PRIVATE);
            布尔激活= pref.getBoolean(激活,FALSE);
            如果(激活== FALSE){按钮BTN =(按钮)findViewById(R.id.get code);
            AlertDialog.Builder dlgAlert =新AlertDialog.Builder(本);
            dlgAlert.setMessage(的getString(R.string.congrats)+\\ n+的getString(R.string.promcd)+\\ n+ \"ASC2013-\"+Build.ID+\"-\"+android.os.Build.SERIAL.charAt(3)+\"-\"+Build.SERIAL.charAt(6)+\"-\"+Build.SERIAL.charAt(9)+\"-\"+Build.SERIAL.charAt(12));
            dlgAlert.setPositiveButton(R.string.go,
                    新DialogInterface.OnClickListener(){
                        公共无效的onClick(DialogInterface对话,诠释它){
                            意图emailIntent =新意图(android.content.Intent.ACTION_SEND);
                            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,新的String [] {lorenzocascio@gmail.com});
                            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,的getString(R.string.validreq)+ Build.BOOTLOADER);
                            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,的getString(R.string.why)+\\ n+的getString(R.string.validreq1)+\\ n+的getString(R.string.dialogMSG1);                            emailIntent.setType(纯/文);                            startActivity(emailIntent);                        }
                    });
            dlgAlert.setCancelable(真);
            dlgAlert.create()显示()。
                共享preferences.Editor编辑器= pref.edit();
                editor.putBoolean(激活,真正的);
                editor.commit();
            }
      打破;
    }
      开关(item.getItemId()){
      案例R.id.settings:
          意图设置=新意图(MainActivity.this,Settings.class);
            MainActivity.this.startActivity(设置);
    }
    返回true;
}


解决方案

您可以在用户单击该按钮保存在共享preferences的标志。接下来的时间,你可以在共​​享preferences检查是否存在该标志。

  button.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
    共享preferences preF = getShared preferences(促销,MODE_PRIVATE);
    布尔激活= pref.getBoolean(激活,FALSE);
    如果(激活== FALSE){//用户未活化促销code - >激活
        共享preferences.Editor编辑器= pref.edit();
        editor.putBoolean(激活,真正的);
        editor.commit();
    }
}

I need to check if a user has already clicked a button… I have a button in my menu with a "promo code" inside… I need to check if user already clicked it so i can tell him (the next time he clicks it) "You already redeemed this promo code!" How do i do that? I need only the piece of code where i can check for button clicked.

PS: alredy searched the whole web for this!

Thank you!

EDIT: my code below. Hope it’ll help others…

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    boolean clicked = false;
    switch (item.getItemId()) {
        case R.id.getcode: 
            SharedPreferences pref = getSharedPreferences("promo", MODE_PRIVATE);
            boolean activated = pref.getBoolean("activated", false);
            if(activated == false) {  Button btn = (Button) findViewById(R.id.getcode);
            AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
            dlgAlert.setMessage(getString(R.string.congrats) + "\n" + getString(R.string.promcd) + "\n" + "ASC2013-"+Build.ID+"-"+android.os.Build.SERIAL.charAt(3)+"-"+Build.SERIAL.charAt(6)+"-"+Build.SERIAL.charAt(9)+"-"+Build.SERIAL.charAt(12));
            dlgAlert.setPositiveButton(R.string.go,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {"lorenzocascio@gmail.com"});
                            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(R.string.validreq)+Build.BOOTLOADER);
                            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, getString(R.string.why) + "\n" + getString(R.string.validreq1) +"\n"+getString(R.string.dialogMSG1);

                            emailIntent.setType("plain/text");  

                            startActivity(emailIntent);

                        }
                    });
            dlgAlert.setCancelable(true);
            dlgAlert.create().show(); 
                SharedPreferences.Editor editor = pref.edit();
                editor.putBoolean("activated", true);
                editor.commit();
            } 
      break;
    }
      switch (item.getItemId()) {
      case R.id.settings: 
          Intent settings = new Intent(MainActivity.this, Settings.class);
            MainActivity.this.startActivity(settings);


    }
    return true;
}

解决方案

You can save a flag in shared preferences if the user clicks the button. Next time, you can check in the shared preferences if there exists the flag.

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    SharedPreferences pref = getSharedPreferences("promo", MODE_PRIVATE);
    boolean activated = pref.getBoolean("activated", false);
    if(activated == false) {  // User hasn't actived the promocode -> activate it 
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean("activated", true);
        editor.commit();
    } 
}

这篇关于如何检查是否用户已经点击了一个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆