当前位置: 网学 > 编程文档 > Android > 正文

android 在两个Activity之间互传数据

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 12/10/19
下载{$ArticleTitle}原创论文样式

新建工程:

public class Training_TransActivity extends Activity implements OnClickListener

2.定义widget

private EditText mText;
private Button mSend;
private String mString;

3.实现

mText=(EditText)findViewById(R.id.edit);
mText.setCursorVisible(false);
mSend=(Button)findViewById(R.id.send);
mSend.setOnClickListener(this);

4。定义另一个Activity

public class Jump extends Activity implements OnClickListener{
private TextView mDisplay;
private Button mBack;
Intent intent;
Bundle bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activitytwo);

mDisplay=(TextView)findViewById(R.id.dis);
mBack=(Button)findViewById(R.id.back);
intent=this.getIntent();
bundle=intent.getExtras();
String textString=bundle.getString("text");
mDisplay.setText(textString);
mBack.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bundle.putBoolean("reply", true);
intent.putExtras(bundle);
this.setResult(1, intent);
this.finish();
}

}


5,原来Activity修改代码:

public void onClick(View v) {
// TODO Auto-generated method stub
if(v==mSend){
mString=mText.getText().toString();
Intent intent=new Intent();
Bundle bundle=new Bundle();
bundle.putString("text", mString);
intent.putExtras(bundle);
intent.setClass(this, Jump.class);
startActivityForResult(intent, 0);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode) {
case 1:
Bundle bundle=data.getExtras();
Boolean flag;
flag=bundle.getBoolean("reply");
if(flag == true){
mText.setText("the other received my data!");
}else if(flag ==false){
mText.setText("the other not received my data!!!");
}
break;
default:
break;
}
}


6.修改androidmanifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wistron.trainingtransactivity"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Training_TransActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Jump"></activity>
</application>
<uses-sdk android:minSdkVersion="5" />

</manifest>
 

  • 上一篇资讯: [Android]
  • 网学推荐

    免费论文

    原创论文

    浏览:
    设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
    版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
    湘ICP备09003080号