小試身手前的小知識

Handler 處理器

Runnable 需要做的工作事項

小試身手小範例

device028  

device029  

  快 轉 ing

device030  

device031  

device032  

Java檔

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class sihTimer extends Activity {
/** Called when the activity is first created. */
Handler aHandler;
TextView aTextView;
int count = 11;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);

aTextView = (TextView)findViewById(R.id.TextView01);
aHandler = new Handler();
aHandler.post(runnable);
}
final Runnable runnable = new Runnable() {
public void run() {
// TODO Auto-generated method stub

if (count > 0) {
aTextView.setText(Integer.toString(count-1));
count--;
aHandler.postDelayed(runnable, 1000);
}else{
aTextView.setText("碰");
}
}
};
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (aHandler != null) {
aHandler.removeCallbacks(runnable);
}
}
}

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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="10秒倒數計時器"
android:textSize="40sp"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="300sp"
android:id="@+id/TextView01"
/>
</LinearLayout>

arrow
arrow
    創作者介紹
    創作者 S 的頭像
    S

    S's Journal

    S 發表在 痞客邦 留言(2) 人氣()