首先创建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>
结果:
点击下一页:
点击百度一下: