Handler的使用

mac2022-06-30  82

package com.example.wang.testapp2; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import java.util.Random; public class TestActivity6 extends AppCompatActivity { TextView tv_3,tv_5; TextView tv_4,tv_6; Button bt_4,bt_5,bt_3; //定义 Handler,Handler在子线程里发消息,在主线程里处理消息,所以需要定义为全局变量 Handler h=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); //处理消息 if (msg.what==1) { String m=msg.obj.toString(); tv_5.setText(tv_5.getText()+" "+m); } if (msg.what==2) { tv_5.setText(tv_5.getText()+" 空消息"); } } }; int i=10; Handler hl=new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 1: if (i==20) { bt_3.setEnabled(false); bt_5.setEnabled(false); return; } i++; tv_6.setText(i + ""); //发送 hl.sendEmptyMessageDelayed(1, 1000); hl.removeMessages(2); bt_3.setEnabled(false); bt_4.setEnabled(true); bt_5.setEnabled(true); break; case 2: if (i==1) { bt_4.setEnabled(false); bt_5.setEnabled(false); return; } i--; tv_6.setText(i + ""); //发送 hl.sendEmptyMessageDelayed(2, 1000); hl.removeMessages(1); bt_3.setEnabled(true); bt_4.setEnabled(false); bt_5.setEnabled(true); break; case 3: hl.removeMessages(1); hl.removeMessages(2); bt_3.setEnabled(true); bt_4.setEnabled(true); bt_5.setEnabled(false); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test6); tv_3=(TextView)findViewById(R.id.tv_3); tv_4=(TextView)findViewById(R.id.tv_4); tv_5=(TextView)findViewById(R.id.tv_5); tv_6=(TextView)findViewById(R.id.tv_6); bt_4=(Button)findViewById(R.id.bt_4); bt_5=(Button)findViewById(R.id.bt_5); bt_3=(Button)findViewById(R.id.bt_3); bt_5.setEnabled(false); } public void bt3_OnClick(View v) { switch (v.getId()) { case R.id.bt_3: //发送增加的消息 hl.sendEmptyMessage(1); break; case R.id.bt_4: //发送减少的消息 hl.sendEmptyMessage(2); break; case R.id.bt_5: //发送暂停的消息 hl.sendEmptyMessage(3); break; //default: } } public void bt2_OnClick(View v) { //启动线程 new Thread(){ @Override public void run() { //发送消息 //1-创建Message Message m=Message.obtain(); m.what=1; m.obj="新信息1"; //2-发送即时消息 h.sendMessage(m); //发送延时消息 m=Message.obtain(); m.what=1; m.obj="新消息2"; h.sendMessageDelayed(m,2000); //发送空消息 h.sendEmptyMessage(2); //发送延时空消息 h.sendEmptyMessageDelayed(2,3000); } }.start(); } public void bt1_OnClick(View v) { //创建子线程1 new Thread() { @Override public void run() { for (int i = 0; i < 10; i++) { try { //暂停一下,时间随机 Thread.sleep(new Random().nextInt(2000)); } catch (Exception e) {} runOnUiThread(new Runnable() { @Override public void run() { tv_3.setText(tv_3.getText()+" 北京"); } }); } runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(TestActivity6.this, "北京 循环结束", Toast.LENGTH_SHORT).show(); } }); } }.start(); new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 10; i++) { runOnUiThread(new Runnable() { @Override public void run() { tv_4.setText(tv_4.getText()+" 上海"); } }); try { //暂停一下,时间随机 Thread.sleep(new Random().nextInt(2000)); } catch (Exception e) {} } runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(TestActivity6.this, "上海 循环结束", Toast.LENGTH_SHORT).show(); } }); } }).start(); } } java <?xml version="1.0" encoding="utf-8"?> <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" tools:context="com.example.wang.testapp2.TestActivity6" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="北京" android:id="@+id/tv_3"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="上海" android:id="@+id/tv_4"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="启动" android:onClick="bt1_OnClick"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_5" android:text="接收的消息"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发消息" android:onClick="bt2_OnClick"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_6" android:text="10" android:layout_gravity="center"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="增加" android:onClick="bt3_OnClick" android:id="@+id/bt_3"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="减少" android:onClick="bt3_OnClick" android:id="@+id/bt_4"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="暂停" android:onClick="bt3_OnClick" android:id="@+id/bt_5"/> </LinearLayout> xml

转载于:https://www.cnblogs.com/wangchuanqi/p/5499943.html

最新回复(0)