***Activity的传值
*第一种:Intent传值
主xml文件中设置按钮,点击跳转:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_name"
android:onClick="send"/> //设置点击跳转事件
主Java代码中传值:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//点击按钮传递数据到指定activity中
public void send(View view){
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
intent.putExtra("name", "张三");//putExtra(String key,value):key表示唯一标示当前值的键,value为要传的数据
intent.putExtra("age", 20);
intent.putExtra("sex", '男');
intent.putExtra("bl", true);
startActivity(intent);
}
}
目标活动的xml布局中添加textview来显示获取的值,目标活动的java代码中获取值:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
tv = (TextView) findViewById(R.id.tv);
//1.获取激活组件的intent对象
Intent intent = getIntent();
//2.根据key获取传递的数据
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 0);//getIntExtra(表示获取数据的key,如果根据key没有获得数据时显示的默认值)
char sex = intent.getCharExtra("sex", ' ');
boolean bl = intent.getBooleanExtra("bl", false);
//3.将数据显示到TextView中
tv.setText("name:"+name+"age:"+age+"sex:"+sex+"bl:"+bl);
}
*第二种:Bundle传值
public void send(View view){
//1.创建Intent意图对象
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
//2.创建bundle对象,用来存储需要传递的数据
Bundle bundle = new Bundle();
//3.将需要传递的数据存储到bundle对象中
bundle.putString("name", "凤姐");
bundle.putChar("sex", '女');
bundle.putInt("age", 20);
//4.将bundle对象存储到intent对象中
intent.putExtras(bundle);
//5.启动activity
startActivity(intent);
}
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
tv = (TextView) findViewById(R.id.tv);
//1.获取激活组件的intent对象
Intent intent = getIntent();
//2.获取传递的bundle的对象
Bundle bundle = intent.getExtras();
//3.在bundle中根据key获取传递的数据
String name = bundle.getString("name");
int age = bundle.getInt("age");
char sex = bundle.getChar("sex");
//4.将数据显示到TextView中
tv.setText("name:"+name+"age:"+age+"sex:"+sex);
}
*第三种:使用Application全局对象传值
1.单独创建一个类继承Application,将需要存储的数据定义,并提供set及get方法
2.在发送的activity中存储数据
3.在接收的activity中获取数据
4.在清单文件中进行注册
<application
android:name="自定义application的包名.类名">
</application>
public class MyApplication extends Application {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class MainActivity extends Activity {
private MyApplication application;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//点击按钮传递数据到目标activity中
public void send(View view){
application = (MyApplication) getApplication();
application.setName("小明");
application.setAge(18);
startActivity(new Intent(MainActivity.this, OtherActivity.class));
}
}
public class OtherActivity extends Activity {
private TextView tv;
private MyApplication application;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
tv = (TextView) findViewById(R.id.tv);
application = (MyApplication) getApplication();
String name = application.getName();
int age = application.getAge();
tv.setText("name=" +name +"age="+age);
}
}
<application
android:name="com.example.creatactivity.MyApplication">
</application>
*第四种:启动Activity回传数据
案例演示:
public class MainActivity extends Activity {
private EditText et_num1,et_num2;
private TextView tv_result;
private static final int REQUEST_CODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_num1 = (EditText) findViewById(R.id.editText1);
et_num2 = (EditText) findViewById(R.id.editText2);
tv_result = (TextView) findViewById(R.id.textView3);
}
//点击按钮传递数据到目标activity中
public void onClick(View view){
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
String num1 = et_num1.getText().toString();
String num2 = et_num2.getText().toString();
intent.putExtra("num1", num1);
intent.putExtra("num2", num2);
startActivityForResult(intent, REQUEST_CODE);//startActivityForResult(请求的intent对象,大于0的整数请求码)
}
/*
* 该方法用来处理setResult()方法回传的数据
* int requestCode,请求码
* int resultCode,结果码
* Intent data,回传的intent意图对象
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE&&resultCode==Activity.RESULT_OK){
String result = data.getStringExtra("info");
tv_result.setText(result);
}
}
}
public class OtherActivity extends Activity {
private TextView tv_num1,tv_num2;
private EditText et_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
tv_num1 = (TextView) findViewById(R.id.tv1);
tv_num2 = (TextView) findViewById(R.id.tv3);
et_result = (EditText) findViewById(R.id.et1);
Intent intent = getIntent();
tv_num1.setText(intent.getStringExtra("num1"));
tv_num2.setText(intent.getStringExtra("num2"));
}
//点击按钮将数据回传到主activity
public void send(View view){
String result = et_result.getText().toString();//获取结果数据
Intent intent = new Intent();
intent.putExtra("info", result);
setResult(Activity.RESULT_OK, intent);//setResult(表示请求的结果码,表示回传的数据的intent对象)
OtherActivity.this.finish();//关闭当前的activity
}
}