WEB开发网
开发学院手机开发Android 开发 Android消息机制 阅读

Android消息机制

 2010-09-10 00:43:00 来源:WEB开发网   
核心提示:view plaincopy to clipboardprint?public class Activity1 extends Activity implements OnClickListener{Button button = null;TextView text = null;@Overrideprotected

view plaincopy to clipboardprint?

public class Activity1 extends Activity implements OnClickListener{

Button button = null;

TextView text = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity1);

button = (Button)findViewById(R.id.btn);

button.setOnClickListener(this);

text = (TextView)findViewById(R.id.content);

}

public void onClick(View v) {

switch (v.getId()) {

case R.id.btn:

Looper looper = Looper.myLooper();//取得当前线程里的looper

MyHandler mHandler = new MyHandler(looper);//构造一个handler使之可与looper通信

//buton等组件可以由mHandler将消息传给looper后,再放入messageQueue中,同时mHandler也可以接受来自looper消息

mHandler.removeMessages(0);

String msgStr = "主线程不同组件通信:消息来自button";

Message m = mHandler.obtainMessage(1, 1, 1, msgStr);//构造要传递的消息

mHandler.sendMessage(m);//发送消息:系统会自动调用handleMessage方法来处理消息

break;

}

}

private class MyHandler extends Handler{

public MyHandler(Looper looper){

super(looper);

}

@Override

public void handleMessage(Message msg) {//处理消息

text.setText(msg.obj.toString());

}

}

}

public class Activity1 extends Activity implements OnClickListener{

Button button = null;

TextView text = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity1);

button = (Button)findViewById(R.id.btn);

button.setOnClickListener(this);

text = (TextView)findViewById(R.id.content);

上一页  1 2 3 4 5  下一页

Tags:Android 消息 机制

编辑录入:coldstar [复制链接] [打 印]
赞助商链接