【问题标题】:two different action on single button click?单击单个按钮有两种不同的操作?
【发布时间】:2012-01-02 18:25:28
【问题描述】:

我有一个按钮,我需要执行两个操作,即当我在安装后第一次打开我的应用程序时,它应该执行一个任务。第一次启动后它不想执行第一个操作怎么能我实施这个?

我实现了这个,

private int _clicks = 0;
k = (Button)findViewById(R.id.button1);


    if(count == 1)
         //do whatever
    if(count == 2)
         //do whatever
    if(count == 3)
         //do whatever
}
});

【问题讨论】:

    标签: android button multiprocessing


    【解决方案1】:

    我会使用SharedPreferences 对象作为“标志”。第一次打开您的应用程序时,请在 SharedPreferences 对象中设置一些标志。在您的按钮onClickListener() 中检查此标志的值。

    【讨论】:

      【解决方案2】:

      您必须记住您已经执行了该任务。因此,我建议您在执行任务后设置的 SharedPreferences 中存储一个值,并在将来处理任何其他点击之前重新检查。

      SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
      
      if(!prefs.getBoolean("WAS_INITIALIZED", false)){
          // will only be executed after the first run. 
          // remember that it was initialized
          Editor editor = prefs.edit();
          editor.putBoolean("WAS_INITIALIZED", true);
      
          // put you code which should only be run once here ..
      }
      

      【讨论】:

        【解决方案3】:

        简单。在 SD 卡中为您的操作创建文件。根据您的要求修改文件,因此每当您的应用程序启动时,它都会读取文件并根据您在那里编写的操作做出反应。

        试试这个。

        没有全局变量就不能改变动作,所以全局变量会放在SD卡里。试着理解这一点。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-08-09
          • 1970-01-01
          • 2019-04-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-02
          相关资源
          最近更新 更多