当前位置:网站首页>Activity的创建和跳转
Activity的创建和跳转
2022-07-02 06:34:00 【FF小迷糊吖~】
任务要求:
需要提交所有类的Java源文件代码和布局文件代码(xml文件),注意:提交Java源文件代码时选择代码语言为Java,提交布局文件代码时选择代码语言为XML
任务描述:
按要求实现如下程序效果,图一为程序主界面。
(1)点击界面中“请登录”跳转到登录界面,在登录界面点击“本机号码一键登录”按钮时返回程序主界面,并且将用户输入的手机号码返回给主界面并显示在会员名的位置,刚才显示的“请登录”改为“已登录”;
(2)点击主界面中的“常用信息”跳转到常用信息界面,跳转时需将所有信息(姓名、手机号、邮箱、地址,需要将这些属性封装到Information类中,采用对象的方式传递数据)传递到该界面进行显示。
程序中所需图片资源接:https://pan.baidu.com/s/1T624eEXyt9kFSfvdGuGdhw 提取码:p9g9




AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.homework07">
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="旅程" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".LoginActivity" android:label="登录"></activity>
<activity android:name=".InformationActivity" android:label="常用信息"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_margin="20dp">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/photo"/>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical">
<TextView android:id="@+id/tv_user" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="尊敬的会员" android:textColor="@android:color/white" android:textSize="23sp" android:textStyle="bold" android:layout_marginTop="5dp"/>
<TextView android:id="@+id/tv_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="请登录" android:textColor="@android:color/white" android:textSize="15sp"/>
</LinearLayout>
</LinearLayout>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我的工具" android:textSize="26sp" android:textColor="#737373" android:layout_marginTop="10dp"/>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white" android:padding="15dp">
<LinearLayout android:id="@+id/information" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_margin="10dp">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@drawable/info"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="常用信息" android:textSize="16sp" android:layout_marginTop="10dp"/>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_margin="10dp">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@drawable/video"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="直播活动" android:textSize="16sp" android:layout_marginTop="10dp"/>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_margin="10dp">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@drawable/outside"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="出行清单" android:textSize="16sp" android:layout_marginTop="10dp"/>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_margin="10dp">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@drawable/travel"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旅行足迹" android:textSize="16sp" android:layout_marginTop="10dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
login_layout.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">
<ImageView android:layout_width="120dp" android:layout_height="120dp" android:src="@drawable/photo" android:layout_marginTop="80dp" android:layout_gravity="center_horizontal"/>
<EditText android:id="@+id/phoneNum" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" android:hint="请输入本机号码" android:layout_margin="16dp"/>
<Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:text="本机号码一键登录" android:textColor="@android:color/white" android:background="#FF8800"/>
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:text="其他登录方式" android:textColor="#737373" android:background="#FAFAFA" android:layout_marginTop="10dp"/>
</LinearLayout>
info_layout.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">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名:" android:textSize="20sp"/>
<EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="手机号码:" android:textSize="20sp"/>
<EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="邮箱:" android:textSize="20sp"/>
<EditText android:id="@+id/et_email" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="地址:" android:textSize="20sp"/>
<EditText android:id="@+id/et_address" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp"/>
</LinearLayout>
</LinearLayout>
MainActivity.java:
package com.example.homework07;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView loginTextView;
private TextView user;
private final int LOGIN_REQUEST = 99;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout information = findViewById(R.id.information);
loginTextView = findViewById(R.id.tv_login);
user = findViewById(R.id.tv_user);
loginTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Activity跳转
Intent intent = new Intent();
//设置目的Activity类
intent.setClass(MainActivity.this,LoginActivity.class);
//跳转到新的Activity,返回响应
startActivityForResult(intent,LOGIN_REQUEST);
}
});
information.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Activity跳转
Intent intent = new Intent();
//设置目的Activity类
intent.setClass(MainActivity.this,InformationActivity.class);
//传递自定义类型的对象
Information info = new Information("王一博","189****1234","[email protected]","中国");
intent.putExtra("info",info);
//跳转到新的Activity,没有返回响应
startActivity(intent);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 100 && requestCode == LOGIN_REQUEST){
String phoneNum = data.getStringExtra("phoneNum");
//考虑现实生活手机号的位数为11位
if(!phoneNum.isEmpty() && phoneNum.length()== 11){
user.setText(phoneNum);
loginTextView.setText("已登录");
}
}
}
}
LoginActivity.java:
package com.example.homework07;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_layout);
final EditText text = findViewById(R.id.phoneNum);
Button loginBtn = findViewById(R.id.btn_login);
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//返回数据
Intent response = new Intent();
response.putExtra("phoneNum",text.getText().toString());
//响应
setResult(100,response);
//结束当前的LoginActivity
finish();
}
});
}
}
InformationActivity.java:
package com.example.homework07;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
public class InformationActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info_layout);
//接受从MainActivity传递的数据
Intent request = getIntent();
//获取自定义类型的数据
Information information = (Information) request.getSerializableExtra("info");
String name = information.getName();
String phone = information.getPhone();
String email = information.getEmail();
String address = information.getAddress();
EditText nameET = findViewById(R.id.et_name);
EditText phoneET = findViewById(R.id.et_phone);
EditText emailET = findViewById(R.id.et_email);
EditText addressET = findViewById(R.id.et_address);
nameET.setText(name);
phoneET.setText(phone);
emailET.setText(email);
addressET.setText(address);
}
}
Information.java:
package com.example.homework07;
import java.io.Serializable;
//使用Intent传递该类型对象时,需要让该类实现Serializable接口
public class Information implements Serializable {
private String name;
private String phone;
private String email;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Information() {
}
public Information(String name, String phone, String email, String address) {
this.name = name;
this.phone = phone;
this.email = email;
this.address = address;
}
}
边栏推荐
- Watermelon book -- Chapter 5 neural network
- 微服务实战|微服务网关Zuul入门与实战
- [go practical basis] how to customize and use a middleware in gin
- What are the differences between TP5 and laravel
- 双非本科生进大厂,而我还在底层默默地爬树(上)
- Matplotlib剑客行——初相识Matplotlib
- 机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案
- How to choose between efficiency and correctness of these three implementation methods of distributed locks?
- 《统计学习方法》——第五章、决策树模型与学习(上)
- 概率还不会的快看过来《统计学习方法》——第四章、朴素贝叶斯法
猜你喜欢

