如何仅在第一次运行时调用项目

时间:2015-02-19 21:31:26

标签: java android visibility

我想展示一个" png"图像给用户,但仅在应用程序第一次运行时,因此对于后续运行,它将不会出现。

我通常这样做:

helpimg.setVisibility(View.VISIBLE);

然后,通过使用OnTouchlistener函数,我可以使它消失,但每次用户运行我的应用程序时都必须使用此方法。 我需要第一次这样做,因为它只是一个介绍性的帮助图像,它为用户提供了如何使用该应用程序的说明。 那我怎么能编码呢?

1 个答案:

答案 0 :(得分:2)

您可以使用SharedPreferece来实现此目的。例如:

//Check if the image has been shown
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
boolean first = sharedPref.getBoolean("first", true);
if(first){
    helpimg.setVisibility(View.VISIBLE);
    //Set the preference to false
    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putBoolean("first", false);
    editor.commit();
}else{
    helpimg.setVisibility(View.GONE);
}