微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

移动应用开发 第5讲 Activity课堂综合练习

作业总要求
使用附件“素材”压缩包中的素材完成下列任务:

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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