APP页面之添加页面 && 传递参数方法

文章介绍了在Android开发中如何创建新的Activity,包括通过右键新建、设置Activity和Layout名称,以及使用Intent进行页面跳转。同时,详细讲解了两种传递参数的方法:一是通过Intent的putExtra方法,二是使用Bundle对象来传递多个参数。在接收端,通过getIntent和getExtras方法获取并显示传递的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

添加一个页面

  1. 在src上面右键,new一个Other
  2. 选择Android,Android Activity
  3. 选择Blank Activity
  4. 更改Activity Name(类的名字),Layout Name自动更改(布局名字),点击Finish完成
    在这里插入图片描述

页面跳转

  1. 在java文件中,Intent类 ,new一个对象,设置要跳转的页面构造方法
Intent intent = new Intent(this, SecondActivity.class);

// 第一个参数当前页面,第二个参数跳转到哪个页面
  1. 跳转
startActivity(intent);
package com.example.ymsz.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void goSecond(View v){
    	// Intent 设置要跳转的页面
        Intent intent = new Intent(this, SencondActivity.class);
        //跳转
        startActivity(intent);
    }
}

传递参数方法一

Intent类的方法putExtra

putExtra,参数1键值队(接收的时候需要这个参数),参数2传递的信息(可以是很多种数据类型)

intent.putExtra("MyData", "法外狂徒");
package com.example.ymsz.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void goSecond(View v){
    	// Intent 设置要跳转的页面
        Intent intent = new Intent(this, SencondActivity.class);
        intent.putExtra("MyData", "法外狂徒");
        //跳转
        startActivity(intent);
    }
}

接收
  1. 使用Intent
Intent i = this.getIntent();
  1. 用一个数据类型变量,把键里面的值获取出来
data = i.getStringExtra("Mydata");
  1. 把获取的数据显示在页面
Toast.makeText(this, "第二个页面收到数据," + data, 0).show();

具体代码:

package com.example.ymsz.activity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class SencondActivity extends Activity {

	private String data;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sencond);
		
		Intent i = this.getIntent();
		data = i.getStringExtra("MyData");
		Toast.makeText(this, "第二个页面收到数据," + data, 0).show();
	}
	public void goThird(View v){
    	// Intent 设置要跳转的页面
        Intent intent = new Intent(this, ThirdActivity.class);
        intent.putExtra("MyData", data);
        //跳转
        startActivity(intent);
    }
}

传递参数方法二

传递参数类型多了,就需要用这种方法

  1. 使用Bundle类,可以传递很多参数
Intent intent = new Intent(this, SencondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("MyData", "法外狂徒");
bundle.putInt("ID", 100);

intent.putExtras(bundle);
  1. 接收方法和第一种一样
Intent i = this.getIntent();
Bundle b = i.getExtras();
String data = b.getString("MyData");
int data2 = b.getInt("ID");

Toast.makeText(this, "第二个页面收到数据," + data + data2, 0).show();

页面一

package com.example.ymsz.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void goSecond(View v){
    	// Intent 设置要跳转的页面
        Intent intent = new Intent(this, SencondActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("MyData", "法外狂徒");
        bundle.putInt("ID", 100);
        
        intent.putExtras(bundle);
        //intent.putExtra("MyData", "法外狂徒");
        //跳转
        startActivity(intent);
    }
}

页面二

package com.example.ymsz.activity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class SencondActivity extends Activity {

	private String data;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sencond);
		
		Intent i = this.getIntent();
		// data = i.getStringExtra("MyData");
		Bundle b = i.getExtras();
		String data = b.getString("MyData");
		int data2 = b.getInt("ID");

		Toast.makeText(this, "第二个页面收到数据," + data + data2, 0).show();
	}
	public void goThird(View v){
    	// Intent 设置要跳转的页面
        Intent intent = new Intent(this, ThirdActivity.class);
        intent.putExtra("MyData", data);
        //跳转
        startActivity(intent);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值