package max.test04;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements Callback {
private TextView txt1=null;
private Button btn1=null;
private Button btn2=null;
private Handler handler;
private int i=0;
private boolean bl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt1=(TextView)this.findViewById(R.id.txt1);
btn1=(Button)this.findViewById(R.id.btn1);
btn2=(Button)this.findViewById(R.id.btn2);
btn2.setOnClickListener(btn2Click);
handler = new Handler(this);
btn1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bl=false;
}
});
}
private View.OnClickListener btn2Click= new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(new TThreadTest()).start();
}
};
@Override
public boolean handleMessage(Message msg) {
// TODO Auto-generated method stub
switch(msg.what)
{
case 0:
String s = (String)msg.obj;
// //if (msg.obj InstanceOf String)
txt1.setText(s);
// Log.d("log text", s);
break;
case 1:
break;
}
return false;
}
private class TThreadTest implements Runnable
{
@Override
public void run() {
// TODO Auto-generated method stub
//etText.setText("线程赋值");
//handler.obtainMessage(0, "线程赋值").sendToTarget();
bl=true;
while(bl){
i++;
handler.obtainMessage(0,String.valueOf(i)).sendToTarget();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}