Android - Pass data to next page

 

First create an Activity:

MainActivity: add method inside

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: Receive the passed value through the getXxxExtra() method

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);
    }
}

Layout file activity_main.xml: add controls

<?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>

Layout file activity_second.xml: Add a <TextView> control to receive the passed value

<?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>

result:

 

 

Click on the next page:

 

Click on Baidu:

 

 

Guess you like

Origin blog.csdn.net/dengfengling999/article/details/123692754