1、打开新的Activity,不传递参数
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, HelloActivity.class);//HellActivity为新的Activity
startActivity(intent);
}
});
}
2、打开新的Activity并传递参数,用Bundle传值:
MainActivity.java:
package com.example.intent;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
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 {
private Button button;
private EditText editText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到Button
button = (Button) findViewById(R.id.button);
//找到Text
editText = (EditText) findViewById(R.id.text);
OnClickListener onClickListener = new OnClickListener() {
public void onClick(View v) {
//取得EditText的值
String text = editText.getText().toString();
//新建一个Bundle
Bundle bundle = new Bundle();
//赋值
bundle.putString("text", text);
Intent intent = new Intent();
intent.setClass(MainActivity.this, HelloActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
};
button.setOnClickListener(onClickListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
在HelloActivity.java中接收参数
package com.example.intent;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
public class HelloActivity extends Activity {
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello);
textView = (TextView) findViewById(R.id.textview);
//得到当前的Intent
Intent intent = this.getIntent();
//得到Bundle
Bundle bundle = intent.getExtras();
//通过key得到value
String text = bundle.getString("text");
//赋值给textview
textView.setText(text);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_hello, menu);
return true;
}
}
传参的第二种方式:
MainActivity.java:
package com.example.intent;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
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 {
private Button button;
private EditText editText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到Button
button = (Button) findViewById(R.id.button);
//找到Text
editText = (EditText) findViewById(R.id.text);
OnClickListener onClickListener = new OnClickListener() {
public void onClick(View v) {
//取得EditText的值
String text = editText.getText().toString();
Intent intent = new Intent();
intent.setClass(MainActivity.this, HelloActivity.class);
intent.putExtra("text",text);
startActivity(intent);
}
};
button.setOnClickListener(onClickListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
在HelloActivity.java中接收参数
package com.example.intent;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
public class HelloActivity extends Activity {
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello);
textView = (TextView) findViewById(R.id.textview);
//得到当前的Intent
Intent intent = this.getIntent();
//通过key得到value
String text = intent.getStringExtra("text");
//赋值给textview
textView.setText(text);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_hello, menu);
return true;
}
}