Android入门小实验

17 篇文章 1 订阅
5 篇文章 0 订阅

从一个Activity页面启动另一个Activity页面。

1、页面布局:activity.xml

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

    <Button
        android:id="@+id/btnStartAnotherAty"
        android:layout_width="247dp"
        android:layout_height="75dp"
        android:text="启动另一个Activity" />
</LinearLayout>

2、java文件:mainactivity.java

package com.example.test1;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_main);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btnStartAnotherAty).setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //在onClick()中启动另一个Activity

                startActivity(new Intent(MainActivity.this,AnotherAty.class));
            }
        });

    }
}

3、页面布局:activity_another.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="match_parent"
    tools:context=".AnotherAty">

    <TextView
        android:id="@+id/textView"
        android:layout_width="153dp"
        android:layout_height="35dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="172dp"
        android:text="这是另一个Activity"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

4、java文件:anotheraty

package com.example.test1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class AnotherAty extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_another_aty);
    }
}

依次完成四个文件即可

运行结果:
在这里插入图片描述
在这里插入图片描述
当我们在完成上面小功能后,可以尝试着传送一下消息!

因为我很懒!所以直接在上面代码改的,有些地方为了适配代码略有改动。

【页面布局:activity.xml】

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

    <EditText
        android:id="@+id/ediText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <requestFocus />
        </EditText>
    <Button
        android:id="@+id/btnStartAnotherAty"
        android:layout_width="247dp"
        android:layout_height="75dp"
        android:text="启动另一个Activity" />

【java文件:mainactivity.java】

package com.example.test1;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_main);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btnStartAnotherAty).setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //在onClick()中启动另一个Activity
                Intent intent =new Intent();
                intent.setClass(MainActivity.this,AnotherAty.class);
                EditText txt =(EditText)findViewById(R.id.ediText1);
                Bundle bundle= new Bundle();
                bundle.putString("text",txt.getText().toString());
                intent.putExtras(bundle);
                startActivity(intent);
                //startActivity(new Intent(MainActivity.this,AnotherAty.class));
            }
        });

    }
}

【页面布局:activity_another.xml】

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="match_parent"
    tools:context=".AnotherAty">

    <TextView
        android:id="@+id/TextView2"
        android:layout_width="153dp"
        android:layout_height="35dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="172dp"

        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

【java文件:anotheraty】

package com.example.test1;

import androidx.appcompat.app.AppCompatActivity;

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

public class AnotherAty extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_another_aty);
        TextView txt2=(TextView)findViewById(R.id.TextView2);
        Bundle bunde =this.getIntent().getExtras();

        String str =bunde.getString("text");
        txt2.setText(str);

    }
}

运行结果:
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程饱饱吃得好饱

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

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

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

打赏作者

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

抵扣说明:

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

余额充值