当前位置:网站首页>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;
}
}
边栏推荐
- Programmers with ten years of development experience tell you, what core competitiveness do you lack?
- web安全与防御
- Insight into cloud native | microservices and microservice architecture
- Amq6126 problem solving ideas
- oracle修改数据库字符集
- idea查看字节码配置
- Matplotlib swordsman Tour - an artist tutorial to accommodate all rivers
- 微服务实战|声明式服务调用OpenFeign实践
- 自定義Redis連接池
- 定时线程池实现请求合并
猜你喜欢
![[go practical basis] how to customize and use a middleware in gin](/img/fb/c0a4453b5d3fda845c207c0cb928ae.png)
[go practical basis] how to customize and use a middleware in gin

微服务实战|微服务网关Zuul入门与实战

Idea view bytecode configuration
![[staff] time sign and note duration (full note | half note | quarter note | eighth note | sixteenth note | thirty second note)](/img/bf/2b0b9c640bdad2c55293f905a22055.jpg)
[staff] time sign and note duration (full note | half note | quarter note | eighth note | sixteenth note | thirty second note)

十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?

定时线程池实现请求合并

机器学习之数据类型案例——基于朴素贝叶斯法,用数据辩男女

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

破茧|一文说透什么是真正的云原生
![[go practical basis] gin efficient artifact, how to bind parameters to structures](/img/c4/44b3bda826bd20757cc5afcc5d26a9.png)
[go practical basis] gin efficient artifact, how to bind parameters to structures
随机推荐
Cartoon rendering - average normal stroke
CKEditor 4.10.1 上传图片提示“不正确的服务器响应” 问题解决
知识点很细(代码有注释)数构(C语言)——第三章、栈和队列
十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?
深入剖析JVM是如何执行Hello World的
"Redis source code series" learning and thinking about source code reading
Idea view bytecode configuration
西瓜书--第五章.神经网络
Watermelon book -- Chapter 6 Support vector machine (SVM)
Long summary (code with comments) number structure (C language) -- Chapter 4, string (Part 1)
Number structure (C language) -- Chapter 4, compressed storage of matrices (Part 2)
京东高级工程师开发十年,编写出:“亿级流量网站架构核心技术”
Pool de connexion redis personnalisé
What are the waiting methods of selenium
Knowledge points are very detailed (code is annotated) number structure (C language) -- Chapter 3, stack and queue
微服务实战|熔断器Hystrix初体验
告别996,IDEA中必装插件有哪些?
「Redis源码系列」关于源码阅读的学习与思考
Flink - use the streaming batch API to count the number of words
Microservice practice | fuse hytrix initial experience