5.9
与小组成员讨论如何完善咨询中的自我测试页面的测试
代码行量:154行
学习所花时间:1h
package com.example.memosystem.activity;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.example.memosystem.R;
public class yiyuceshi extends AppCompatActivity {
// 问题数组和对应分值数组
private String[] questions = {
"我感到消沉和无助",
"我对未来感到毫无希望",
"我觉得自己毫无价值",
"我失去了对生活的兴趣",
"我常常感到疲倦和缺乏精力",
"我睡眠不好或者睡眠过多",
"我食欲有明显改变",
"我情绪容易波动,经常感到焦虑或不安",
"我注意力不集中,记忆力下降",
"我有自杀念头或计划",
"我在过去两周内有过精神疲劳或无法集中精力的问题",
"我感到无助和孤独,缺乏人际关系",
"我感到控制不了自己的情绪",
"我心情低落和消沉,无法从中恢复",
"我对生活失去信心和动力?"
};
private int[][] scores = {
{1,0},
{1,0},
{1,0},
{1,0},
{1,0},
{1,0},
{1,0},
{1,0},
{1,0},
{2,0},
{1,0},
{1,0},
{1,0},
{1,0},
{1,0},
};
private int currentQuestionIndex = 0;
private int totalScore = 0;
// Define SharedPreferences key for storing total score
private static final String SCORE_KEY = "SCORE_KEY";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yiyuceshi);
// Load the total score from SharedPreferences
SharedPreferences sharedPref = getPreferences(MODE_PRIVATE);
totalScore = sharedPref.getInt(SCORE_KEY, 0);
final TextView questionTextView = findViewById(R.id.questionTextView);
final RadioGroup answerRadioGroup = findViewById(R.id.answerRadioGroup);
final Button submitButton = findViewById(R.id.submitButton);
// Show the first question
showQuestion(questionTextView, answerRadioGroup);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedRadioButtonId = answerRadioGroup.getCheckedRadioButtonId();
if (selectedRadioButtonId != -1) {
RadioButton selectedRadioButton = findViewById(selectedRadioButtonId);
int selectedAnswerIndex = answerRadioGroup.indexOfChild(selectedRadioButton);
totalScore += scores[currentQuestionIndex][selectedAnswerIndex];
currentQuestionIndex++;
if (currentQuestionIndex < questions.length) {
showQuestion(questionTextView, answerRadioGroup);
} else {
showResult();
}
// Save the updated total score to SharedPreferences
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(SCORE_KEY, totalScore);
editor.apply();
}
}
});
}
private String[][] options = {
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
};
private void showQuestion(TextView questionTextView, RadioGroup answerRadioGroup) {
questionTextView.setText(questions[currentQuestionIndex]);
answerRadioGroup.removeAllViews(); // Clear previous options
// Add options for the current question
for (String optionText : options[currentQuestionIndex]) {
RadioButton radioButton = new RadioButton(this);
radioButton.setText(optionText);
answerRadioGroup.addView(radioButton);
}
}
private void showResult() {
String resultText;
if (totalScore >= 0 && totalScore <= 5) {
resultText = "情绪状态稳定,没有出现明显的抑郁倾向。";
} else if (totalScore >= 6 && totalScore <= 10) {
resultText = "可能存在一定程度的抑郁症状,建议密切关注情绪波动并积极寻求支持与辅导。";
} else {
resultText = "存在明显的抑郁倾向,建议寻求专业心理咨询或心理治疗,以获得更深层次的支持和帮助。";
}
// Display the result on screen
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("抑郁测试评估结果");
builder.setMessage(resultText);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing, just close the dialog
}
});
builder.show();
}
}
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.example.memosystem.R;
public class yiyuceshi extends AppCompatActivity {
// 问题数组和对应分值数组
private String[] questions = {
"我感到消沉和无助",
"我对未来感到毫无希望",
"我觉得自己毫无价值",
"我失去了对生活的兴趣",
"我常常感到疲倦和缺乏精力",
"我睡眠不好或者睡眠过多",
"我食欲有明显改变",
"我情绪容易波动,经常感到焦虑或不安",
"我注意力不集中,记忆力下降",
"我有自杀念头或计划",
"我在过去两周内有过精神疲劳或无法集中精力的问题",
"我感到无助和孤独,缺乏人际关系",
"我感到控制不了自己的情绪",
"我心情低落和消沉,无法从中恢复",
"我对生活失去信心和动力?"
};
private int[][] scores = {
{1,0},
{1,0},
{1,0},
{1,0},
{1,0},
{1,0},
{1,0},
{1,0},
{1,0},
{2,0},
{1,0},
{1,0},
{1,0},
{1,0},
{1,0},
};
private int currentQuestionIndex = 0;
private int totalScore = 0;
// Define SharedPreferences key for storing total score
private static final String SCORE_KEY = "SCORE_KEY";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yiyuceshi);
// Load the total score from SharedPreferences
SharedPreferences sharedPref = getPreferences(MODE_PRIVATE);
totalScore = sharedPref.getInt(SCORE_KEY, 0);
final TextView questionTextView = findViewById(R.id.questionTextView);
final RadioGroup answerRadioGroup = findViewById(R.id.answerRadioGroup);
final Button submitButton = findViewById(R.id.submitButton);
// Show the first question
showQuestion(questionTextView, answerRadioGroup);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedRadioButtonId = answerRadioGroup.getCheckedRadioButtonId();
if (selectedRadioButtonId != -1) {
RadioButton selectedRadioButton = findViewById(selectedRadioButtonId);
int selectedAnswerIndex = answerRadioGroup.indexOfChild(selectedRadioButton);
totalScore += scores[currentQuestionIndex][selectedAnswerIndex];
currentQuestionIndex++;
if (currentQuestionIndex < questions.length) {
showQuestion(questionTextView, answerRadioGroup);
} else {
showResult();
}
// Save the updated total score to SharedPreferences
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(SCORE_KEY, totalScore);
editor.apply();
}
}
});
}
private String[][] options = {
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
{"是", "否"},
};
private void showQuestion(TextView questionTextView, RadioGroup answerRadioGroup) {
questionTextView.setText(questions[currentQuestionIndex]);
answerRadioGroup.removeAllViews(); // Clear previous options
// Add options for the current question
for (String optionText : options[currentQuestionIndex]) {
RadioButton radioButton = new RadioButton(this);
radioButton.setText(optionText);
answerRadioGroup.addView(radioButton);
}
}
private void showResult() {
String resultText;
if (totalScore >= 0 && totalScore <= 5) {
resultText = "情绪状态稳定,没有出现明显的抑郁倾向。";
} else if (totalScore >= 6 && totalScore <= 10) {
resultText = "可能存在一定程度的抑郁症状,建议密切关注情绪波动并积极寻求支持与辅导。";
} else {
resultText = "存在明显的抑郁倾向,建议寻求专业心理咨询或心理治疗,以获得更深层次的支持和帮助。";
}
// Display the result on screen
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("抑郁测试评估结果");
builder.setMessage(resultText);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing, just close the dialog
}
});
builder.show();
}
}