心如止水 宠辱不惊

坚持 专注 兴趣 激情

Acitivty间数据的传递

MainActivity.java
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public void click(View v){
    	Intent intent = new Intent(this, SecondActivity.class);
    	//把数据封装至intent对象中
//    	intent.putExtra("malename", "李志");
//    	intent.putExtra("femalename", "芙蓉姐姐");
    	
    	//把数据封装至bundle对象中
    	Bundle bundle = new Bundle();
    	bundle.putString("malename", "李志");
    	bundle.putString("femalename", "芙蓉姐姐");
    	
    	//把bundle对象封装至intent对象中
    	intent.putExtras(bundle);
    	startActivity(intent);
    }
    
}


SecondActivity.java

public class SecondActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		
		Intent intent = getIntent();
		//从intent对象中把封装好的数据取出来
//		String maleName = intent.getStringExtra("malename");
//		String feMaleName = intent.getStringExtra("femalename");
		
		Bundle bundle = intent.getExtras();
		String maleName = bundle.getString("malename");
		String feMaleName = bundle.getString("femalename");
		
		Random rd = new Random();
		int yinyuan = rd.nextInt(100);
		
		TextView tv = (TextView) findViewById(R.id.tv);
		tv.setText(maleName + "和" + feMaleName + "的姻缘值为" + yinyuan);
	}
}


阅读更多
个人分类: Android
相关热词: acitivty
上一篇Activity间的跳转
下一篇Activity的四种启动模式
想对作者说点什么? 我来说一句

Qt窗体间传递数据的三种模型

2015年09月23日 7KB 下载

没有更多推荐了,返回首页

关闭
关闭