android之Chronometer
首先定义activity_main.xml文件
代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Chronometer android:id="@+id/myChronometer" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/butStart" android:text="开始计时" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/butStop" android:text="停止计时" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout>
然后改写MainActivity类
代码如下:
package com.example.myclock; import android.os.Bundle; import android.os.SystemClock; import android.os.Vibrator; import android.app.Activity; import android.app.Service; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; import android.widget.Chronometer.OnChronometerTickListener; public class MainActivity extends Activity { private Chronometer myChronometer = null; private Button butStart = null; private Button butStop = null; private Vibrator vibrator = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.myChronometer = (Chronometer)super.findViewById(R.id.myChronometer); this.vibrator = (Vibrator)super.getApplication().getSystemService(Service.VIBRATOR_SERVICE); this.butStart = (Button)super.findViewById(R.id.butStart); this.butStop = (Button)super.findViewById(R.id.butStop); this.butStart.setOnClickListener(new OnClickListenerlmplStart()); this.butStop.setOnClickListener(new OnClickListenerlmplStop()); this.myChronometer.setFormat("当前计时时间:%s。"); this.myChronometer.setOnChronometerTickListener(new OnChronometerTickListenerlmpl()); } private class OnChronometerTickListenerlmpl implements OnChronometerTickListener{ public void onChronometerTick(Chronometer chronometer){ String time = chronometer.getText().toString().replaceAll("[^(\\d{2}:\\d{2})]", ""); if("01:00".equals(time)){ MainActivity.this.vibrator.vibrate(new long[]{1000,10,1000,100},0); } } } private class OnClickListenerlmplStart implements OnClickListener{ public void onClick(View view){ MainActivity.this.myChronometer.start(); } } private class OnClickListenerlmplStop implements OnClickListener{ public void onClick(View view){ MainActivity.this.myChronometer.stop(); MainActivity.this.myChronometer.setBase(SystemClock.elapsedRealtime()); MainActivity.this.vibrator.cancel(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
最后改写AndroidMainfest.xml文件
代码如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myclock" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.VIBRATE"/> </manifest>
运行效果如下图:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。