2012-01-02 85 views
0

我有一个按钮,我需要两个动作发生,那就是当我安装后第一次打开我的应用程序时,它应该执行任务。第一次启动后,它不想执行第一个动作我怎么能执行这个?单按钮点击两种不同的动作?

我实现这一点,

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

回答

3

你必须记住你已经完成了任务。因此,我建议您在执行任务后设置的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 .. 
} 
4

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

0

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

试试这个。

没有一个全局变量,你不能改变动作,所以全局变量将被放置在SD卡中。试着理解这一点。