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

Làm thế nào để tạo đồng hồ kỹ thuật số với textview trong Android?

Ví dụ này trình bày Cách tạo đồng hồ kỹ thuật số với textview 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"
   xmlns:app = "https://schemas.android.com/apk/res-auto"
   xmlns:tools = "https://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity"
   android:orientation = "vertical">
   <TextView
      android:id = "@+id/textview"
      android:layout_width = "match_parent"
      android:layout_height = "match_parent"
      android:gravity = "center"
      android:textStyle = "bold"
      android:textSize = "50sp"
      app:fontFamily = "@font/orbitron" />
</LinearLayout>

Trong đoạn mã trên, chúng tôi đã sử dụng textview. Nó sẽ hiển thị đồng hồ với hoạt ảnh nhấp nháy như chúng ta thấy trong đồng hồ kỹ thuật số bình thường.

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

package com.example.andy.myapplication;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
   TextView textview;
   @Override
   protected void onCreate(Bundle readdInstanceState) {
      super.onCreate(readdInstanceState);
      setContentView(R.layout.activity_main);
      textview = findViewById(R.id.textview);
      Date today = new Date();
      SimpleDateFormat formatter = new SimpleDateFormat("HH:MM");
      textview.setText(formatter.format(today));
      blink();
   }
   private void blink() {
      final Handler hander = new Handler();
      new Thread(new Runnable() {
         @Override
         public void run() {
            try {
               Thread.sleep(550);
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
            hander.post(new Runnable() {
               @Override
               public void run() {
                  if(textview.getVisibility() = = View.VISIBLE) {
                     textview.setVisibility(View.INVISIBLE);
                  } else {
                     textview.setVisibility(View.VISIBLE);
                  }
                  blink();
               }
            });
         }
      }).start();
   }
}

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ạo đồng hồ kỹ thuật số với textview trong Android?

Trong khoảng thời gian trên, chế độ xem văn bản sẽ nhấp nháy vĩnh viễn