public void onClick(View v) {
Intent i=new Intent(MainActivity.this,NewActivity.class);
i.putExtra("data","我是要传递的字符串");
startActivity(i);
SecondActivity
super.onCreate(savedInstanceState); setContentView(R.layout.activity_new); Intent i=getIntent(); T=(TextView) findViewById(R.id.textView1); T.setText(i.getStringExtra("data")); }
FirstActivity
btn0.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, SecondlyActivity.class); startActivityForResult(intent, 5); } });
获取返回的值:
protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) {
case 1: if (resultCode == RESULT_OK) {
K1 = data.getFloatExtra("redk",0);//根据传值的类型,data.getFloatExtra可以是data.getStringExtra 或其他 B1 = data.getFloatExtra("redb",0); K2 = data.getFloatExtra("grek",0); B2 = data.getFloatExtra("greb",0); K3 = data.getFloatExtra("bluk",0); B3 = data.getFloatExtra("blub",0); break;
传回的K,B,直接使用就可以。
SecondActivity
btn3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(); 写入要传入的值: intent.putExtra("redk", 1); intent.putExtra("redb", 2); intent.putExtra("grek", 3); intent.putExtra("greb", 4); intent.putExtra("bluk", 5); intent.putExtra("blub", 6); setResult(RESULT_OK,intent); finish(); } });