<?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:orientation="vertical">
<!-- 上下两部分-->
<!-- 答题区-->
<!-- 题目-->
<TextView
android:id="@+id/text_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="窗前明月光,下一句是什么?"
/>
<!-- edittext写答案-->
<EditText
android:id="@+id/edit_answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入答案"
/>
<!-- 下一题按钮-->
<Button
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
/>
<!-- 角色信息区 -->
<!-- 角色名-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="角色名:无名"
/>
<!-- 剑法名称-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="武学:唐诗剑法"
/>
<!-- 剑法等级-->
<TextView
android:id="@+id/text_level"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="剑法等级:第一重"
/>
</LinearLayout>
package com.example.myapplication5;
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;
public class SongActivity3 extends AppCompatActivity implements View.OnClickListener {
private TextView text_question;
private EditText edit_answer;
private Button btn_submit;
private TextView text_level;
private int index=0;//当前到哪一题
String[] answers={"a","b","c"};
String[] questions={"窗前明月光,下一句是什么?","飞流直下三千尺,下一句是什么?","大漠孤烟直,下一句是什么?"};
String[] level_names={"","第一重","第二重","第三重","第四重","第五重","第六重","第七重","第八重","第九重"};
private int level=1;//剑法等级
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_song3);
text_question = findViewById(R.id.text_question);
edit_answer = findViewById(R.id.edit_answer);
btn_submit = findViewById(R.id.btn_submit);
text_level = findViewById(R.id.text_level);
//点击提交按钮,核对答案
btn_submit.setOnClickListener(this);
}
@Override
public void onClick(View view) {
//如果点击的是提交按钮
switch (view.getId()){
case R.id.btn_submit:
//点击提交按钮,核对答案
String str_ans = edit_answer.getText().toString();
//都要修改数据,
if(answers[index].equals(str_ans)){
//答对
updateData(true);
}else{
//答错
updateData(false);
}
refreshInterface();
//用新数据渲染页面
break;
}
}
void updateData(boolean levelUp){
//修改题目索引
index++;
//剑法升级或者保持不变
if(levelUp){
level++;
}
}
//重新渲染界面
void refreshInterface(){
edit_answer.setText("");
text_question.setText(questions[index]);
text_level.setText("剑法等级:"+level_names[level]);
}
}
题外话:银行卡余额变动短信提醒或许用微信通知更加实惠一点。