0
点赞
收藏
分享

微信扫一扫

Android(二十二):时间、计时器 - TextClock、DigitalClock、AnalogClock、Chronometer

东林梁 2022-07-14 阅读 89


展示

Android(二十二):时间、计时器 - TextClock、DigitalClock、AnalogClock、Chronometer_xml

源码

Resources/drawable/hand_hour.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<size android:width="20dp" android:height="800dp" />
<solid android:color="#ff00ff" />
</shape>
</item>
</selector>

Resources/drawable/hand_minute.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<size android:width="20dp" android:height="1200dp" />
<solid android:color="#ffffff" />
</shape>
</item>
</selector>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">

<RelativeLayout
android:id="@+id/layout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextClock
android:id="@+id/tc01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="年 -> yyyy yy" />

<TextClock
android:id="@+id/tc02"
android:layout_below="@id/tc01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="月 -> MMMM MM" />

<TextClock
android:id="@+id/tc03"
android:layout_below="@id/tc02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="周 -> EEEE EE" />

<TextClock
android:id="@+id/tc04"
android:layout_below="@id/tc03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="日 -> dd d" />

<TextClock
android:id="@+id/tc05"
android:layout_below="@id/tc04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="午 -> a" />

<TextClock
android:id="@+id/tc06"
android:layout_below="@id/tc05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="时 -> hh h" />

<TextClock
android:id="@+id/tc07"
android:layout_below="@id/tc06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="分 -> mm m" />

<TextClock
android:id="@+id/tc08"
android:layout_below="@id/tc07"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="秒 -> ss s" />

<DigitalClock
android:id="@+id/dc01"
android:layout_below="@id/tc08"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</RelativeLayout>

<AnalogClock
android:id="@+id/ac01"
android:layout_toRightOf="@id/layout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<AnalogClock
android:id="@+id/ac02"
android:layout_below="@id/layout01"
android:layout_width="200dp"
android:layout_height="200dp"
android:dial="@drawable/logo"
android:hand_hour="@drawable/hand_hour"
android:hand_minute="@drawable/hand_minute" />

<Chronometer
android:id="@+id/cm01"
android:layout_below="@id/ac02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#ff0000"
android:textSize="40dp" />

<LinearLayout
android:layout_below="@id/cm01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:orientation="horizontal">

<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="开始" />

<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="停止" />

<Button
android:id="@+id/btnReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="重置" />

<Button
android:id="@+id/btnFormat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="格式化" />
</LinearLayout>

</RelativeLayout>

var chronometer = (Chronometer) FindViewById(Resource.Id.cm01);
var btn_start = (Button) FindViewById(Resource.Id.btnStart);
var btn_stop = (Button) FindViewById(Resource.Id.btnStop);
var btn_base = (Button) FindViewById(Resource.Id.btnReset);
var btn_format = (Button) FindViewById(Resource.Id.btnFormat);

btn_start.Click += (sender, args) => { chronometer?.Start(); };
btn_stop.Click += (sender, args) => { chronometer?.Stop(); };
btn_base.Click += (sender, args) => { chronometer.Base = SystemClock.ElapsedRealtime(); };
btn_format.Click += (sender, args) => { chronometer.Format = "Time:%s"; };

chronometer.ChronometerTick += (sender, args) =>
{
Toast.MakeText(this, chronometer.Text, ToastLength.Long)?.Show();
};


举报

相关推荐

0 条评论