Computer >> Máy Tính >  >> Lập trình >> Android

Làm thế nào để tích hợp Android Snackbar?

Snackbar cũng giống như Bánh mì nướng trong android nhưng nó sẽ tương tác với hành động. Nó sẽ hiển thị thông báo ở cuối màn hình mà không có bất kỳ tương tác nào với các chế độ xem khác và tự động đóng sau một khoảng thời gian chờ.

Ví dụ này trình bày cách tích hợp Android Snackbar.

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Mở build.gradle và thêm phụ thuộc vào thư viện hỗ trợ thiết kế.

apply plugin: 'com.android.application'
android {
   compileSdkVersion 28
   defaultConfig {
      applicationId "com.example.andy.myapplication"
      minSdkVersion 19
      targetSdkVersion 28
      versionCode 1
      versionName "1.0"
      testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
   }
   buildTypes {
      release {
         minifyEnabled false
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      }
   }
}
dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation 'com.android.support:appcompat-v7:28.0.0'
   implementation 'com.android.support:design:28.0.0'
   implementation 'com.android.support.constraint:constraint-layout:1.1.3'
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'com.android.support.test:runner:1.0.2'
   androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Bước 3 - Thêm mã sau vào res / layout / activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="https://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:id="@+id/layout"
   android:layout_height="match_parent">
   <Button
      android:id="@+id/button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:text="Click here" />
</android.support.design.widget.CoordinatorLayout>

Trong đoạn mã trên, chúng ta có khai báo id bố cục vì snackback yêu cầu chế độ xem gốc. khi người dùng nhấp vào nút, nó sẽ mở thanh nhanh ở dưới cùng.

Bước 4 - Thêm mã sau vào src / MainActivity.java

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
   CoordinatorLayout coordinatorLayout;
   @TargetApi(Build.VERSION_CODES.O)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      coordinatorLayout=findViewById(R.id.layout);
      Button button=findViewById(R.id.button);
      button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Snackbar snackbar = Snackbar
            .make(coordinatorLayout, "Welcome to tutorialspoint.com", Snackbar.LENGTH_LONG)
            .setAction("Click", new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                  String url = "https://www.tutorialspoint.com";
                  Intent i = new Intent(Intent.ACTION_VIEW);
                  i.setData(Uri.parse(url));
                  startActivity(i);
               }
            });
            // Changing message text color
            snackbar.setActionTextColor(Color.RED);
            // Changing action button text color
            View sbView = snackbar.getView();
            TextView textView = (TextView) sbView.findViewById(
            android.support.design.R.id.snackbar_text);
            textView.setTextColor(Color.YELLOW);
            snackbar.show();
         }
      });
   }
}

Ở trên khi người dùng nhấp vào nút, nó sẽ hiển thị thanh nhanh như hình dưới đây -

Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Welcome to tutorialspoint.com", Snackbar.LENGTH_LONG)
.setAction("Click", new View.OnClickListener() {
   @Override
   public void onClick(View view) {
      String url = "https://www.tutorialspoint.com";
      Intent i = new Intent(Intent.ACTION_VIEW);
      i.setData(Uri.parse(url));
      startActivity(i);
   }
});
// Changing message text color
snackbar.setActionTextColor(Color.RED);
// Changing action button text color
View sbView = snackbar.getView();
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.YELLOW);
snackbar.show();

Trong đoạn mã trên, chúng tôi đã khai báo thanh ăn nhẹ với thông báo chào mừng bạn đến với tutorialspoint.com và một nút là "nhấp chuột". khi người dùng nhấp vào nút nhấp, nó sẽ mở ra một trang web điểm hướng dẫn trong trình duyệt mặc định.

Bước 5 - Thêm mã sau vào tệp kê khai.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
   package="com.example.andy.myapplication">
   <uses-permission android:name="android.permission.INTERNET" />
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
      <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
   </application>
</manifest>

Trong đoạn mã trên, chúng tôi đã khai báo quyền truy cập internet để hiển thị trang web.

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ một studio android, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Run từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn -

Làm thế nào để tích hợp Android Snackbar?

Khi bạn nhấp vào một nút, nó sẽ hiển thị thông báo thanh nhanh với nút nhấp như hình trên.

Làm thế nào để tích hợp Android Snackbar?

Khi bạn nhấp vào nút "click", nó sẽ mở trình duyệt mặc định với trang web tutorialspoint.com.