Android实现简单的时钟
2021/11/2 6:11:53
本文主要是介绍Android实现简单的时钟,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
参考:
https://blog.csdn.net/huanongjingchao/article/details/38374233?locationNum=5&fps=1
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.wear.widget.BoxInsetLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="@dimen/box_inset_layout_padding" tools:context=".MainActivity" tools:deviceIds="wear"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:padding="@dimen/inner_frame_layout_padding" app:layout_boxedEdges="all"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/top" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:orientation="horizontal" > <TextView android:id="@+id/mint" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="00" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=":" android:textSize="30dp" /> <TextView android:id="@+id/sec" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="00" android:textSize="30dp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/top" android:layout_centerHorizontal="false" android:layout_marginTop="60dp" android:orientation="horizontal" > <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start" /> <Button android:id="@+id/reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="reset" /> </LinearLayout> </LinearLayout> </FrameLayout> </androidx.wear.widget.BoxInsetLayout>
mainActivity
package com.example.miaobiao; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import com.example.miaobiao.databinding.ActivityMainBinding; import java.lang.ref.WeakReference; public class MainActivity extends Activity { private TextView mint; private TextView sec; private Button start; private Button reset; private long timeusedinsec; public boolean isstop = false; private Handler mHandler = new MyHandler(this); static class MyHandler extends Handler { private final WeakReference<Activity> mActivity; MyHandler(Activity activity){ this.mActivity = new WeakReference<>(activity); } @Override public void handleMessage(Message msg) { MainActivity activity = (MainActivity) mActivity.get(); if(activity != null) { super.handleMessage(msg); activity.handleMyMessage(msg); } } } public void handleMyMessage( Message msg ) { switch (msg.what) { case 1: // 添加更新ui的代码 if (!isstop) { updateView(); mHandler.sendEmptyMessageDelayed(1, 1000); } break; case 0: break; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } private void initViews() { mint = (TextView) findViewById(R.id.mint); sec = (TextView) findViewById(R.id.sec); reset = (Button) findViewById(R.id.reset); start = (Button) findViewById(R.id.start); reset.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub mint.setText("00"); sec.setText("00"); start.setText("start"); timeusedinsec=0; isstop=true; } }); start.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub mHandler.removeMessages(1); String aaa=start.getText().toString(); if(aaa.equals("start")){ mHandler.sendEmptyMessage(1); isstop = false; start.setText("pause"); }else { mHandler.sendEmptyMessage(0); isstop = true; start.setText("start"); } } }); } private void updateView() { timeusedinsec += 1; int minute = (int) (timeusedinsec / 60)%60; int second = (int) (timeusedinsec % 60); if (minute < 10) mint.setText("0" + minute); else mint.setText("" + minute); if (second < 10) sec.setText("0" + second); else sec.setText("" + second); } }
效果:
这篇关于Android实现简单的时钟的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-18android.permission.read_media_video
- 2024-01-18android_getaddrinfo failed eai_nodata
- 2024-01-18androidmo
- 2024-01-15Android下三种离屏渲染技术
- 2024-01-09Android 蓝牙使用
- 2024-01-06Android对接华为AI - 文本识别
- 2023-11-15代码安全之代码混淆及加固(Android)
- 2023-11-10简述Android语音播报TTS
- 2023-11-06Android WiFi工具类
- 2023-07-22Android开发未来的出路