日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

补8

發(fā)布時(shí)間:2025/4/5 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 补8 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

  • 幀動(dòng)畫(時(shí)間的暫停和開始)
  • 創(chuàng)建安卓應(yīng)用
  • 編寫視圖(activity_shuji.xml)
  • 字符串資源文件strings.xml
  • 主界面
  • 運(yùn)行效果

幀動(dòng)畫(時(shí)間的暫停和開始)

創(chuàng)建安卓應(yīng)用

編寫視圖(activity_shuji.xml)

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="vertical"tools:context=".shuji"><TextViewandroid:id="@+id/tv_time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="70sp"android:textColor="#ff0000" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_start"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="doStart"android:text="@string/start" /><Buttonandroid:id="@+id/btn_stop"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="doStop"android:text="@string/stop" /></LinearLayout> </LinearLayout>

字符串資源文件strings.xml

<string name="app">動(dòng)態(tài)顯示時(shí)間</string><string name="start">開始</string><string name="stop">停止</string>

主界面

package net.zjs.xianxing;import androidx.appcompat.app.AppCompatActivity;import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.TextView;import java.text.SimpleDateFormat; import java.util.Date;public class shuji extends AppCompatActivity {private Thread thread;private Handler handler;private TextView tvTime;private SimpleDateFormat sdf;private boolean anniu;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//利用布局資源文件設(shè)置用戶界面setContentView(R.layout.activity_shuji);//通過資源標(biāo)識獲得控件實(shí)例tvTime = findViewById(R.id.tv_time);//實(shí)例化簡單日期格式化對象sdf = new SimpleDateFormat("hh:mm:ss");tvTime.setText(sdf.format(new Date()));//創(chuàng)建消息處理器,送的消息進(jìn)行處理,更新主界面元素(標(biāo)簽)的內(nèi)容handler = new Handler(){public void handleMessage(Message msg){super.handleMessage(msg);if(msg.what ==0x0001){tvTime.setText(sdf.format(new Date()));}}};//創(chuàng)建線程,定時(shí)發(fā)送消息}public void doStart(View view){anniu =true;thread = new Thread(new Runnable() {@Overridepublic void run() {while(anniu){//向主線程發(fā)送消息handler.sendEmptyMessage(0x001);//讓線程睡眠500毫秒try {Thread.sleep(500);}catch (InterruptedException e){e.printStackTrace();}}}});//啟動(dòng)線程thread.start();}public void doStop(View view){anniu = false;thread =null;}protected void onDestroy(){super.onDestroy();//銷毀線程thread = null;} }

運(yùn)行效果

總結(jié)

以上是生活随笔為你收集整理的补8的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。