处理信息Message-Handler
Posted storm47
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了处理信息Message-Handler相关的知识,希望对你有一定的参考价值。
package com.hanqi.zuoye; 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 java.util.Random; public class xianchengActivity extends AppCompatActivity { Button bt, b1,b2,b3; TextView tv ,tv2; Random r=new Random(); //定义Handler Handler h=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); //处理消息 if(msg.what==1){ String str = msg.obj.toString(); tv.setText("+"+str+"+"); } if(msg.what==2){ tv.setText("这是空消息"); } } }; int ii=10; Handler h2=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case 1: if(ii==20){ return; } ii++; tv2.setText(ii+""); //继续发送消息 h2.sendEmptyMessageDelayed(1, 1000); h2.removeMessages(2); break; case 2: if(ii==1){return;} ii--; tv2.setText(ii + ""); //继续发送消息 h2.sendEmptyMessageDelayed(2, 1000); h2.removeMessages(1); break; case 3: h2.removeMessages(1); h2.removeMessages(2); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_xiancheng); bt=(Button)findViewById(R.id.bt); b1=(Button)findViewById(R.id.b1); b2=(Button)findViewById(R.id.b2); b3=(Button)findViewById(R.id.b3); tv=(TextView)findViewById(R.id.tv); tv2=(TextView)findViewById(R.id.tv2); } public void bt1(View v){ for( int i=0;i<10;i++){ new Thread( ){ @Override public void run() { try { Thread.sleep(r.nextInt(1000)); } catch (Exception e){ } runOnUiThread(new Runnable() { @Override public void run() { bt.setText("城市1"); } }); return; } }.start(); new Thread(){ @Override public void run() { try { Thread.sleep(r.nextInt(1000)); } catch (Exception e){ } runOnUiThread(new Runnable() { @Override public void run() { bt.setText("城市2"); } }); return; } }.start(); } } public void bt2(View v){ //启动线程 new Thread( ){ @Override public void run() { //发送消息 //创建Message Message m = Message.obtain(); m.what=1;//id m.obj="我传递的信息"; //发送即时消息 h.sendMessage(m); m = Message.obtain(); m.what=1;//id m.obj="我传递的信息2"; //发送延时消息 h.sendMessageDelayed(m,3000); //发送空消息 //只发送一个what,用于判断,无需message实例 h.sendEmptyMessage(2); //延时空消息 h.sendEmptyMessageDelayed(2,3000); } }.start(); } public void bt3(View v){ //if(v.getId()==R.id.b1){} switch (v.getId()){ case R.id.b1: //发送消息 h2.sendEmptyMessage(1); b1.setEnabled(false); b2.setEnabled(true); b3.setEnabled(true); if(ii==20){ b1.setEnabled(false); b3.setEnabled(false); } break; case R.id.b2: //发送消息 h2.sendEmptyMessage(2); b1.setEnabled(true); b2.setEnabled(false); b3.setEnabled(true); if(ii==1){ b2.setEnabled(false); b3.setEnabled(false); } break; case R.id.b3: //发送消息 h2.sendEmptyMessage(3); b1.setEnabled(true); b2.setEnabled(true); b3.setEnabled(false); break; } } }
以上是关于处理信息Message-Handler的主要内容,如果未能解决你的问题,请参考以下文章
vbscript 各种自定义代码片段 - 有关详细信息,请参阅注释