📜  Android中WhatsNew库的实现

📅  最后修改于: 2022-05-13 01:54:58.488000             🧑  作者: Mango

Android中WhatsNew库的实现

在本文中,我们将使用 WhatsNew 库展示应用程序的重要内容。它可以简单地被认为是显示一些信息,如通知、说明、常见问题或条款和条件之类的东西。下面给出了一个示例 GIF,以了解我们将在本文中做什么。请注意,我们将使用Java语言来实现这个项目。

分步实施

第 1 步:创建一个新项目

要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,选择Java作为编程语言。

第 2 步:添加依赖项和 mavenCentral() 存储库



导航到Gradle Scripts > build.gradle(Module:app)并在依赖项部分添加以下依赖项。

mavenCentral()存储库添加到您的构建文件中。将其添加到 allprojects{} 部分中存储库末尾的根 build.gradle 中。

添加此依赖项后同步您的项目,现在我们将转向它的实现。

步骤 3:使用 activity_main.xml 文件

导航到app > res > layout > activity_main.xml并将以下代码添加到该文件中。下面是activity_main.xml文件的代码。

XML


  
    


Java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
  
import androidx.appcompat.app.AppCompatActivity;
  
import io.github.tonnyl.whatsnew.WhatsNew;
import io.github.tonnyl.whatsnew.item.WhatsNewItem;
import io.github.tonnyl.whatsnew.util.PresentationOption;
  
public class MainActivity extends AppCompatActivity {
  
    Button button;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // initialising layout
        button = findViewById(R.id.whatsnew);
          
        // click on button
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // creating a new Instance for 
                // showing Content using WhatsNew
                WhatsNew whatsNew = WhatsNew.newInstance(
                        // add as much item you want with 
                        // item title, description and image
                        new WhatsNewItem("Firebase Authentication", "Firebase Authentication service provides easy to use UI libraries and SDKs to authenticate users to your app.", R.drawable.circle),
                        new WhatsNewItem("Firebase Realtime Database", "The Firebase Realtime Database is a cloud based NoSQL database ", R.drawable.circle),
                        new WhatsNewItem("Cloud Firestore", " The cloud Firestore is a NoSQL document database that provides services like store, sync, and query through the application on a global scale", R.drawable.circle),
                        new WhatsNewItem("Firebase", " irebase is a product of Google which helps developers to build, manage, and grow their apps easily. It helps developers to build their apps faster and in a more secure way.", WhatsNewItem.NO_IMAGE_RES_ID)
                );
                whatsNew.setPresentationOption(PresentationOption.DEBUG);
                // on click present this layout
                whatsNew.presentAutomatically(MainActivity.this);
            }
        });
    }
}


第 4 步:使用MainActivity。 Java文件

转到主活动。 Java文件,参考如下代码。

下面是MainActivity的代码。 Java文件。代码中添加了注释以更详细地理解代码。

Java

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
  
import androidx.appcompat.app.AppCompatActivity;
  
import io.github.tonnyl.whatsnew.WhatsNew;
import io.github.tonnyl.whatsnew.item.WhatsNewItem;
import io.github.tonnyl.whatsnew.util.PresentationOption;
  
public class MainActivity extends AppCompatActivity {
  
    Button button;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // initialising layout
        button = findViewById(R.id.whatsnew);
          
        // click on button
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // creating a new Instance for 
                // showing Content using WhatsNew
                WhatsNew whatsNew = WhatsNew.newInstance(
                        // add as much item you want with 
                        // item title, description and image
                        new WhatsNewItem("Firebase Authentication", "Firebase Authentication service provides easy to use UI libraries and SDKs to authenticate users to your app.", R.drawable.circle),
                        new WhatsNewItem("Firebase Realtime Database", "The Firebase Realtime Database is a cloud based NoSQL database ", R.drawable.circle),
                        new WhatsNewItem("Cloud Firestore", " The cloud Firestore is a NoSQL document database that provides services like store, sync, and query through the application on a global scale", R.drawable.circle),
                        new WhatsNewItem("Firebase", " irebase is a product of Google which helps developers to build, manage, and grow their apps easily. It helps developers to build their apps faster and in a more secure way.", WhatsNewItem.NO_IMAGE_RES_ID)
                );
                whatsNew.setPresentationOption(PresentationOption.DEBUG);
                // on click present this layout
                whatsNew.presentAutomatically(MainActivity.this);
            }
        });
    }
}

输出: