android bundle 简单传值

android | 2016-01-01 16:41:40 | 阅读 1276 次 | 评论(0)
package com.example.postvalue;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
	private Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setClass(MainActivity.this, SecondPage.class);
				Bundle bundle = new Bundle();
				bundle.putString("用户名:", "张三");
				bundle.putString("密码:", "888888");
				intent.putExtras(bundle);
				MainActivity.this.startActivity(intent);
			}

		});

		Toast.makeText(this, "准备发送!?", 3000).show();

	}

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

}
/*******************************/
package com.example.postvalue;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

public class SecondPage extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		Bundle bunde = this.getIntent().getExtras();
		String eml = bunde.getString("用户名:");  
		String pwd = bunde.getString("密码:");    
		
		Toast.makeText(this,"user:"+eml+"  password:"+pwd, 3000).show();
		
	}

}


/***********************************************/
ps:   布局文件加个按钮,  别忘记在Manifest增加配制
<activity
            android:name="com.example.postvalue.SecondPage"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="SecondPage" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
文章评论,共0条
游客请输入验证码
浏览175766次
最新评论
  • zhouwenyuan:博主可以开发房产APP吗?
  • qunxingw:结合附件,在分表A或B...实验一下宏就理解了
  • qunxingw:这仅是小范围的一种思路,此题是指数级的数据。