当前位置: 代码迷 >> Android >> android学习札记 Intent 一个Activity当中的值传到另一个Activity
  详细解决方案

android学习札记 Intent 一个Activity当中的值传到另一个Activity

热度:66   发布时间:2016-05-01 20:19:58.0
android学习笔记 Intent 一个Activity当中的值传到另一个Activity

\\第一个Activity中给Intent添加键值对

public void onClick(View view) {
??int buttonId = view.getId();???
??switch (buttonId) {
??case R.id.button_1:
???String editText1Str = editText1.getText().toString();
???String editText2Str = editText2.getText().toString();
???Intent intent = new Intent();
???intent.putExtra("one", editText1Str);
???intent.putExtra("two", editText2Str);
???intent.setClass(Activity01.this, ResultActivity.class);
???Activity01.this.startActivity(intent);
???break;

}

\\第二个Activity中用键还取值

protected void onCreate(Bundle savedInstanceState) {
??super.onCreate(savedInstanceState);
??setContentView(R.layout.result);
??TextView textView = (TextView) findViewById(R.id.textview_2);
??Intent intent = getIntent();
??
??String oneStr = intent.getStringExtra("one");
??String twoStr = intent.getStringExtra("two");
??int one=Integer.parseInt(oneStr);
??int two=Integer.parseInt(twoStr);
??int result = one * two;
??textView.setText(result + "");

  相关解决方案