在Android Studio中用.xml和.java实现界面跳转-大学Android实验

本文介绍了两个Android应用页面(activity_main.xml和activity_main2.xml)的布局设计,使用ConstraintLayout管理和RadioButton、ImageView的选择,以及如何通过Intent在两个Activity间传递用户的选择,包括图片资源的处理。
摘要由CSDN通过智能技术生成

一.代码

1. activity_main.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=".MainActivity">

   <TextView
       android:id="@+id/textView"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       app:layout_constraintBottom_toTopOf="@+id/radioGroup"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toTopOf="parent" />

   <RadioGroup
       android:id="@+id/radioGroup"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginTop="50dp"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toBottomOf="@+id/textView">

       <RadioButton
           android:id="@+id/rb1"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="青海湖"
           tools:ignore="HardcodedText" />

       <RadioButton
           android:id="@+id/rb2"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="日月山" />

       <RadioButton
           android:id="@+id/rb3"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="无图片"
           tools:ignore="HardcodedText" />
   </RadioGroup>

   <Button
       android:id="@+id/bt4"
       android:layout_width="250dp"
       android:layout_height="50dp"
       android:layout_marginTop="10dp"
       android:text="下一步"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toStartOf="parent"
       tools:ignore="HardcodedText" />

</androidx.constraintlayout.widget.ConstraintLayout>

2. activity_main2.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=".MainActivity2">

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical">

       <TextView
           android:id="@+id/textView"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:textSize="20dp"
           android:layout_marginTop="10dp"
           android:layout_marginLeft="5dp"
           tools:ignore="RtlHardcoded,SpUsage" />

       <ImageView
           android:id="@+id/imageView"
           android:layout_width="match_parent"
           android:layout_height="270dp"
           android:scaleType="fitStart"
           android:layout_marginTop="10dp"
           android:layout_marginLeft="5dp"
           android:layout_marginRight="5dp"
           tools:ignore="ContentDescription" />

       <Button
           android:id="@+id/bt2"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="返 回"
           android:textSize="20dp"
           android:layout_marginTop="10dp"
           tools:ignore="HardcodedText,SpUsage" />

   </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

3. MainActivity.java

package com.example.pagechange;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
   RadioGroup radioGroup;
   RadioButton radioButton;
   TextView textView;
   Button button;

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

       radioGroup = findViewById(R.id.radioGroup);
       button = findViewById(R.id.bt4);

       final Intent intent = new Intent(this, MainActivity2.class);

       radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(RadioGroup radioGroup, int i) {
               if (i == R.id.rb1) {
                   intent.putExtra("ID", "rb1");
               } else if (i == R.id.rb2) {
                   intent.putExtra("ID", "rb2");
               } else {
                   intent.putExtra("ID", "rb3");
               }
           }
       });

       button.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               startActivity(intent);
           }
       });
   }
}

4. MainActivity2.java

package com.example.pagechange;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {
   TextView textView;
   ImageView imageView;
   Button button;

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

       textView = findViewById(R.id.textView);
       imageView = findViewById(R.id.imageView);

       Intent intent = getIntent();
       String i = intent.getStringExtra("ID");

       assert i != null;
       if (i.equals("rb1")) {
           textView.setText("欣赏迷人的:青海湖");
           imageView.setImageResource(R.drawable.img1);
       } else if (i.equals("rb2")) {
           textView.setText("欣赏迷人的:日月山");
           imageView.setImageResource(R.drawable.img2);
       } else {
           textView.setText("~~~");
           imageView.setImageResource(R.drawable.img3);
       }

       button = findViewById(R.id.bt2);
       button.setOnClickListener(view -> {
           Intent backIntent = new Intent(MainActivity2.this, MainActivity.class);
           startActivity(backIntent);
       });
   }
}

二. 模拟器运行结果

059bc158ada047a882432ac15d375874.png

ea646348f9734f8897521121ebda9d2f.png

f394a23cddb84d8d907cb5d950259c03.png

c6695c1c89fe4ddf94f12b4ae4d0bc46.png

b67925ab92ba4a33ae9f4ddd249f260a.png

a7c920d958b54b93b4165271726974f8.png

 注意:

         图片要自己找图下载并重命名,本实验的三个图片分别是命名为 img1.jpg,img2.jpg,img3.jpg;(当然可以自己命名,注意代码中名字都对应上就可以哈)

         图片放的位置是:app-res-drawable中,可以直接拖拽图片到drawable松开,图片自动存储到此位置。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值