移动应用开发 第5讲 Activity课堂综合练习
2024-08-30 00:55:01
作业总要求
使用附件“素材”压缩包中的素材完成下列任务:
1.完成小游戏主程序,如图mainActivity.png。
2.在主程序界面当按下游戏介绍按钮时进行游戏介绍界面如图gameintroduce.png,按“返回”按钮则回到主程序界面。
3.在主程序界面按下“进入游戏”按钮则进入游戏界面如图gameScores.png,按“返回”按钮加回主界面同时将子程序界面中的游戏成绩返回到主程序并显示在主程序中如图mainActivity1.png所示。
4.提交程序运行视频
5.所有相关的Java和XML文件。
思路:
一个主页面,四个子页面
主页面
MainActivity.class
package com.example.myapplication1; import android.content.Intent;
import android.os.Bundle; import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView; public class MainActivity extends AppCompatActivity { private ImageView imageView1;
private ImageView imageView2;
private ImageView imageView3;
private ImageView imageView4;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1=findViewById(R.id.imageView);
imageView2=findViewById(R.id.imageView2);
imageView3=findViewById(R.id.imageView3);
imageView4=findViewById(R.id.imageView4);
textView=findViewById(R.id.textView);
imageView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivityForResult(new Intent(MainActivity.this,
GameintroduceActivity.class),1);
}
}); imageView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivityForResult(new Intent(MainActivity.this,
Entergame.class),2);
}
}); imageView3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivityForResult(new Intent(MainActivity.this,
Archivedownload.class),3);
}
}); imageView4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivityForResult(new Intent(MainActivity.this,
Designintroduce.class),4);
}
}); } @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 1:
{assert data != null;
String position = data.getStringExtra("position1");
textView.setText(position);
break;}
case 2:{
assert data != null;
String position = data.getStringExtra("position2");
textView.setText(position);
break;}
case 3:{
assert data != null;
String position = data.getStringExtra("position3");
textView.setText(position);
break;} case 4:{
assert data != null;
String position = data.getStringExtra("position4");
textView.setText(position);
break;} default:
{textView.setText("");
break;}
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="@drawable/gameface"
android:gravity="center"
android:orientation="vertical"> <ImageView
android:id="@+id/imageView" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/introduce" /> <ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/startgame" /> <ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/icon_load" /> <ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/icon_works" /> <TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="60dp"
android:layout_weight="4"
/>
</LinearLayout>
游戏介绍界面
GameIntroduceActivity.class
package com.example.myapplication1; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView; public class GameintroduceActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameintroduce);
textView=findViewById(R.id.textView2);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = getIntent();
String s = textView.getText().toString();
intent.putExtra("position1", s);
setResult(RESULT_OK,intent);
finish();
}
});
}
}
gameintroduce.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@drawable/game1"
tools:context=".GameintroduceActivity"> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是游戏介绍"
android:textColor="#DC143C"
android:textSize="60dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
进入游戏界面
Entergame.class
package com.example.myapplication1; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView; public class Entergame extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entergame);
textView=findViewById(R.id.textView3);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = getIntent();
String s = textView.getText().toString();
intent.putExtra("position2", s);
setResult(RESULT_OK,intent);
finish();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@drawable/game2"
tools:context=".Entergame"> <TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#DC143C"
android:textSize="60dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="获得游戏分数3600" />
</RelativeLayout>
存档界面
Archivedownload.class
package com.example.myapplication1; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView; public class Archivedownload extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_archivedownload);
textView=findViewById(R.id.textView4);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = getIntent();
String s = textView.getText().toString();
intent.putExtra("position3", s);
setResult(RESULT_OK,intent);
finish();
}
});
}
}
activity_archivedownload.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@drawable/game3"
tools:context=".Archivedownload">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#DC143C"
android:textSize="60dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="这是档案下载"/>
</RelativeLayout>
设计人员界面
Designintroduce.class
package com.example.myapplication1; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView; public class Designintroduce extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_designintroduce);
textView=findViewById(R.id.textView5);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = getIntent();
String s = textView.getText().toString();
intent.putExtra("position4", s);
setResult(RESULT_OK,intent);
finish();
}
});
}
}
activity_designintroduce.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".Designintroduce">
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#DC143C"
android:textSize="60dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="这是设计人员"/>
</RelativeLayout>
最新文章
- form表单序列化后的数据转json对象
- Leetcode 113. Path Sum II
- CentOS下httpd下php 连接mysql 本机可以,127.0.0.1不能访问
- URAL 1069 Prufer Code(模拟)
- SQL备份(全)
- atlassian-jira-confluence-bitbucket破解
- RedisTemplate.htm
- Lotus Sametime
- XenCenter注册码一年申请
- c#一个简单的实例告诉你,多继承还可以这么来
- PHP高效获取远程图片尺寸和大小
- Safe Area Layout Guide
- IDEA(jetbrain通用)优雅级使用教程(转)
- 洛谷P1164 小A点菜 DP入门
- Windows下安装pymssql
- ubuntu14简介/安装/菜鸟使用手册
- git中多账号切换问题的解决方案(转)
- Mabatis中#{}和${}的区别
- HOJ 2133&POJ 2964 Tourist(动态规划)
- maven nexus linux私服搭建