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

Làm thế nào để đặt độ trễ trong Android?

Trong một số trường hợp, sau một thời gian, chúng tôi cần cập nhật giao diện người dùng để giải quyết vấn đề này, Trong ví dụ này trình bày cách đặt độ trễ trong 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 - Thêm mã sau vào res / layout / activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   android:id = "@+id/parent"
   xmlns:tools = "https://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity"
   android:gravity = "center"
   android:orientation = "vertical">
   <TextView
      android:id = "@+id/textChanger"
      android:layout_margin = "20dp"
      android:textAlignment = "center"
      android:text = "Initial text"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content" />
</LinearLayout>

Trong đoạn mã trên, chúng tôi đã sử dụng chế độ xem văn bản, chính nó hiển thị "văn bản ban đầu" sau một thời gian trì hoãn, nó sẽ cập nhật bằng văn bản mới.

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

package com.example.andy.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
   int view = R.layout.activity_main;
   TextView textChanger;
   @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(view);
      textChanger = findViewById(R.id.textChanger);
      Handler handler = new Handler();
      handler.postDelayed(new Runnable() {
         @Override
         public void run() {
            textChanger.setText("After some delay, it changed to new text");
         }
      }, 5000);
   }
}

Trong đoạn mã trên, chúng tôi đã sử dụng trình xử lý để duy trì độ trễ như được hiển thị bên dưới -

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
   @Override
   public void run() {
      textChanger.setText("After some delay, it changed to new text");
   }
}, 5000);

Trong đoạn mã trên sau 5000ms nó đang cập nhật văn bản. 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ừ android studio, 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 Chạy 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 độ trễ trong Android?

Ban đầu, nó sẽ hiển thị văn bản như hình trên. sau một thời gian, nó sẽ cập nhật văn bản như hình dưới đây -

Làm thế nào để đặt độ trễ trong Android?