android handler 学习
16lz
2021-01-23
package pos.app;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.DialogInterface;import android.content.Intent;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.widget.Button;public class Android extends Activity {/** Called when the activity is first created. */@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.home); Button helpButton =(Button) findViewById(R.id.login_help); helpButton.setOnClickListener(new Button.OnClickListener(){@Overridepublic void onClick(View v) {Intent intent = new Intent(AndroidApp.this,MyHandlerActivity.class);startActivity(intent);} });}}
package pos.app;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Button;
public class MyHandlerActivity extends Activity {
Button button;
MyHandler myHandler;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
setTitle("My Handler Activity");
button = (Button)findViewById(R.id.login_help);
myHandler = new MyHandler();
MyThread m = new MyThread();
new Thread(m).start();
}
class MyHandler extends Handler {
public MyHandler(){
}
public MyHandler( Looper L){
super(L);
}
@Override
public void handleMessage(Message msg){
Log.v("MyHandler", "handleMessage....");
super.handleMessage(msg);
Bundle bundle = msg.getData();
String text = bundle.getString("text");
MyHandlerActivity.this.button.setText(text);
}
}
class MyThread implements Runnable {
@Override
public void run() {
try{
Thread.sleep(1000);
}catch(Exception e){
}
Message msg = new Message();
Bundle bundle = new Bundle();
bundle.putString("text", "handler text");
msg.setData(bundle);
MyHandlerActivity.this.myHandler.sendMessage(msg);
}
}
}
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer