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

Làm thế nào để sử dụng bố cục trình tự Android?

Trước khi đi vào bố cục trình tự android, chúng ta nên biết bố cục trình tự trong android là gì. Bố cục trình tự chứa một chuỗi các bước với thanh tiến trình. Theo trình tự, nó theo sau một thanh tiến trình hoạt ảnh.

Ví dụ này trình bày Cách sử dụng bố cục trình tự Android.

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 (ứng dụng) 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.google.code.gson:gson:2.8.5'
   implementation 'com.android.support.constraint:constraint-layout:1.1.3'
   testImplementation 'junit:junit:4.12'
   implementation 'com.github.transferwise:sequence-layout:1.0.7'
   androidTestImplementation 'com.android.support.test:runner:1.0.2'
   androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

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

// Tệp xây dựng cấp cao nhất nơi bạn có thể thêm các tùy chọn cấu hình chung cho tất cả các dự án con / mô-đun.

buildscript {
   repositories {
      google()
      jcenter()
   }
   dependencies {
      classpath 'com.android.tools.build:gradle:3.2.1'
      // NOTE: Do not place your application dependencies here; they belong
      // in the individual module build.gradle files
   }
}
allprojects {
   repositories {
      google()
      jcenter()
      maven { url "https://jitpack.io" }
   }
}
task clean(type: Delete) {
   delete rootProject.buildDir
}

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

<?xml version="1.0" encoding="utf-8"?>
<com.transferwise.sequencelayout.SequenceLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:app="https://schemas.android.com/apk/res-auto">
   <com.transferwise.sequencelayout.SequenceStep
      android:id="@+id/first"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:subtitle="Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
         Lorem Ipsum has been the industry's standard dummy text ever since the 1500s."
      app:anchor="30 Nov"
      app:title="First step"/>
   <com.transferwise.sequencelayout.SequenceStep
      android:id="@+id/second"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:subtitle="Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
         Lorem Ipsum has been the industry's standard dummy text ever since the 1500s."
      app:title="Second step"/>
   <com.transferwise.sequencelayout.SequenceStep
      android:id="@+id/third"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:anchor="Today"
      app:title="Third step"
      app:subtitle="Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
         Lorem Ipsum has been the industry's standard dummy text ever since the 1500s" />
</com.transferwise.sequencelayout.SequenceLayout>

Ở phần trên, chúng tôi đã khai báo bố cục trình tự là bố cục mẹ và thêm các bước trình tự dưới dạng các bước riêng lẻ. mỗi bước liên hệ với chế độ xem neo, tiêu đề và phụ đề.

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

package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
import com.transferwise.sequencelayout.SequenceStep;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   SequenceStep sequenceStep,sequenceStep2,sequenceStep3;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      sequenceStep=findViewById(R.id.first);
      sequenceStep2=findViewById(R.id.second);
      sequenceStep3=findViewById(R.id.third);
      sequenceStep2.setActive(true);
      sequenceStep.setOnClickListener(this);
      sequenceStep2.setOnClickListener(this);
      sequenceStep3.setOnClickListener(this);
   }
   @Override
   public void onClick(View v) {
      switch (v.getId()) {
         case R.id.first:
            Toast.makeText(MainActivity.this,"This is first step",Toast.LENGTH_LONG).show();
            break;
         case R.id.second:
            Toast.makeText(MainActivity.this,"This is second step",Toast.LENGTH_LONG).show();
            break;
         case R.id.third:
            Toast.makeText(MainActivity.this,"This is Third step",Toast.LENGTH_LONG).show();
            break;
      }
   }
}

Trong đoạn mã trên, chúng tôi đã khai báo các bước trình tự và đưa ra trên Trình nghe nhấp chuột. Để thực hiện các bước, hãy sử dụng mã sau.

sequenceStep2.setActive(true);

Trong đoạn mã trên, chúng ta đã khai báo rằng serial2 đang hoạt động có nghĩa là thanh tiến trình sẽ hiển thị cho đến bước trình tự 2.

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 để sử dụng bố cục trình tự Android?

Trong ví dụ trên, khi chúng ta đã khai báo chế độ đang hoạt động, nó sẽ hiển thị một thanh tiến trình.