<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.lbs09.MainActivity" >
<Button
android:id="@+id/button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="BUTTON"/>
</RelativeLayout>
package com.shumin.lbs09;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
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(new OnClickListener() {
@Override
public void onClick(View v) {
Message msg=handler.obtainMessage();
handler.sendMessage(msg);
System.out.println("onClick------>"+Thread.currentThread().getName());
}
});
// button.setOnClickListener(listener);
WorkerThread wt=new WorkerThread();
wt.start();
}
/* class ButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
Message msg=handler.obtainMessage();
handler.sendMessage(msg);
System.out.println("onClick------>"+Thread.currentThread().getName());
}
}*/
class WorkerThread extends Thread{
@Override
public void run(){
Looper.prepare();
handler=new Handler(){
@Override
public void handleMessage(Message msg){
System.out.println("WorkerThread------>"+Thread.currentThread().getName());
System.out.println("收到消息!");
}
};
Looper.loop();
}
}
@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);
}
}