实操·会员验证+猜数游戏

内容:实现流程:打开 APP 先显示验证页 → 输入正确会员密码 → 跳转到猜数字游戏页。

第一步 创建项目

  1. 在“ New Project ”界面,选择Empty Views Activity(空活动,最简单的模板),点击“ Next ”。
  2. 填写项目信息:
    • Name(项目名):GuessTheNumbers
    • Package name(包名):默认即可(类似com.example.Guessthenumber
    • Save location(保存路径):选一个自己能找到的文件夹
    • Language(语言):选Java(更基础)
    • Minimum SDK(最低支持的安卓版本):选默认的“ API 24 ”(覆盖大多数手机)
  3. 点击“ Finish ”,等待项目加载完成(第一次可能会久一点,看网络)。

第二步 设计会员验证页面布局(activity_login.xml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="50dp"
>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="会员验证"
android:textColor="@color/black"
android:textSize="30sp"
android:gravity="center"
android:layout_marginTop="40dp"
android:layout_marginBottom="20dp"
/>

<EditText
android:id="@+id/et_vip_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:hint="请输入会员密码"
android:inputType="textPassword"
android:layout_marginTop="30dp"
/>

<Button
android:id="@+id/btn_vip_check"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击验证"
android:textSize="20sp"
android:layout_marginTop="15dp"
/>

<TextView
android:id="@+id/tv_vip_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="20dp"
/>
</LinearLayout>

第三步 会员验证页面逻辑(LoginActivity.java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.example.guessthenumbers;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {

private EditText etVipPassword;
private Button btnVipCheck;
private TextView tvVipResult;

private static String CORRECT_PASSWORD="vip666";
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// 加载布局
setContentView(R.layout.activity_login);
// 绑定控件(通过ID关联布局中的元素)
etVipPassword = findViewById(R.id.et_vip_password);
btnVipCheck = findViewById(R.id.btn_vip_check);
tvVipResult = findViewById(R.id.tv_vip_result);

// 按钮点击事件:验证密码并跳转
btnVipCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 1. 获取用户输入的密码
String inputPassword = etVipPassword.getText().toString().trim();

// 2. 验证逻辑
if (inputPassword.isEmpty()) {
// 输入为空
tvVipResult.setText("请输入会员密码");
} else if (inputPassword.equals(CORRECT_PASSWORD)) {
// 密码正确:跳转到猜数字页面
tvVipResult.setText("验证成功,正在进入游戏...");

// 创建跳转意图(从当前页到游戏页)
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent); // 执行跳转

// 关闭当前验证页(可选:防止返回键回到验证页)
finish();
} else {
// 密码错误
// (正确密码:vip666)
tvVipResult.setText("密码错误,请重新输入");
// 清空输入框
etVipPassword.setText("");
}
}
});
}
}

第四步 设计猜数游戏页面布局(activity_main.xml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="50dp"
android:orientation="vertical"
>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="猜数游戏"
android:textSize="30sp"
android:textColor="@color/black"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:layout_marginBottom="20dp"
/>

<EditText
android:id="@+id/et_game_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:textSize="20sp"
android:hint="请输入0-100之间的数字"
android:layout_marginTop="30dp"
android:layout_marginBottom="10dp"
/>

<Button
android:id="@+id/btn_game_check"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击验证"
android:textSize="20sp"
android:background="#CECECE"
android:layout_marginTop="15dp"
/>

<TextView
android:id="@+id/tv_game_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="20dp"
/>

</LinearLayout>

第五步 猜数游戏逻辑(MainActivity.java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.example.guessthenumbers;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

// 控件声明
private EditText etGameNumber; // 数字输入框
private Button btnGameCheck; // 猜测按钮
private TextView tvGameResult; // 结果提示

// 游戏变量
private int targetGameNumber; // 目标随机数
private int minRange = 0; // 当前范围最小值
private int maxRange = 100; // 当前范围最大值

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 加载布局
setContentView(R.layout.activity_main);

// 绑定控件
etGameNumber = findViewById(R.id.et_game_number);
btnGameCheck = findViewById(R.id.btn_game_check);
tvGameResult = findViewById(R.id.tv_game_result);

// 生成0-100的随机数(游戏初始化)
targetGameNumber = new Random().nextInt(101); // 0-100闭区间

// 猜测按钮点击事件
btnGameCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 1. 获取用户输入的数字
String input = etGameNumber.getText().toString().trim();

// 2. 输入验证
if (input.isEmpty()) {
tvGameResult.setText("请输入数字后再猜~");
return;
}

// 3. 转换为整数(处理非数字输入)
int userNumber;
try {
userNumber = Integer.parseInt(input);
} catch (NumberFormatException e) {
tvGameResult.setText("请输入有效的数字!");
etGameNumber.setText(""); // 清空错误输入
return;
}

// 4. 检查数字范围
if (userNumber < 0 || userNumber > 100) {
tvGameResult.setText("请输入0-100之间的数字!");
return;
}

// 5. 核心逻辑:对比用户输入与目标数字
if (userNumber == targetGameNumber) {
// 猜对了
tvGameResult.setText("恭喜你!猜对了!🎉");
} else if (userNumber < targetGameNumber) {
// 猜小了:更新最小值
minRange = userNumber;
tvGameResult.setText("猜小了~ 范围:" + minRange + " 到 " + maxRange);
} else {
// 猜大了:更新最大值
maxRange = userNumber;
tvGameResult.setText("猜大了~ 范围:" + minRange + " 到 " + maxRange);
}

// 6. 清空输入框,方便下次输入
etGameNumber.setText("");
}
});
}
}

第六步 配置文件(AndroidManifest.xml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/taikongren"
android:label="@string/app_name"
android:roundIcon="@mipmap/taikongren"
android:supportsRtl="true"
android:theme="@style/Theme.GuessTheNumbers"
tools:targetApi="31">

<activity
android:name=".LoginActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="true"/>
</application>

</manifest>

完整流程测试步骤

  1. 运行 APP,首先显示「会员验证页」。

    效果如下图所示:

    会员验证界面

  2. 输入正确密码 vip666(可在 LoginActivity 中修改)。

  3. 点击「验证并进入游戏」,自动跳转到「猜数字游戏页」。

    猜数游戏界面

  4. 在游戏页输入数字猜测,系统会提示范围(如 “猜小了~ 范围:20 到 100”)。

  5. 猜对后显示 “恭喜你!猜对了!🎉”。

核心技术点说明

  • 页面跳转:通过 Intent 实现两个页面的切换,startActivity(intent) 触发跳转。
  • 启动页设置AndroidManifest.xml 中,LoginActivity 包含 <intent-filter> 标记,因此成为 APP 打开的第一个页面。
  • 数据验证:两个页面都添加了输入检查(空输入、格式错误等),提升用户体验。
  • 生命周期管理finish() 方法关闭验证页,防止用户按返回键回到验证页(可选逻辑)。
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2025 唐小唐
  • 访问人数: | 浏览次数: