Handler用Bundle传值

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;
            }
        }
    };

}

posted on 2013-10-21 22:37  大米稀饭  阅读(396)  评论(0编辑  收藏  举报