Android編程之簡(jiǎn)單計(jì)時(shí)器實(shí)現(xiàn)方法
本文實(shí)例講述了Android編程之簡(jiǎn)單計(jì)時(shí)器實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
這里利用ContextMenu(上下文菜單),Chronometer實(shí)現(xiàn)簡(jiǎn)單計(jì)數(shù)器。
Main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width= "fill_parent" android:layout_height = "fill_parent" android:gravity = "center_horizontal" > <Chronometer android:id ="@+id/chronometer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:format="%s" android:textSize="80px" android:textColor="#00FF00" /> </LinearLayout >
/layout/menu/context_menu.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/timer_start" android:title=" 開始計(jì)時(shí) " /> <item android:id="@+id/timer_stop" android:title=" 終止計(jì)時(shí) " /> <item android:id="@+id/timer_reset" android:title=" 清零 " /> </menu>
主Activity:
public class MainActivity extends Activity {
private Chronometer timer;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 獲得計(jì)時(shí)器對(duì)象
timer = (Chronometer)this.findViewById(R.id.chronometer);
//長(zhǎng)按計(jì)時(shí)器時(shí),出現(xiàn)上下文菜單
this.registerForContextMenu(timer);
}
//創(chuàng)建上下文菜單
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
// ContextMenu的Item不支持Icon,所以不用再資源文件中,為它們?cè)O(shè)定圖標(biāo)
if (v.getId() == R.id.chronometer)
{
//加載xml菜單布局文件
this.getMenuInflater().inflate(R.menu.context_menu, menu);
// 設(shè)定頭部圖標(biāo)
menu.setHeaderIcon(R.drawable.icon);
// 設(shè)定頭部標(biāo)題
menu.setHeaderTitle(" 計(jì)時(shí)器控制選項(xiàng) ");
}
}
//選擇菜單項(xiàng)后的響應(yīng)
@Override
public boolean onContextItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.timer_start:
// 將計(jì)時(shí)器清零
timer.setBase(SystemClock.elapsedRealtime());
//開始計(jì)時(shí)
timer.start();
break;
case R.id.timer_stop:
//停止計(jì)時(shí)
timer.stop();
break;
case R.id.timer_reset:
//將計(jì)時(shí)器清零
timer.setBase(SystemClock.elapsedRealtime());
break;
}
return super.onContextItemSelected(item);
}
}
運(yùn)行結(jié)果如圖所示:

長(zhǎng)按計(jì)時(shí)器彈出上下文菜單選擇開始計(jì)時(shí):

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- android之計(jì)時(shí)器(Chronometer)的使用以及常用的方法
- Android時(shí)分秒計(jì)時(shí)器的兩種實(shí)現(xiàn)方法
- android計(jì)時(shí)器,時(shí)間計(jì)算器的實(shí)現(xiàn)方法
- android開發(fā)教程之間隔執(zhí)行程序(android計(jì)時(shí)器)
- Android實(shí)現(xiàn)的秒表計(jì)時(shí)器示例
- Android計(jì)時(shí)器的三種實(shí)現(xiàn)方式(Chronometer、Timer、handler)
- Android 編程下的計(jì)時(shí)器代碼
- Android開發(fā)實(shí)現(xiàn)的計(jì)時(shí)器功能示例
- Android中CountDownTimer倒計(jì)時(shí)器用法實(shí)例
- Android實(shí)現(xiàn)計(jì)時(shí)器功能
相關(guān)文章
5個(gè)Android開發(fā)中比較常見的內(nèi)存泄漏問題及解決辦法
本文主要介紹了5個(gè)Android開發(fā)中比較常見的內(nèi)存泄漏問題及解決辦法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02
Android快速實(shí)現(xiàn)一個(gè)財(cái)務(wù)APP程序詳解
這篇文章主要介紹了Android實(shí)現(xiàn)的財(cái)務(wù)APP程序,結(jié)合前后端共功能完善,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Android開發(fā)實(shí)現(xiàn)NFC刷卡讀取的兩種方式
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)中實(shí)現(xiàn)NFC刷卡讀取的兩種方式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Android實(shí)現(xiàn)網(wǎng)易云音樂高仿版流程
這篇文章主要介紹了Android實(shí)現(xiàn)網(wǎng)易云音樂高仿版,包含了首頁(yè)復(fù)雜發(fā)現(xiàn)界面布局和功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Android小部件Widget開發(fā)過程中的坑和問題小結(jié)
這篇文章主要介紹了Android小部件Widget開發(fā)過程中的坑和問題小結(jié),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Android關(guān)于Button背景或樣式失效問題解決方法
大家好,本篇文章主要講的是Android關(guān)于Button背景或樣式失效問題解決方法,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
Android ProgressDialog用法之實(shí)現(xiàn)app上傳文件進(jìn)度條轉(zhuǎn)圈效果
這篇文章主要介紹了Android ProgressDialog用法之實(shí)現(xiàn)app上傳文件進(jìn)度條轉(zhuǎn)圈效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03

