简体   繁体   English

在按钮上单击我要显示祝酒消息

[英]On a button click I want to display a toast message

I want that this message is displayed only the first three times this button is clicked and never after that even if the application is restarted, I want to know the best way to achieve it. 我希望仅在单击此按钮的前三下显示此消息,并且即使在重新启动应用程序之后也不要在此之后显示此消息,我想知道实现此消息的最佳方法。 I plan to make a DB and enter values in that DB on button press and place my toast logic inside of an if statement. 我计划制作一个数据库,并在按下按钮时在该数据库中输入值,并将我的Toast逻辑放在if语句内。 I want to know if there is a better way to do it without using a DB. 我想知道是否有更好的方法可以不使用数据库。

If by better you mean easier then you can use the SharedPreferences mechanism. 如果更好地意味着您更轻松,那么可以使用SharedPreferences机制。

The shared preferences is basically a property set that is common for your entire activity or can be used by each activity separately. 共享首选项基本上是整个活动共有的属性集,或者可以由每个活动单独使用。

class Listener implements OnClickListener{
    final private SharedPreferences prefs;
    Listener(SharedPreferences prefs){
         this.prefs=prefs;
    }
    public void onClick(View view) {
        int count = prefs.getInt("toastCount",0);
        if(count>=3) return;
        //do something else here
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt("toastCount", count+1);
        editor.commit();
    }
}

and you can initiate this listnener using the getPreferences() or getSharedPreferences() methods from your activity 您可以使用活动中的getPreferences()getSharedPreferences()方法来启动此侦听器

i think Shared Preferences is better than DB. 我认为共享首选项比数据库更好。

You just add count(int) in shared Prefernces. 您只需在共享的首选项中添加count(int)

You can use this code: 您可以使用以下代码:

button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {

        Toast.makeText(app.getBaseContext(),"Your message", 
            Toast.LENGTH_SHORT).show();
}

}); });

Consider persisting your data somewhere in the memory. 考虑将数据保留在内存中的某个位置。 Trivial data like this should not go into the database. 这样的琐碎数据不应该进入数据库。 It would be a bad decision. 这将是一个错误的决定。

暂无
暂无

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

相关问题 如果 RecyclerView 中的列表为空,我想显示带有空消息的 Toast - If the List is Empty in the RecyclerView, I want to Display Toast with Empty Message 我想在 ViewModel Class 中显示有关 Retrofit 故障的 Toast 消息 - I want to display Toast message on Retrofit failure in ViewModel Class Android 按钮点击显示不同的 Toast 消息取决于点击次数 - Android button click show different Toast message depend on the click count 我想写在文本框中,如果它显示在页面上,如果它没有显示在页面上,则单击下一步按钮,然后直接单击下一步按钮 - i want to write in textbox if it is display on page and click on next button if it is not display on the page then directly click on next button 单击后如何显示吐司消息并使按钮不可见? - how to show a toast message and make a button invisible after click? Android Toast消息显示 - Android Toast message display 我有一个 Recycler View,onRun 会显示,但是单击按钮后它将是空的,我想设置一个默认的 recycler 视图 - I have a Recycler View, onRun will display, but upon a button click it will be empty, I want to set a default recycler view 单击图像按钮时,如何创建一个类以显示一次吐司消息? - How to create a class to display toast message once when image button clicked? 如何在不使用 toast 消息的情况下在微调器和按钮上设置错误消息,例如在 edittext 上? - how do I set an Error message on spinner and button like on edittext without using toast message? 单击选项卡时如何显示吐司消息? - How to show toast message when click tab?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM