package com.handler.cn; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.Toast; public class main extends Activity { private Button myButton01, myButton02; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myButton01 = (Button) findViewById(R.id.Button01); myButton02 = (Button) findViewById(R.id.Button02); myButton01.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { myHandler.sendEmptyMessage(0); } }); myButton02.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { Message myMessage = new Message(); myMessage.what = 1; Bundle myBundle = new Bundle(); myBundle.putString("name", "刘馨妍"); myBundle.putString("number", "1234567890"); myMessage.setData(myBundle); myHandler.sendMessage(myMessage); } }); } // 创建Handler类的实例 Handler myHandler = new Handler() { public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 0: Toast.makeText(main.this, "结果是:" + msg.what, Toast.LENGTH_LONG) .show(); break; case 1: Bundle myBundle1 = msg.getData(); String myName = myBundle1.getString("name"); String myNumber = myBundle1.getString("number"); Toast.makeText( main.this, "姓名:" + myName + " ,号码:" + myNumber + " ,结果是:" + msg.what, Toast.LENGTH_LONG).show(); break; } } }; }
Handler用Bundle传值
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Android Handler机制
Handler机制
ide 赋值 构造方法 -
Intent传值和Bundle传值
Android 为我们提供了两个比较常用的传值对象:Intent 和 Bundle ;用途:一般情况下,我们在 Activity 中传值用 Intent ,而
Intent传值和Bundle传值 Intent传值 Bundle传值 bundle Android -
handler之bundle
handler之bundle
bundle handler