首页 > 代码库 > 带数据的activity跳转
带数据的activity跳转
public class MainActivity extends Activity {
TextView tv;
EditText et;
Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.tv);
et = (EditText)findViewById(R.id.et);
bt = (Button)findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
if(et.getText().length()>0) //如果et存在数据
{
Bundle bundle = new Bundle(); //定义bundle
bundle.putString("username", et.getText().toString()); //将et饿的数据放入bundle
intent.putExtras(bundle); //将bundle放入intent
}
startActivity(intent); //启动项目activity
}
});
}
}
public class SecondActivity extends Activity {
TextView tv;
Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
tv = (TextView)findViewById(R.id.tv);
Intent intent = getIntent(); //接收穿来的activity数据
Bundle bundle = intent.getExtras(); //接收穿来的intent里的bundle
String username = bundle.getString("username").toString(); //定义username
tv.setText("unername: " + username); //tv输出内容
}
}
若有不足之处,欢迎指出。
带数据的activity跳转