Android 单击一个按钮可以执行两种不同的操作?

Android 单击一个按钮可以执行两种不同的操作?,android,button,multiprocessing,Android,Button,Multiprocessing,我有一个按钮,我需要执行两个操作,即当我在安装后第一次打开我的应用程序时,它应该执行一个任务。在第一次启动后,它不想执行第一个操作。我如何实现这一点 我实施这个, private int _clicks = 0; k = (Button)findViewById(R.id.button1); if(count == 1) //do whatever if(count == 2) //do whatever if(count == 3

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

我实施这个,

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
}
});

我将使用
SharedReferences
对象作为“标志”。首次打开应用程序时,请在
SharedReferences
对象中设置一些标志。检查按钮
onClickListener()

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

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 ..
}

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

试试这个

如果没有全局变量,则无法更改操作,因此全局变量将放置在SD卡中。试着去理解这一点