Redis 序列化 GenericJackson2JsonRedisSerializer和Jackson2JsonRedisSerializer的区别

Learn combinelatest through a practical example
![[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)](/img/ae/1ecb352c51a101f237f244da5a2ef7.jpg)
[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)

【Go实战基础】gin 如何设置路由

Chrome user script manager tempermonkey monkey

Microservice practice | load balancing component and source code analysis

During MySQL installation, mysqld Exe reports that the application cannot start normally (0xc000007b)`

Solutions to Chinese garbled code in CMD window

Matplotlib swordsman Tour - an artist tutorial to accommodate all rivers

zk配置中心---Config Toolkit配置与使用
随机推荐
记录下对游戏主机配置的个人理解与心得
使用IBM MQ远程连接时报错AMQ 4043解决思路
Oracle modifies tablespace names and data files
微服务实战|熔断器Hystrix初体验
长篇总结(代码有注释)数构(C语言)——第四章、串(上)
Solution to amq4036 error in remote connection to IBM MQ
How to choose between efficiency and correctness of these three implementation methods of distributed locks?
Chrome浏览器标签管理插件–OneTab
【Go实战基础】如何安装和使用 gin
Idea view bytecode configuration
Bold prediction: it will become the core player of 5g
远程连接IBM MQ报错AMQ4036解决方法
Flink - use the streaming batch API to count the number of words
idea查看字节码配置
Pdf document of distributed service architecture: principle + Design + practice, (collect and see again)
Beats (filebeat, metricbeat), kibana, logstack tutorial of elastic stack
[go practical basis] how can gin get the request parameters of get and post
Say goodbye to 996. What are the necessary plug-ins in idea?
Number structure (C language -- code with comments) -- Chapter 2, linear table (updated version)
Chrome视频下载插件–Video Downloader for Chrome