![Handler(一)--MarsChen Android 教程学习笔记 - Roller44 - 病友说 Handler(一)--MarsChen Android 教程学习笔记 - Roller44 - 病友说](http://img1.ph.126.net/vze780ncBFQhzpIqV-LxKg==/6608790560608543912.png)
<RelativeLayout 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.shumin.lbs08.MainActivity" >
<Button
android:id="@+id/button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="BUTTON"/>
</RelativeLayout>
package com.shumin.lbs08;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
private Button button;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button);
ButtonListener listener=new ButtonListener();
button.setOnClickListener(listener);
handler=new FirstHandler();
}
class ButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
Message msg=handler.obtainMessage();
msg.what=44;
handler.sendMessage(msg);
}
}
class FirstHandler extends Handler{
@Override
public void handleMessage(Message msg) {
System.out.println("消息是:"+(int)msg.what);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}