Android 平台数字竞猜应用开发实战
应用功能概述
本文详细解析如何在 Android 生态下构建轻量级数字竞猜交互模块。核心业务涵盖:目标值动态生成、输入数值大小比对反馈、非法空值拦截校验、尝试次数递减控制以及全局状态重置机制。整体架构遵循单向数据流设计原则,确保UI层与逻辑层完全解耦。
视图层构建规范
界面采用垂直线性容器进行纵向排布,对组件标识符进行语义化重命名以提升可读性。同时补充状态提示文本域,替代硬编码的弹出式提示依赖。布局结构定义如下:
<?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="24dp">
<EditText
android:id="@+id/et_number_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入 0-9 范围整数"
android:inputType="number" />
<TextView
android:id="@+id/tv_attempt_tracker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:textColor="#2C3E50" />
<Button
android:id="@+id/btn_validate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="提交校验" />
<Button
android:id="@+id/btn_reinitialize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="初始化重开" />
</LinearLayout>
控制器逻辑实现
活动类通过实现原生点击监听接口集中管理交互路由。引入安全解析器防范类型转换异常,并将核心判定逻辑封装为独立方法。运行期状态管理采用局部变量映射,具体实现代码参考下方示例:
package com.example.guessapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class NumberGuessActivity extends AppCompatActivity implements View.OnClickListener {
private static final int ALLOWED_ROUNDS = 5;
private EditText etUserInput;
private TextView tvRoundInfo;
private Button btnSubmit;
private Button btnRefresh;
private int currentRounds = ALLOWED_ROUNDS;
private int secretTarget;
private final Random seedGenerator = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_number_guess);
mapUIComponents();
seedInitialTarget();
registerInteractionHandlers();
}
private void mapUIComponents() {
etUserInput = findViewById(R.id.et_number_input);
tvRoundInfo = findViewById(R.id.tv_attempt_tracker);
btnSubmit = findViewById(R.id.btn_validate);
btnRefresh = findViewById(R.id.btn_reinitialize);
}
private void registerInteractionHandlers() {
btnSubmit.setOnClickListener(this);
btnRefresh.setOnClickListener(this);
}
@Override
public void onClick(View clickedView) {
switch (clickedView.getId()) {
case R.id.btn_reinitialize:
executeSystemRestart();
break;
case R.id.btn_validate:
evaluateUserSubmission();
break;
}
}
private void executeSystemRestart() {
seedInitialTarget();
currentRounds = ALLOWED_ROUNDS;
refreshStatusLabel("系统已重新洗牌,剩余回合:" + currentRounds);
clearInputField();
}
private void seedInitialTarget() {
secretTarget = seedGenerator.nextInt(10);
}
private void evaluateUserSubmission() {
String rawEntry = etUserInput.getText().toString().trim();
if (TextUtils.isEmpty(rawEntry)) {
triggerNotification("数据源缺失,请输入有效字符");
return;
}
if (currentRounds <= 0) {
triggerNotification("配额已用尽,请执行初始化操作");
disableInteractionControls();
return;
}
try {
int parsedValue = Integer.parseInt(rawEntry);
decrementCounter();
if (parsedValue == secretTarget) {
triggerNotification("✔️ 匹配成功,验证通过");
lockInteractiveElements();
} else if (parsedValue < secretTarget) {
triggerNotification("📉 阈值偏低,余量:" + currentRounds);
} else {
triggerNotification("📊 阈值偏高,余量:" + currentRounds);
}
} catch (NumberFormatException parseError) {
triggerNotification("⛔ 格式校验失败,仅支持整型数值");
}
}
private void decrementCounter() {
currentRounds--;
updateProgressIndicator();
}
private void updateProgressIndicator() {
tvRoundInfo.setText("当前存活回合:" + currentRounds);
}
private void refreshStatusLabel(String displayMessage) {
tvRoundInfo.setText(displayMessage);
}
private void clearInputField() {
etUserInput.setText("");
}
private void disableInteractionControls() {
etUserInput.setEnabled(false);
btnSubmit.setEnabled(false);
}
private void lockInteractiveElements() {
disableInteractionControls();
btnRefresh.setEnabled(true);
}
private void triggerNotification(String payload) {
Toast.makeText(this, payload, Toast.LENGTH_SHORT).show();
}
}
