如何在Android开发中activity之间数据传递
1个回答
2016-10-17 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
从一个Activity(IntentDemo)跳转到另外一个Activity(Other),其中利用Intent来传递数据
程序Demo如下:
IntentDemo.java
package com.android.intentdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class IntentDemo extends Activity {
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initComponent();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(IntentDemo.this, Other.class);
// 在Intent中传递数据
intent.putExtra("name", "AHuier");
intent.putExtra("age", 22);
intent.putExtra("address", "XiaMen");
// 启动Intent
startActivity(intent);
}
});
}
private void initComponent() {
button = (Button) findViewById(R.id.button);
}
}
other.java
package com.android.intentdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Other extends Activity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
initComponent();
Intent intent = getIntent();
int age = intent.getIntExtra("age", 0);
String name = intent.getStringExtra("name");
String address = intent.getStringExtra("address");
textView.setText("My age is " + age + "\n" + "My name is " + name + "\n" + "My address "
+ address);
}
private void initComponent() {
textView = (TextView) findViewById(R.id.msg);
}
}
程序Demo如下:
IntentDemo.java
package com.android.intentdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class IntentDemo extends Activity {
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initComponent();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(IntentDemo.this, Other.class);
// 在Intent中传递数据
intent.putExtra("name", "AHuier");
intent.putExtra("age", 22);
intent.putExtra("address", "XiaMen");
// 启动Intent
startActivity(intent);
}
});
}
private void initComponent() {
button = (Button) findViewById(R.id.button);
}
}
other.java
package com.android.intentdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Other extends Activity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
initComponent();
Intent intent = getIntent();
int age = intent.getIntExtra("age", 0);
String name = intent.getStringExtra("name");
String address = intent.getStringExtra("address");
textView.setText("My age is " + age + "\n" + "My name is " + name + "\n" + "My address "
+ address);
}
private void initComponent() {
textView = (TextView) findViewById(R.id.msg);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询