首页 > 代码库 > 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 | 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 多线程刷新界面
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。