首页 > 代码库 > android 多线程刷新界面

android 多线程刷新界面

[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
packagecom.example.ui_reflash;
 
importjava.util.ArrayList;
 
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.app.Activity;
importandroid.view.Menu;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
 
publicclass MainActivity extendsActivity {
  publicint i=0;
  publicint num=0;
  privateButton btn;
  privateButton btnan;
  publicfinal static  int success=1;
  privateint no_success=0;
  privateThread thread;
  Handler mhandler=newHandler(){
    publicvoid handleMessage(Message msg){
        //switch(msg.what){
        //case success:
        System.out.println("mhandler");
        //btn.setText(msg.obj.toString());
        ArrayList arraylist=newArrayList();
        arraylist=(ArrayList)msg.obj;
        //num++;
        btn.setText(arraylist.get(arraylist.size()-1).toString());
       // break;
        //}
    }
  };
    @Override
    protectedvoid 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(newOnClickListener() {
            @Override
            publicvoid onClick(View v) {
                System.out.println("hello");
                 thread=newThread(runnable);
                thread.start();
            }
 
        });
    }
    
  Runnable runnable=newRunnable(){
    @Override
    publicvoid run() {
        // TODO Auto-generated method stub
        ArrayList list=newArrayList();
             
            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 多线程刷新界面