這次學習了Handler,這篇博客只為記錄只用。
一、使用Handler每隔五秒鍾彈出一個Toast消息
布局兩個button
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <Button
8 android:id="@+id/btnstart"
9 android:layout_width="fill_parent"
10 android:layout_height="wrap_content"
11 android:text="Start Handler" />
12
13 <Button
14 android:id="@+id/btnend"
15 android:layout_width="fill_parent"
16 android:layout_height="wrap_content"
17 android:text="EndHandler" />
18
19 </LinearLayout>
過程實現
1 package com.yyj.Handler1;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.os.Handler;
6 import android.view.View;
7 import android.view.View.OnClickListener;
8 import android.widget.Button;
9 import android.widget.Toast;
10
11 public class Handler1Activity extends Activity {
12 /** Called when the activity is first created. */
13 Handler handler=new Handler();
14 @Override
15 public void onCreate(Bundle savedInstanceState) {
16 super.onCreate(savedInstanceState);
17 setContentView(R.layout.main);
18
19 Button buttonstart=(Button)findViewById(R.id.btnstart);
20 Button buttonend=(Button)findViewById(R.id.btnend);
21
22 buttonstart.setOnClickListener(new OnClickListener() {
23
24 @Override
25 public void onClick(View v) {
26 handler.post(newthread);
27 }
28 });
29 buttonend.setOnClickListener(new OnClickListener() {
30
31 @Override
32 public void onClick(View v) {
33 handler.removeCallbacks(newthread);
34 }
35 });
36 }
37 Runnable newthread =new Runnable(){
38
39 @Override
40 public void run() {
41 Toast.makeText(Handler1Activity.this, "By newthread!", Toast.LENGTH_SHORT).show();
42 handler.postDelayed(newthread, 5000);
43 }
44 };
45 }
二、使用Handler控制進度條
布局一個水平進度條,和一個按鈕
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <ProgressBar
8 android:id="@+id/progressBar"
9 style="?android:attr/progressBarStyleHorizontal"
10 android:layout_width="200dp"
11 android:progress="0"
12 android:layout_height="wrap_content"
13 android:visibility="gone" />
14
15 <Button
16 android:id="@+id/button1"
17 android:layout_width="wrap_content"
18 android:layout_height="wrap_content"
19 android:text="Start" />
20
21 </LinearLayout>
功能實現
package com.yyj.Hander2; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; public class Hander2Activity extends Activity { /** Called when the activity is first created. */ Button start; ProgressBar progressBar; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); start=(Button)findViewById(R.id.button1); progressBar=(ProgressBar)findViewById(R.id.progressBar); start.setOnClickListener(new OnClickListener() { public void onClick(View v) { progressBar.setVisibility(View.VISIBLE); handler.post(newthread); } }); } Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { progressBar.setProgress(msg.arg1); handler.post(newthread); super.handleMessage(msg); } }; Runnable newthread=new Runnable() { int i=0; public void run() { i+=10; Message message=handler.obtainMessage(); //還有可以用setData來實現 message.arg1=i; try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } handler.sendMessage(message); if (i==100) { handler.removeCallbacks(newthread); } } }; }
因為這個學的感覺有些亂,所以不做講解只做記錄、查詢之用