本文主要是介绍Android AnalogClock TextClock DigitalClock Chronometer 时钟 定时器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
AnalogClock 相关属性:
android:dial="@drawable/img1" //表盘 android:hand_hour="@drawable/alert_dialog_icon" //时针 android:hand_minute="@drawable/alert_dialog_icon" //分针
TextClock 相关属性:
android:format12Hour="yyyy/mm/dd hh:mm:ss" //12小时 格式
android:format24Hour="yyyy/mm/dd hh:mm:ss" //24小时 格式
android:timeZone="+8" //时区设置
Chronometer 相关属性:
ch.setFormat("计时:%s");ch.setBase(SystemClock.elapsedRealtime()); ch.start();ch.stop();
ch.setOnChronometerTickListener();
package shortcut.song.com.myapplication;import android.os.SystemClock; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Chronometer;public class ClockActivity extends AppCompatActivity {Chronometer ch;Button mButtonStart;@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_clock);ch = (Chronometer)findViewById(R.id.chronometer);mButtonStart = (Button)findViewById(R.id.start_chr);mButtonStart.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {ch.setBase(SystemClock.elapsedRealtime());ch.start();mButtonStart.setEnabled(false);}});ch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {@Override public void onChronometerTick(Chronometer chronometer) {if (SystemClock.elapsedRealtime() - ch.getBase() > 20 * 1000){ch.stop();mButtonStart.setEnabled(true);}}});} }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_clock" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="shortcut.song.com.myapplication.ClockActivity"><AnalogClock android:id="@+id/analogclock1" android:layout_width="wrap_content" android:layout_height="wrap_content" /><AnalogClock android:id="@+id/analogclock2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/analogclock1" android:dial="@drawable/img1" android:hand_hour="@drawable/alert_dialog_icon" android:hand_minute="@drawable/alert_dialog_icon" /><DigitalClock android:id="@+id/digitalclock" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/analogclock2" /><TextClock android:id="@+id/textclock1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/digitalclock" android:timeZone="+7" android:format12Hour="yyyy年mm月dd日 hh:mm" /><TextClock android:id="@+id/textclock2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/textclock1" android:format24Hour="yyyy/mm/dd hh:mm:ss" android:timeZone="+8" /><Chronometer android:id="@+id/chronometer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/textclock2" /><Button android:id="@+id/start_chr" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/chronometer" android:text="Start" /></RelativeLayout>
这篇关于Android AnalogClock TextClock DigitalClock Chronometer 时钟 定时器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!