Android handle
<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=".MainActivity" > <TextView
android:id="@+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_below="@id/text_id" /> </RelativeLayout>
package com.ibox365.s0725001; import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity { 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.btn);
button.setOnClickListener(new ButtonListen()); handler=new myHandle(); } class ButtonListen implements OnClickListener
{ /* (non-Javadoc)
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub Message msg=handler.obtainMessage();
msg.what=2;
handler.sendMessage(msg);
} } class myHandle extends Handler{ /* (non-Javadoc)
* @see android.os.Handler#handleMessage(android.os.Message)
*/
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg); int what =msg.what;
System.out.println(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;
} }
多线程 操作
<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"
android:orientation="vertical"
tools:context=".MainActivity" > <TextView
android:id="@+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送消息" /> </LinearLayout>
package com.ibox365.s0725002; import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends Activity { private Button button;
private TextView textView;
private Handler handler; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); button=(Button) findViewById(R.id.btn_id);
button.setOnClickListener(new ButtonListen());
textView=(TextView) findViewById(R.id.text_id);
handler=new MyHandle(); } class ButtonListen implements OnClickListener
{
@Override
public void onClick(View v) {
Thread thread=new MyThread();
thread.start();
}
}
class MyThread extends Thread{ @Override
public void run() {
System.out.println("MyThread==>"+Thread.currentThread().getName()); try {
Thread.sleep(2*1000);
} catch (InterruptedException e) { e.printStackTrace();
} String s="get data from network!";
Message msg=handler.obtainMessage();
msg.obj=s;
handler.sendMessage(msg);
}
} class MyHandle extends Handler{
@Override
public void handleMessage(Message msg) { System.out.println("MyHandle==>"+Thread.currentThread().getName());
String s=(String) msg.obj;
textView.setText(s);
}
} }
思路图: