📜  如何在Android中添加小吃店

📅  最后修改于: 2021-05-08 20:35:26             🧑  作者: Mango

Snackbar提供有关操作的轻量级反馈。该消息显示在移动设备的屏幕底部,而在较大的设备上显示在左下方。小吃栏出现在屏幕的所有元素上方。但是没有组件受它影响。在视图层次结构中具有CoordinatorLayout可使Snackbar启用某些功能,例如滑动到关闭和自动移动小部件。 Snackbar与Toast类似,但唯一的主要区别是可以使用Snackbar添加动作。

方法:

  1. build.gradle文件中添加支持库,并在依赖项”部分添加Material Design依赖项,这是Material Design的一部分,这就是我们必须添加依赖项的原因。
    dependencies {
        implementation 'com.google.android.material:material:1.1.0'
    }
    
  2. 现在,将以下代码添加到activity_main.xml文件中。它将创建一个名为Open Snackbar的按钮。
    activity_main.xml
    
    
      
        


    MainActivity.java
    package org.geeksforgeeks.gfgsnackbar;
      
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.coordinatorlayout
        .widget.CoordinatorLayout;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
      
    import com.google.android.material
        .snackbar
        .Snackbar;
      
    public class MainActivity
        extends AppCompatActivity {
      
        Button button;
        CoordinatorLayout layout;
      
        @Override
        protected void onCreate(
            Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            button = findViewById(R.id.button);
            layout = findViewById(R.id.layout);
      
            button.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v)
                    {
      
                        // Create a snackbar
                        Snackbar snackbar
                            = Snackbar
                                  .make(
                                      layout,
                                      "Message is deleted",
                                      Snackbar.LENGTH_LONG)
                                  .setAction(
                                      "UNDO",
      
                                      // If the Undo button
                                      // is pressed, show
                                      // the message using Toast
                                      new View.OnClickListener() {
                                          @Override
                                          public void onClick(View view)
                                          {
                                              Toast
                                                  .makeText(
                                                      MainActivity.this,
                                                      "Undo Clicked",
                                                      Toast.LENGTH_SHORT)
                                                  .show();
                                          }
                                      });
      
                        snackbar.show();
                    }
                });
        }
    }


  3. 现在,在MainActivity中添加以下代码。 Java文件。这将定义按钮,并将onClickListener添加到按钮。在onClickListener中,创建并调用了Snackbar。因此,每当单击按钮时,onClickListener都会创建一个小吃栏并调用它,用户会看到该消息。这个快餐栏包含一个动作,如果单击该动作,将显示一个吐司。

    主要活动。Java

    package org.geeksforgeeks.gfgsnackbar;
      
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.coordinatorlayout
        .widget.CoordinatorLayout;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
      
    import com.google.android.material
        .snackbar
        .Snackbar;
      
    public class MainActivity
        extends AppCompatActivity {
      
        Button button;
        CoordinatorLayout layout;
      
        @Override
        protected void onCreate(
            Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            button = findViewById(R.id.button);
            layout = findViewById(R.id.layout);
      
            button.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v)
                    {
      
                        // Create a snackbar
                        Snackbar snackbar
                            = Snackbar
                                  .make(
                                      layout,
                                      "Message is deleted",
                                      Snackbar.LENGTH_LONG)
                                  .setAction(
                                      "UNDO",
      
                                      // If the Undo button
                                      // is pressed, show
                                      // the message using Toast
                                      new View.OnClickListener() {
                                          @Override
                                          public void onClick(View view)
                                          {
                                              Toast
                                                  .makeText(
                                                      MainActivity.this,
                                                      "Undo Clicked",
                                                      Toast.LENGTH_SHORT)
                                                  .show();
                                          }
                                      });
      
                        snackbar.show();
                    }
                });
        }
    }
    

输出: