activity传参数与跳转
来源:互联网 发布:python交换两个值 编辑:程序博客网 时间:2024/05/16 19:56
acitvity 开发
A、配置文件
<activity android:name=".OtherActivity" android:label="otherActivity">
</activity>
B、编写布局文件
C、写一个类继承activity
重写onCreate
在这个方法中设置布局文件setContentView(R.layout.other);
activity传参数
//意图传参数方法一
//intent.putExtra("name", "谭建平");
//intent.putExtra("age", 25);
//意图传参数方法二
Bundle bundle=new Bundle();
bundle.putString("name", "谭建平");
bundle.putInt("age", 25);
intent.putExtras(bundle);
Intent intent= this.getIntent();//得到激活他的意图
//方式一获得参数
String name=intent.getStringExtra("name");
int age=intent.getIntExtra("age", 0);
//方式二获得参数
int age=intent.getExtras().getInt("age");
String name=intent.getExtras().getString("name");
设置要激活的组件
Intent intent=new Intent(MainActivity.this, OtherActivity.class);//为intent设置要激活的组件
//写法一:intent.setClass(MainActivity.this, OtherActivity.class);//设置要激活的组件
//写法二:intent.setComponent(new ComponentName(MainActivity.this,OtherActivity.class));//设置要激活的组件
当打开的activity关闭的时候返回值
打开activity的方式为
startActivityForResult(intent, 10);//打开的意图关闭的时候 10为请求吗
被打开的activity关闭的时候
Intent intent2=new Intent();
intent2.putExtra("result", "这是关闭返回的结果");
setResult(10, intent2);//10为返回码
finish();//关闭activity
打开的activity重写
/**
* 打开的activity关闭的时候
* requestCode请求码
* resultCode返回码
*请求码和返回码是用于区分跳转的时候多个activity
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(this, data.getStringExtra("result"), Toast.LENGTH_SHORT).show();
super.onActivityResult(requestCode, resultCode, data);
}
private OnClickListener calrBMI = new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("key_height", fieldHeight.getText().toString());
bundle.putString("key_weight", fieldWeight.getText().toString());
intent.setClass(ActivityMain.this,Report.class);
intent.putExtras(bundle);
startActivity(intent);
}
};
接收方:
Bundle bundle = new Bundle();
bundle = this.getIntent().getExtras();
double height = Double.parseDouble(bundle.getString("key_height"))/100;
double weight = Double.parseDouble(bundle.getString("key_weight"));
- activity传参数与跳转
- Android应用之Activity传参数与跳转
- Activity跳转和传参数
- Activity跳转与传值
- Activity跳转与传值
- Activity跳转与传值
- Activity跳转与传值
- Activity跳转与传值
- Activity : 跳转与传值
- 页面跳转与传值(Activity跳转与传值)
- 页面跳转与传值(Activity跳转与传值)
- 页面跳转与传值(Activity跳转与传值)
- 页面跳转与传值(Activity跳转与传值)
- 页面跳转与传值(Activity跳转与传值)
- Activity的跳转与传值
- Activity的跳转与传值
- Activity的跳转与传值
- Activity的跳转与传值
- ios 发送http协议请求消息
- Jquery动态增加行和删除行
- iOS应用的真机调试
- 11级_Java_曹建波6.07 MySQL语言结构
- Python编写FTP下载程序
- activity传参数与跳转
- EZ-USB 68013学习笔记(二)——USB协议之传输的基本单元--包
- 投影仪的使用
- Android入门第八篇之GridView(九宫图)
- JDBCl连接各种数据库
- Python SMTP协议发送QQ邮件
- openstack中 novnc配置相关选项 VNC配置
- Android入门第七篇之ListView (二)
- IOS开发系列——xml文件解析