新开activity并且新旧窗口之间传值
首先在清单文件中配置组件,在application节点下增加
<activity android:name=".otherActivity" android:label="@string/activity_other" ></activity>
新建activity类 并且继承activity 重写oncreate方法
package com.example.mulactivity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class otherActivity extends Activity { private EditText editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.other); editor=(EditText) findViewById(R.id.editText1); //获取用于激活该窗口的意图对象 Intent intent=getIntent(); editor.setText(intent.getStringExtra("name")); } public void close(View view){ //设置返回数据 setResult(30, new Intent().putExtra("return", "哈哈哈")); this.finish(); } }
在原先activity中设置意图并传值
package com.example.mulactivity; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void show(View view){ //设置要激活的组件 Intent intent=new Intent(this, otherActivity.class); //向意图中传入值 intent.putExtra("name", "张杰"); //打开窗口 //startActivity(intent); //打开窗口并能够接受返回数据 startActivityForResult(intent, 200); } //当新窗口返回结果时该方法调用 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Toast.makeText(this.getApplicationContext(), data.getStringExtra("return"), 1).show(); } }