涉及方法:
setOnClickListener
Intent
intent.putExtra
startActivityForResult
intent.getStringExtra
getIntent
setResult
第一个activity的内容
package com.mz.Demo01;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class First extends Activity {
private Button btn_s=null;
private TextView tv_ret=null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Log.i("第一个activity","onCreate");
btn_s=(Button)this.findViewById(R.id.btn_send);
tv_ret=(TextView)this.findViewById(R.id.tv_return);
btn_s.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(First.this,Second.class);
intent.putExtra("weather", "天气好吗?");
//First.this.startActivity(intent);
First.this.startActivityForResult(intent, 1);//跳转到second后,second返回内容
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("第一个activity","onDestroy");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.i("第一个activity","onPause");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.i("第一个activity","onRestart");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.i("第一个activity","onResume");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.i("第一个activity","onStart");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.i("第一个activity","onStop");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch (resultCode) {
case RESULT_OK:
tv_ret.setText("返回的数据是:"+data.getStringExtra("answer"));
break;
case RESULT_CANCELED:
tv_ret.setText("操作取消");
break;
default:
break;
}
}
}
第二个activity内容
package com.mz.Demo01;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Second extends Activity {
private TextView tv_rec;
private Button btn_ret;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
tv_rec=(TextView)findViewById(R.id.tv_receive);
btn_ret=(Button)findViewById(R.id.btn_send2);
Intent intent=this.getIntent();//获得Intent对象
String info=intent.getStringExtra("weather");
tv_rec.setText("接收到的内容是:"+info);
btn_ret.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent it=getIntent();
//第2个Activity跳转到第1个Activity执行的操作
Second.this.getIntent();
it.putExtra("answer", "今天天气很好");
Second.this.setResult(RESULT_OK,it);//回传操作码和数据
Second.this.finish();
}
});
}
}
注意:有一点,在创建新的activity之后需要在
AndroidManifest中写入新的activity
<activity
android:label="@string/app_name"
android:name=".Second" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
也可以如图添加
实现后,演示如下: