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

How to call an item only for the first time of running

我想向用户显示 "png" 图片,但仅在应用程序第一次运行时显示,因此在后续运行中不会显示。

我通常是这样做的:

helpimg.setVisibility(View.VISIBLE);

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

您可以使用 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);
}