当前位置:网站首页>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;
}
}
边栏推荐
- oracle修改数据库字符集
- Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度
- 微服务实战|原生态实现服务的发现与调用
- Attributes of classfile
- Demand delineation executive summary
- Programmers with ten years of development experience tell you, what core competitiveness do you lack?
- CKEditor 4.10.1 上传图片提示“不正确的服务器响应” 问题解决
- What are the waiting methods of selenium
- 长篇总结(代码有注释)数构(C语言)——第四章、串(上)
- Statistical learning methods - Chapter 5, decision tree model and learning (Part 1)
猜你喜欢
Enterprise level SaaS CRM implementation
Dix ans d'expérience dans le développement de programmeurs vous disent quelles compétences de base vous manquez encore?
西瓜书--第五章.神经网络
Servlet全解:继承关系、生命周期、容器和请求转发与重定向等
自定義Redis連接池
Chrome user script manager tempermonkey monkey
【Go实战基础】gin 如何获取 GET 和 POST 的请求参数
数构(C语言)——第四章、矩阵的压缩存储(下)
How to use pyqt5 to make a sensitive word detection tool
[staff] time sign and note duration (full note | half note | quarter note | eighth note | sixteenth note | thirty second note)
随机推荐
How to use pyqt5 to make a sensitive word detection tool
Redis zadd导致的一次线上问题排查和处理
Timed thread pool implements request merging
Oracle modifies tablespace names and data files
Insight into cloud native | microservices and microservice architecture
一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
idea查看字节码配置
机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案
Idea view bytecode configuration
【Go实战基础】gin 如何绑定与使用 url 参数
Oracle modify database character set
Knife4j 2.X版本文件上传无选择文件控件问题解决
Solutions to Chinese garbled code in CMD window
概念到方法,绝了《统计学习方法》——第三章、k近邻法
VIM operation command Encyclopedia
Microservice practice | Eureka registration center and cluster construction
数构(C语言--代码有注释)——第二章、线性表(更新版)
Demand delineation executive summary
记录下对游戏主机配置的个人理解与心得
十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?