Intent的基本操作
intent的跳转activity和得到返回信息
发送的activity Send.java 和receive.java
package com.jackie.intentproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Send extends Activity {
private Button but=null;
private TextView msg=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.send);
this.but=(Button) super.findViewById(R.id.but);
this.msg=(TextView) super.findViewById(R.id.msg);
this.but.setOnClickListener(new OnClickListenerImpl());
}
public class OnClickListenerImpl implements OnClickListener{
@Override
public void onClick(View arg0) {
Intent intent=new Intent(Send.this,Receive.class);
intent.putExtra("myInfo", "深圳百度科技股份有限公司(www.baidu.com)");
Send.this.startActivityForResult(intent, 1);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(resultCode){
case RESULT_OK:
Send.this.msg.setText("返回的内容是:"+data.getStringExtra("retmsg"));
break;
case RESULT_CANCELED:
break;
default:
break;
}
}
}
package com.jackie.intentproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Receive extends Activity {
private TextView show=null;
private Button retBut=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.receive);
this.show=(TextView) super.findViewById(R.id.show);
this.retBut=(Button) super.findViewById(R.id.retBut);
Intent intent=super.getIntent(); //取得当前的intent
String info=intent.getStringExtra("myInfo");
this.show.setText(info);
this.retBut.setOnClickListener(new OnClickListenerImpl());
}
public class OnClickListenerImpl implements OnClickListener{
@Override
public void onClick(View arg0) {
Receive.this.getIntent().putExtra("retmsg", "职员:王尼玛");
Receive.this.setResult(RESULT_OK, Receive.this.getIntent());
Receive.this.finish();
}
}
}
<h2>每个activity文件都有两个xml布局文件 新的activity都需要在AdroidManifest.xm里面注册</h2><div> <activity
android:name="com.jackie.intentproject.Receive"
android:label="@string/receive_name"/>
</div>