1.Intent的putExtra()方法有
public Intent putExtra(String name, boolean value);
public Intent putExtra(String name, byte value);
public Intent putExtra(String name, char value);
public Intent putExtra(String name, short value);
public Intent putExtra(String name, int value);
public Intent putExtra(String name, long value);
public Intent putExtra(String name, float value);
public Intent putExtra(String name, double value);
public Intent putExtra(String name, String value);
public Intent putExtra(String name, CharSequence value);
public Intent putExtra(String name, Parcelable value);
public Intent putExtra(String name, Parcelable[] value);
2.Activity传值与接收数据
2.1 传值
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edit);
button = (Button) findViewById(R.id.button1);
String txt = editText.getText().toString();
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String string = editText.getText().toString();
Intent intent = new Intent(MainActivity.this, main.class);
intent.putExtra("name", string);
startActivity(intent);
}
});
}
2.2 接收数据
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.txt2);
Intent intent = getIntent();
String string = intent.getStringExtra("name");
textView.setText(string);
}
3.Activity用Bundle传值
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edit);
button = (Button) findViewById(R.id.button1);
String txt = editText.getText().toString();
button.setOnClickListener(newOnClickListener() {
@Override
publicvoidonClick(View v) {
String string = editText.getText().toString();
Intent intent =newIntent(MainActivity.this, main.class);
Bundle bundle =newBundle();
bundle.putString("name", string);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
3.1接收Bundle 的Activity
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.txt2);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String string = bundle.getString("name");
textView.setText(string);
}