安卓隐式 Intent 实例(做一个超级简单测试小小游戏)

最近上安卓课程,抱着认真、专注的态度去学习安卓、去做安卓实验。这是第三个实验,前两个简单没什么好说的。这次是做一个测试(检测)的小小游戏。主要是应用了隐式 Intent 的跳转,本次有踩坑,最后会提到,望以此为戒。

一、实验工具:1.jdk 2. Android Studio 3.夜神模拟器(安卓可以用真机,我是ios不能真机运行,具体操作请百度)。

二、所需知识:1.一些基础ui、基础组件属性、基本布局(这些是很基础的,不会,推荐学bilbil 飞哥的安卓课程)。 2.[Intent] (https://www.jianshu.com/p/67d99a82509b)。 3.安卓模板选简洁的。

三、开始实验

1. MainActivity.java

主要思想是通过EditText、RadioGroup获取用户姓名、性别等数据。监听Button,当Button被点击的时候,将姓名、性别通过隐式Intent传递到新页面。并注册返回值监听,等待新页面返回测试所得的参数、结果。

代码:

package com.example.work2;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button btn1;
    TextView text2;
    RadioGroup rg;
    EditText input1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = findViewById(R.id.btn_1);
        input1  = findViewById(R.id.input1);
        rg = findViewById(R.id.rg);
        final String[] str = {"boy"};
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {


            }
        });

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //    Intent = new Intent(MainActivity.this,MainActivity2.class);
            //       startActivity(intent);

                rg = findViewById(R.id.rg);
                int checkedId = rg.getCheckedRadioButtonId();

                Intent = new Intent("mark");
                Bundle = new Bundle();
                bundle.putString("name", String.valueOf(input1.getText()));
                switch (checkedId){
                    case R.id.r_btn1 :
                        bundle.putString("sex" , "boy");
                        break;
                    case R.id.r_btn2 :
                        bundle.putString("sex" , "girl");
                        break;
                }

                intent.putExtras(bundle);
            //    startActivity(intent);
                startActivityForResult(intent,1);  //当打开的页面关闭时,将回调方法,第二次参数为回调码,用于区分不同回调的页面


            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        text2 = findViewById(R.id.text2);
        text2.setBackgroundColor(Color.parseColor("#6666FF"));
        Toast.makeText(MainActivity.this,"测试成功!快快测试结果吧!仅供娱乐哦",Toast.LENGTH_LONG).show();
        text2.setText(data.getExtras().getString("title"));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("Destroy", "onDestroy: 主actitvity已被销毁");
    }
}

2. 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"
    tools:context=".MainActivity"
    android:padding="20dp"
    android:orientation="vertical"
    >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="20dp">
                <TextView
                    android:id="@+id/a_text"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:textSize="17sp"
                    android:paddingTop="10dp"
                    android:text="姓名:"
                    ></TextView>
                <EditText
                    android:id="@+id/input1"
                    android:layout_toRightOf="@+id/a_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#03C4F4"
                    android:hint="请输入姓名"
                    ></EditText>
        </RelativeLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:text="性别:"
            ></TextView>
        <RadioGroup
            android:id="@+id/rg"
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:layout_height="50dp"
            android:paddingBottom="20dp"
            >

                <RadioButton
                    android:id="@+id/r_btn1"
                    android:layout_width="wrap_content"
                    android:text="男"
                    android:checked="true"
                    android:layout_height="match_parent" />

                <RadioButton
                    android:id="@+id/r_btn2"
                    android:layout_width="wrap_content"
                    android:text="女"
                    android:layout_height="match_parent" />

        </RadioGroup>

        <Button
            android:id="@+id/btn_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="点击这里进入测试"
            android:background="#03F433"
            android:textSize="16sp"
            ></Button>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="17sp"
            android:layout_marginTop="20dp"
            android:text="测试结果:"
            ></TextView>

        <TextView
            android:id="@id/text2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="您还未进行测试哦,请进入测试"
            android:textSize="19sp"
            android:padding="20dp"
            android:background="#F45F36"
            >
        </TextView>


</LinearLayout>

效果图:
在这里插入图片描述

3. MainActivity2.java

主要思想是获取前一个传过的用户名字、性别,再进行测试,获得所需的数据(即两个RadioGroup获得)。监听Button,当按钮被点击时,将结果返回给MainActivity1,达到测试的目的。

代码:

package com.example.work2;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.HashMap;

public class MainActivity2 extends AppCompatActivity {
    TextView text1;
    Button btn2;
    String content = "亲爱的";
    String turn = "";
    RadioGroup rg3;
    RadioGroup rg2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        text1 = findViewById(R.id.text1);
        final Bundle = getIntent().getExtras();

        if(bundle.getString("sex").equals("boy")){//对比不能用==
            content += bundle.getString("name") + "男士,请根据提示选择您心中答案:";
        }else{
            content += bundle.getString("name") + "女士,请根据提示选择您心中答案:";
        }
        text1.setText(content);

        rg2 = findViewById(R.id.rg2);
        rg3 = findViewById(R.id.rg3);

        rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {


            }
        });


        rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {


            }
        });

        btn2 = findViewById(R.id.btn_2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap map = new HashMap();
                map.put("str1","心地善良,热爱动物,活泼可爱");
                map.put("str2",",热爱运动,阳光热情");
                int selet2 = rg2.getCheckedRadioButtonId();
                switch (selet2){
                    case R.id.r_2_btn1 :
                        map.put("str1","活泼可爱,心地善良,热爱动物");
                        break;
                    case R.id.r_2_btn2 :
                        map.put("str1","勇敢果决,心地善良,热爱动物");
                        break;
                    case R.id.r_2_btn3 :
                        map.put("str1","勇敢果决,心地善良,热爱动物");
                        break;
                    case R.id.r_2_btn4 :
                        map.put("str1","喜欢自由自在,不需要动物陪伴,自强不息");
                        break;
                }
                int selet3 = rg3.getCheckedRadioButtonId();
                switch (selet3){
                    case R.id.r_3_btn1 :
                        map.put("str2",",热爱运动,阳光热情。");
                        break;
                    case R.id.r_3_btn2 :
                        map.put("str2",",热爱运动,跳跃洒脱。");
                        break;
                    case R.id.r_3_btn3 :
                        map.put("str2",",热爱运动,认真专注。");
                        break;
                    case R.id.r_3_btn4 :
                        map.put("str2",",洁身自好,想法独特。");
                        break;
                }
                Intent = new Intent();
                Bundle bundle2 = new Bundle();
                turn = bundle.getString("name")+"你," + map.get("str1") + map.get("str2");
                bundle2.putString("title",turn);
                intent.putExtras(bundle2);
                setResult(Activity.RESULT_OK,intent);
                finish(); //关闭页面;
            }
        });

    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        Intent = new Intent();
        Bundle bundle2 = new Bundle();
        bundle2.putString("title","未能点击测试按钮,测试失败!");
        intent.putExtras(bundle2);
        setResult(Activity.RESULT_OK,intent);
        finish();
        return super.dispatchKeyEvent(event);
    }
}

4.activity_main2

`<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="未接收到参数"
    android:textSize="18sp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"></TextView>


<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:text="您更喜欢以下哪个动物?"
    android:layout_marginTop="20dp"
    ></TextView>
<RadioGroup
    android:id="@+id/rg2"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="50dp"
    android:paddingBottom="20dp"
    >

    <RadioButton
        android:id="@+id/r_2_btn1"
        android:layout_width="wrap_content"
        android:text="兔子"
        android:checked="true"
        android:paddingRight="10dp"
        android:layout_height="match_parent" />

    <RadioButton
        android:id="@+id/r_2_btn2"
        android:layout_width="wrap_content"
        android:text="狗"
        android:paddingRight="10dp"
        android:layout_height="match_parent" />

    <RadioButton
        android:id="@+id/r_2_btn3"
        android:layout_width="wrap_content"
        android:text="猫"
        android:paddingRight="10dp"
        android:layout_height="match_parent" />

    <RadioButton
        android:id="@+id/r_2_btn4"
        android:layout_width="wrap_content"
        android:text="都不喜欢"
        android:paddingRight="10dp"
        android:layout_height="match_parent" />
</RadioGroup>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:text="您更喜欢以下哪个运动"
    ></TextView>
<RadioGroup
    android:id="@+id/rg3"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="50dp"
    android:paddingBottom="20dp"
    >

    <RadioButton
        android:id="@+id/r_3_btn1"
        android:layout_width="wrap_content"
        android:text="篮球"
        android:paddingRight="10dp"
        android:checked="true"
        android:layout_height="match_parent" />

    <RadioButton
        android:id="@+id/r_3_btn2"
        android:layout_width="wrap_content"
        android:text="羽毛球"
        android:paddingRight="10dp"
        android:layout_height="match_parent" />

    <RadioButton
        android:id="@+id/r_3_btn3"
        android:layout_width="wrap_content"
        android:text="兵乓球"
        android:paddingRight="10dp"
        android:layout_height="match_parent" />

    <RadioButton
        android:id="@+id/r_3_btn4"
        android:layout_width="wrap_content"
        android:text="都不喜欢"
        android:layout_height="match_parent" />

</RadioGroup>


<Button
    android:id="@+id/btn_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="测试"
    android:background="#FFEB3B"
    ></Button>

效果图(左图):
在这里插入图片描述

四、 踩坑:

1.本来想用 setOnCheckedChangeListener 方法来动态存储选项的,我觉得是好习惯(将计算机资源平均分配了)。想法很好,但是这个方法传的是一个匿名内部类,里面的值根本传不出来。所以作罢,改成现在的方式。

2.intent传递Bundle,要用putExtras方法,而不是putExtras。这里纠结了几分钟。

3.忘了。

当然,以上的坑是我基础不牢固所造成的。

五、总结 : 这仅仅是一个小小游戏,自娱自乐的,望海涵。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值