5.10
与小组成员讨论如何完善咨询中的自我测试页面的测试
代码行量:159行
学习所花时间: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 xinggeceshi extends AppCompatActivity {
// 问题数组和对应分值数组
private String[] questions = {
"在社交活动中,我更喜欢",
"我更倾向于",
"我认为自己更多的是",
"在工作中,我更注重",
"我更喜欢",
"我更倾向于",
"我在生活中更重视",
"我更喜欢",
"当面对冲突时,我更倾向于",
"我更容易被哪种类型的人吸引",
"我更倾向于",
"我更倾向于",
"我在处理问题时更偏向于",
"我更希望自己",
"我更愿意"
};
private int[][] scores = {
{1,2},
{2,1},
{2,1},
{2,1},
{1,2},
{2,1},
{1,2},
{2,1},
{1,2},
{1,2},
{2,1},
{1,2},
{1,2},
{1,2},
{2,1},
};
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_xinggeceshi);
// 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 >= 15 && totalScore <= 20) {
resultText = "您可能具有外向、开放、寻求刺激的性格特质,并表现出较高的社交取向和活力。";
} else if (totalScore >= 21 && totalScore <= 30) {
resultText = "您可能具有理性、深思熟虑、冷静分析的性格特征,展现出稳定而务实的思维方式。";
}else if (totalScore >= 31 && totalScore <= 45) {
resultText = "您可能呈现内向、审慎、趋向于稳定和安逸的性格特质,更倾向于独立思考和独处。";
}else if (totalScore >= 46 && totalScore <= 60) {
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 xinggeceshi extends AppCompatActivity {
// 问题数组和对应分值数组
private String[] questions = {
"在社交活动中,我更喜欢",
"我更倾向于",
"我认为自己更多的是",
"在工作中,我更注重",
"我更喜欢",
"我更倾向于",
"我在生活中更重视",
"我更喜欢",
"当面对冲突时,我更倾向于",
"我更容易被哪种类型的人吸引",
"我更倾向于",
"我更倾向于",
"我在处理问题时更偏向于",
"我更希望自己",
"我更愿意"
};
private int[][] scores = {
{1,2},
{2,1},
{2,1},
{2,1},
{1,2},
{2,1},
{1,2},
{2,1},
{1,2},
{1,2},
{2,1},
{1,2},
{1,2},
{1,2},
{2,1},
};
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_xinggeceshi);
// 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 >= 15 && totalScore <= 20) {
resultText = "您可能具有外向、开放、寻求刺激的性格特质,并表现出较高的社交取向和活力。";
} else if (totalScore >= 21 && totalScore <= 30) {
resultText = "您可能具有理性、深思熟虑、冷静分析的性格特征,展现出稳定而务实的思维方式。";
}else if (totalScore >= 31 && totalScore <= 45) {
resultText = "您可能呈现内向、审慎、趋向于稳定和安逸的性格特质,更倾向于独立思考和独处。";
}else if (totalScore >= 46 && totalScore <= 60) {
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();
}
}