android activity Intent 传值 传对象 (1),头条三面技术四面 HR
android activity Intent 传值 传对象
==============================
话不多说,直接上代码,简单粗暴
main。java
package com.yqy.yqy_intenttest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText et_content;
Button btn;
EditText et_name, et_sex;
Button btn_object;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(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().toStr
ing());
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");
评论