主页面写了好多代码和函数,显得很乱,想把写的一些函数集中放在另一个文件,但是好像写的函数关联太多东西,单独调走生成无数警告😅,不知道是不是我的方法不对(就是创建一个新的.kt文件,然后把fun丢进去就完事),源代码网址:https://github.com/UnaAtadura-Eris/MedicaRecord
想要移动的代码具体如下:
private fun faRe() {
showQuestion(questions_faRe[currentQuestionIndex])
nextButton.setOnClickListener {
val currentQuestion = questions_faRe[currentQuestionIndex]
if (currentQuestion.type == QuestionType.SINGLE_CHOICE) {
val selectedOptionId = optionsRadioGroup.checkedRadioButtonId
val selectedIndex =
optionsRadioGroup.indexOfChild(findViewById<RadioButton>(selectedOptionId))
if (selectedIndex == -1) {
// 提示用户选择一个选项
currentQuestionIndex -= 1
Toast.makeText(this, "请选择一个选项", Toast.LENGTH_SHORT).show()
} else{
// 对选中的单选题选项进行处理
// 例如,根据选中的选项执行相应的操作
when (val selectedOption = currentQuestion.options[selectedIndex]) {
"发热程度详解" -> {
val detailedExplanation = faRechengDuxiangJie
detailedExplanationTextView.text = detailedExplanation
currentQuestionIndex -= 1
}
"发热的特点详解" -> {
val detailedExplanation = reXingxiangJie
detailedExplanationTextView.text = detailedExplanation
currentQuestionIndex -= 1
}
"痰的颜色性质详解" -> {
val detailedExplanation = tanYanseXiangjie
detailedExplanationTextView.text = detailedExplanation
currentQuestionIndex -= 1
}
"痰量及咯出详解" -> {
val detailedExplanation = tanLiangxiangJie
detailedExplanationTextView.text = detailedExplanation
currentQuestionIndex -= 1
}
"皮疹特点详解" -> {
val detailedExplanation = piZhenxiangJie
detailedExplanationTextView.text = detailedExplanation
currentQuestionIndex -= 1
}
else -> {
// 这里可以根据需求将 answerToRecord 存储到适当的数据结构中
resultList.add("$selectedOption,")
val resultString = resultList.joinToString("")
answerToRecordTextView.text = resultString
}
}
}
} else if (currentQuestion.type == QuestionType.MULTI_CHOICE) {
val selectedOptions = mutableListOf<String>()
val unselectedOptions = mutableListOf<String>()
for (i in 0 until optionsRadioGroup.childCount) {
val checkBox = optionsRadioGroup.getChildAt(i) as CheckBox
val option = currentQuestion.options[i]
if (checkBox.isChecked) {
selectedOptions.add(option)
} else {
unselectedOptions.add(option)
}
}
val selectedOptionsText = if (selectedOptions.isNotEmpty()) {
"有" + selectedOptions.joinToString("、")
} else {
""
}
val unselectedOptionsText = if (unselectedOptions.isNotEmpty()) {
"无" + unselectedOptions.joinToString("、")
} else {
""
}
val answerToRecord = "$selectedOptionsText,$unselectedOptionsText。"
// 这里可以根据需求将 answerToRecord 存储到适当的数据结构中
// answerToRecordTextView.append(answerToRecord)
resultList.add(answerToRecord)
val resultString = resultList.joinToString("")
answerToRecordTextView.text = resultString
// 对选中的多选题选项进行处理
// 例如,根据选中的选项执行相应的操作
for (selectedOption in selectedOptions) {
when (selectedOption) {
"咳痰" -> {
val newquestions = listOf(
Question(
"痰的颜色性质",
listOf(
"白黏痰",
"黄脓痰",
"绿色痰",
"浓臭痰,痰液分4层",
"浓臭痰,痰液分3层",
"颜色不详",
"痰的颜色性质详解"
),
QuestionType.SINGLE_CHOICE,
emptyList()
),
Question(
"痰的量多多少及是否易咯出",
listOf("量多易咯", "量少难咯", "痰量及咯出详解"),
QuestionType.SINGLE_CHOICE,
emptyList()
)
)
questions_faRe = questions_faRe.plus(newquestions)
}
"皮疹" -> {
val newquestions = listOf(
Question(
"皮疹特点",
listOf(
"发热1天后出疹",
"发热5天至1周出疹",
"发热伴有环形红斑或结节性红斑",
"皮疹特点详解"
),
QuestionType.SINGLE_CHOICE,
emptyList()
)
)
questions_faRe = questions_faRe.plus(newquestions)
}
else -> {
// currentQuestionIndex += 0
// answerToRecordTextView.append("雷狼龙3")
}
}
}
}
currentQuestionIndex++
if (currentQuestionIndex < questions_faRe.size) {
showQuestion(questions_faRe[currentQuestionIndex])
} else {
nextButton.setText("结束")
nextButton.isEnabled = false
}
}
resetButton.setOnClickListener {
resetPage(questions_faRe)
}
returnButton.setOnClickListener {
returnPage(questions_faRe)
}
}
这个问题主要涉及到Kotlin的类和对象。您可以创建一个新的.kt文件,然后在这个文件中创建一个新的类。这个类应该包含您想移动的函数。您还需要将函数中使用的一些变量和对象作为类的属性,这样您的函数就可以正常工作。然后,你在主页面通过创建该类的实例来调用这个函数。
以你给出的代码为例,我们可以创建一个名为QuestionHandler
的类,将faRe
函数放在这个类中,然后将它需要的变量和对象作为QuestionHandler
的属性:
class QuestionHandler(private val activity: Activity,
private val nextButton: Button,
private val resetButton: Button,
private val returnButton: Button,
private val optionsRadioGroup: RadioGroup,
private val detailedExplanationTextView: TextView,
private val answerToRecordTextView: TextView,
private var questions_faRe: List<Question>,
private var currentQuestionIndex: Int,
private val resultList: MutableList<String>) {
// 你的faRe函数放在这里
fun faRe() {
// 省略faRe函数的具体实现...
}
// showQuestion、resetPage、returnPage等你在faRe函数里使用到的其它函数也应该放在这个类中
// 并且这些函数需要访问的变量和对象也需要作为QuestionHandler类的属性
}
然后在你的主页面的代码中,你可以创建QuestionHandler
的实例,并调用faRe
函数:
val questionHandler = QuestionHandler(
this,
nextButton,
resetButton,
returnButton,
optionsRadioGroup,
detailedExplanationTextView,
answerToRecordTextView,
questions_faRe,
currentQuestionIndex,
resultList
)
questionHandler.faRe()
注意:你可能需要修改QuestionHandler
的构造函数以及faRe
函数的访问修饰符,以便它们可以在你的主页面的代码中被访问。同时,如果faRe
函数中使用到的变量和对象是在主页面的其它函数中被修改的,那么你需要把它们设置为QuestionHandler
的属性,并在创建QuestionHandler
的实例时传入正确的值。
以上只是一个基本的实现方式,可能需要根据你的实际情况进行适当的修改。在实际的开发过程中,我们通常会使用一些设计模式,如MVP、MVVM等,来更好地组织我们的代码。