利用Android中的三大主件来实现一个码表
???? Android中的四大組件大家都知道吧!
Activity、Service、Broadcast Receiver、Content Provider
所以我們這里會用到前三大組件:
//這是service類
package cn.zuxia.andorid.app;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;
public class MyService extends Service {
private static String values;
private static boolean flag=true;
//定義四個整型變量,用來存儲時分秒毫秒等
private int mHour = 0, mMinute = 0, mSecond = 0, mMs = 0;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
//這里我們是返回一個空。當然,如果我們要綁定服務,可以在這里發返回一個服務
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
/**
* 重寫的start方法,用來執行一系列操作
*/
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
//得到傳入的值
values = intent.getStringExtra("values");
//如果傳入的是start,也就是開始計時按鈕,就進入
if ("start".equals(values)) {
flag=true;
}
//如果傳入的值是pause,也就是暫停,那么在這里面將要執行暫停操作
else if("pause".equals(values)){
flag=false;
}else if ("continue".equals(values)) {
flag=true;
}
//啟用一個線程,用來刷新時間
new Thread(new Runnable() {
@Override
public void run() {
???????????????????????????????? //暫停時就進入這個循環,循環條件是根據傳入的值,也就是按下的按鈕進行處理的
while (!flag) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//啟用一個while循環
while (flag) {
try {
//休眠10毫秒,然后刷新
Thread.sleep(10);
mMs++;
//獲取一個message實例,傳入數據
Message message = mHandler.obtainMessage();
//定義一個messageId
message.what = 1;
//發送信息
mHandler.sendMessage(message);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (1 == msg.what) {
//定義四個String,主要用于修飾界面美觀,也就是說用來填充顯示是的空位,之后在if語句中賦值
String value1 = "", value2 = "", value3 = "", value4 = "";
//應用一系列的if判斷,用來操作時分秒的計算與賦值
if (mMs >= 99) {
mMs = 0;
mSecond++;
}
if (mSecond > 59) {
mSecond = 0;
mMinute++;
}
if (mMinute > 59) {
mMinute = 0;
mHour++;
}
//如果都是一位數,那么就在其前面加一個0,填充
if (mMs < 10) {
value1 = "0";
}
if (mSecond < 10) {
value2 = "0";
}
if (mMinute < 10) {
value3 = "0";
}
if (mHour < 10) {
value4 = "0";
}
//打印顯示數據
android_stop_watchActivity.mTextView.setText("第一個碼表:" + value4
+ mHour + ":" + value3 + mMinute + ":" + value2
+ mSecond + ":" + value1 + mMs);
}
}
};
}
?
//這是activity類
?package cn.zuxia.andorid.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class android_stop_watchActivity extends Activity {
//聲明兩個button控件
private Button buttonStart,buttonPause;
??? //聲明一個TextView控件,用于顯示碼表信息
public static TextView mTextView;
??? @Override
??? public void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.main);
??????? //獲取TextView實例
??????? mTextView=(TextView) findViewById(R.id.displayFirstInfo);
??????? buttonStart=(Button) findViewById(R.id.startBtn);
??????? buttonPause=(Button) findViewById(R.id.pauseBtn);
??? }
??? /**
???? * 實現單擊按鈕監聽事件方法
???? * @param v
???? */
??? public void onAction(View v){
??? //創建Intent對象
??? Intent intent=new Intent();
??? if (v.getId() == R.id.startBtn) {
startService(intent,"start");
buttonStart.setEnabled(false);
} else if (v.getId() == R.id.pauseBtn) {
String text=buttonPause.getText().toString();
if ("暫停".equals(text)) {
startService(intent,"pause");
buttonPause.setText("繼續");
}else{
startService(intent,"continue");
buttonPause.setText("暫停");
}
} else if (v.getId() == R.id.stopBtn) {
stopService(intent);
buttonStart.setEnabled(true);
}
??? }
??? /**
???? * 創建一個啟動服務方法
???? */
??? private void startService(Intent intent,String values){?
??? intent.setClass(android_stop_watchActivity.this, MyService.class);
??? intent.putExtra("values", values);
??? startService(intent);
??? }
}
//這是布局文件
?<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? android:layout_width="fill_parent"
??? android:layout_height="fill_parent"
??? android:orientation="vertical" >
??? <TextView
??????? android:id="@+id/displayText"
??????? android:layout_width="fill_parent"
??????? android:layout_height="wrap_content"
??????? android:text="@string/hello"
??????? android:gravity="center" />
??? <Button
??????? android:id="@+id/startBtn"
??????? android:layout_width="100dip"
??????? android:layout_height="wrap_content"
??????? android:layout_below="@id/displayText"
??????? android:layout_margin="4dip"
??????? android:onClick="onAction"
??????? android:text="開始"
??????? />
???? <Button
??????? android:id="@+id/pauseBtn"
??????? android:layout_width="100dip"
??????? android:layout_height="wrap_content"
??????? android:layout_below="@id/displayText"
??????? android:layout_toRightOf="@id/startBtn"
??????? android:layout_margin="4dip"
??????? android:onClick="onAction"
??????? android:text="暫停"
??????? />
????? <Button
??????? android:id="@+id/stopBtn"
??????? android:layout_width="100dip"
??????? android:layout_height="wrap_content"
??????? android:layout_below="@id/displayText"
???????? android:layout_toRightOf="@id/pauseBtn"
??????? android:layout_margin="4dip"
??????? android:onClick="onAction"
??????? android:text="停止"
??????? />
????? <TextView
????????? android:id="@+id/displayFirstInfo"
?????????? android:layout_width="fill_parent"
?????????? android:layout_height="wrap_content"
?????????? android:layout_below="@id/pauseBtn"
?????????? android:text="第一個碼表:00:00:00:00"
?????????? android:gravity="center"
????????? />
</RelativeLayout>
?
?
轉載于:https://www.cnblogs.com/Catherine-Brain/p/3475506.html
總結
以上是生活随笔為你收集整理的利用Android中的三大主件来实现一个码表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jquery右键菜单
- 下一篇: [转]编程语言中的 鸭子模型(duck