android activity Intent 传值 传对象,移动智能终端的发展趋势
EditText et_name, et_sex;
Button btn_object;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContent
View(R.layout.activity_main);
// 传值
et_content = (EditText) findViewById(R.id.et_content);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(MainActivity.this, BActivity.class);
i.putExtra("content", et_content.getText().toString());
startActivity(i);
}
});
// 传对象
et_name = (EditText) findViewById(R.id.et_name);
et_sex = (EditText) findViewById(R.id.et_sex);
btn_object = (Button) findViewById(R.id.btn_object);
btn_object.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
MyInfoBean mib = new MyInfoBean(""
et_name.getText().toString(), ""
et_sex.getText().toString());
Bundle bd = new Bundle();
bd.putSerializable("mib", mib);
Intent i = new Intent(MainActivity.this, OActivity.class);
i.putExtras(bd);
startActivity(i);
}
});
}
@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;
}
}
2.传值接收 B.java
package com.yqy.yqy_intenttest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class BActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
//接收值
String content = getIntent().getStringExtra("content");
TextView tv_show = (TextView) findViewById(R.id.tv_show);
tv_show.setText(content);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.b, menu);
return true;
}
}
3.传递对象? 实体类? o。java
package com.yqy.yqy_intenttest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class OActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_o);
Intent i = getIntent();
Bundle bd = i.getExtras();
MyInfoBean mib = (MyInfoBean) bd.getSerializable("mib");
TextView tv_name = (TextView) findViewById(R.id.tv_name);
TextView tv_sex = (TextView) findViewById(R.id.tv_sex);
tv_name.setText(mib.getName());
tv_sex.setText(mib.getSex());
评论