码迷,mamicode.com
首页 > 移动开发 > 详细

android 多线程刷新界面

时间:2015-01-12 19:17:53      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

[1].[代码] [Java]代码 跳至 [1]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.example.ui_reflash;
 
import java.util.ArrayList;
 
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class MainActivity extends Activity {
  public int i=0;
  public int num=0;
  private Button btn;
  private Button btnan;
  public final static  int success=1;
  private int no_success=0;
  private Thread thread;
  Handler mhandler=new Handler(){
    public void handleMessage(Message msg){
        //switch(msg.what){
        //case success:
        System.out.println("mhandler");
        //btn.setText(msg.obj.toString());
        ArrayList arraylist=new ArrayList();
        arraylist=(ArrayList)msg.obj;
        //num++;
        btn.setText(arraylist.get(arraylist.size()-1).toString());
       // break;
        //}
    }
  };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button)findViewById(R.id.btn);
        btnan=(Button)findViewById(R.id.btnan);
        btnan.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("hello");
                 thread=new Thread(runnable);
                thread.start();
            }
 
        });
    }
    
  Runnable runnable=new Runnable(){
    @Override
    public void run() {
        // TODO Auto-generated method stub
        ArrayList list=new ArrayList();
             
            while(true){
                i++;
                list.add(i);
            mhandler.obtainMessage(success,list).sendToTarget();
            System.out.println("runnable");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
    }
       
  };
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >
 
    <Button
        android:id="@+id/btnan"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="点击"
    />
    <Button
        android:id="@+id/btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="数字"
    />
 
</LinearLayout>

android 多线程刷新界面

标签:

原文地址:http://blog.csdn.net/u014311022/article/details/42646017

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!