Android-向下一页传递数据

 

首先创建Activity:

MainActivity:里面添加方法

package com.example.myapplication33;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private EditText et_name;
    private EditText et_age;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_name = (EditText) findViewById(R.id.et_name);//获取id
        et_age = (EditText) findViewById(R.id.et_age);
    }
    public void open(View view){
        String etname=et_name.getText().toString();//获取文本框里面的值,并转为字符串
        int etage=Integer.parseInt(et_age.getText().toString());//获取文本框里面的值,并转为字整型
        Intent intent=new Intent(this,SecondActivity.class);
        intent.putExtra("myname",etname);//使用Intent的putExtra()方法将数据存储在Intent对象中
        intent.putExtra("my_age",etage);
        startActivity(intent);
    }


    public void open1(View view) {
        Intent intent=new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.setData(Uri.parse("http://www.baidu.com"));//打开百度页面
        startActivity(intent);
    }
}

SecondActivity:通过getXxxExtra()方法接收传过来的值

package com.example.myapplication33;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class SecondActivity extends AppCompatActivity {
    private TextView tv_show;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        tv_show= (TextView)findViewById(R.id.tv_show);

        Intent intent=getIntent();
        String name=intent.getStringExtra("myname");
        int age=intent.getIntExtra("my_age",0);

        tv_show.setText(name+"     "+age);
    }
}

布局文件activity_main.xml:添加控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_name"
        android:hint="请输入名字:"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_age"
        android:hint="请输入年龄:"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下一页"
        android:onClick="open"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="百度一下"
        android:onClick="open1"/>


</LinearLayout>

布局文件activity_second.xml:添加一个<TextView>控件,用来接收传过来的值

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_show"
        />

</LinearLayout>

结果:

 

 

点击下一页:

 

点击百度一下:

 

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵俺第一专栏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值