|
|
@@ -1,19 +1,81 @@
|
|
|
package com.yingyangfly.moca.omputation
|
|
|
|
|
|
+import android.os.Bundle
|
|
|
import androidx.core.os.bundleOf
|
|
|
import androidx.navigation.Navigation
|
|
|
+import androidx.recyclerview.widget.GridLayoutManager
|
|
|
import com.yingyang.moca.R
|
|
|
import com.yingyang.moca.databinding.FragmentComputationBinding
|
|
|
import com.yingyangfly.baselib.base.BaseFragment
|
|
|
+import com.yingyangfly.baselib.db.QuestionsBean
|
|
|
import com.yingyangfly.baselib.ext.setOnSingleClickListener
|
|
|
+import com.yingyangfly.baselib.ext.toast
|
|
|
+import com.yingyangfly.moca.adapter.NumberAdapter
|
|
|
|
|
|
/**
|
|
|
* 计算题
|
|
|
*/
|
|
|
class ComputationFragment : BaseFragment<FragmentComputationBinding>() {
|
|
|
|
|
|
- override fun initViews() {
|
|
|
+ /**
|
|
|
+ * 问题id
|
|
|
+ */
|
|
|
+ var questionId = 0
|
|
|
+
|
|
|
+ var choiceItems = ""
|
|
|
+ private val numberList = mutableListOf<String>()
|
|
|
+ private val adapter by lazy { NumberAdapter() }
|
|
|
+
|
|
|
+ private var firstquestion: QuestionsBean? = null
|
|
|
+ private var secondQuestion: QuestionsBean? = null
|
|
|
+ private var thirdQuestion: QuestionsBean? = null
|
|
|
+ private var fourthQuestion: QuestionsBean? = null
|
|
|
+ private var fifthQuestion: QuestionsBean? = null
|
|
|
+
|
|
|
+ private var questionOne = false
|
|
|
+ private var questionTwo = false
|
|
|
+ private var questionThree = false
|
|
|
+ private var questionFour = false
|
|
|
+ private var questionFive = false
|
|
|
+
|
|
|
+ private val stringBufferOne = StringBuffer()
|
|
|
+ private val stringBufferTwo = StringBuffer()
|
|
|
+ private val stringBufferThree = StringBuffer()
|
|
|
+ private val stringBufferFour = StringBuffer()
|
|
|
+ private val stringBufferFive = StringBuffer()
|
|
|
|
|
|
+ override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
+ questionId = arguments?.getInt("questionId") ?: 52
|
|
|
+ choiceItems = arguments?.getString("choiceItems") ?: ""
|
|
|
+ super.onCreate(savedInstanceState)
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun initViews() {
|
|
|
+ binding {
|
|
|
+ numberList.clear()
|
|
|
+ for (i in 1..9) {
|
|
|
+ numberList.add(i.toString())
|
|
|
+ }
|
|
|
+ numberList.add("删除")
|
|
|
+ numberList.add("0")
|
|
|
+ numberList.add("确定")
|
|
|
+ rvInput.layoutManager = GridLayoutManager(mContext, 3)
|
|
|
+ rvInput.adapter = adapter
|
|
|
+ adapter.setData(numberList)
|
|
|
+ adapter.onNumClickListener = { bean: String, type: String ->
|
|
|
+ when (type) {
|
|
|
+ "0" -> {
|
|
|
+ append(bean)
|
|
|
+ }
|
|
|
+ "1" -> {
|
|
|
+ delete()
|
|
|
+ }
|
|
|
+ "2" -> {
|
|
|
+ save()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
override fun initListener() {
|
|
|
@@ -30,10 +92,387 @@ class ComputationFragment : BaseFragment<FragmentComputationBinding>() {
|
|
|
btnNext.setOnSingleClickListener {
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ editResultOne.setOnFocusChangeListener { v, hasFocus ->
|
|
|
+ questionOne = hasFocus
|
|
|
+ binding.editResultOne.postDelayed(
|
|
|
+ { closeKeyBord(binding.editResultOne, mContext) },
|
|
|
+ 300
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ editResultTwo.setOnFocusChangeListener { v, hasFocus ->
|
|
|
+ questionTwo = hasFocus
|
|
|
+ binding.editResultTwo.postDelayed(
|
|
|
+ { closeKeyBord(binding.editResultTwo, mContext) },
|
|
|
+ 300
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ editResultThree.setOnFocusChangeListener { v, hasFocus ->
|
|
|
+ questionThree = hasFocus
|
|
|
+ binding.editResultThree.postDelayed(
|
|
|
+ { closeKeyBord(binding.editResultThree, mContext) },
|
|
|
+ 300
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ editResultFour.setOnFocusChangeListener { v, hasFocus ->
|
|
|
+ questionFour = hasFocus
|
|
|
+ binding.editResultFour.postDelayed(
|
|
|
+ { closeKeyBord(binding.editResultFour, mContext) },
|
|
|
+ 300
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ editResultFive.setOnFocusChangeListener { v, hasFocus ->
|
|
|
+ questionFive = hasFocus
|
|
|
+ binding.editResultFive.postDelayed(
|
|
|
+ { closeKeyBord(binding.editResultFive, mContext) },
|
|
|
+ 300
|
|
|
+ )
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
override fun initData() {
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ override fun onResume() {
|
|
|
+ super.onResume()
|
|
|
+ loadData()
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 加载数据
|
|
|
+ */
|
|
|
+ private fun loadData() {
|
|
|
+ binding.editResultOne.requestFocus()
|
|
|
+ binding.editResultOne.isFocusable = true
|
|
|
+ binding.editResultOne.isFocusableInTouchMode = true
|
|
|
+ binding.editResultOne.postDelayed({ closeKeyBord(binding.editResultOne, mContext) }, 300)
|
|
|
+ if (dao != null) {
|
|
|
+ firstquestion = dao?.getQuestion(questionId)
|
|
|
+ if (firstquestion != null && firstquestion!!.inputString.isNullOrEmpty().not()) {
|
|
|
+ binding.editResultOne.post {
|
|
|
+ if (stringBufferOne.toString().isNullOrEmpty().not()) {
|
|
|
+ stringBufferOne.delete(0, firstquestion!!.inputString.length - 1)
|
|
|
+ }
|
|
|
+ stringBufferOne.append(firstquestion!!.inputString)
|
|
|
+ binding.editResultOne.setText(firstquestion!!.inputString)
|
|
|
+ binding.editResultOne.setSelection(firstquestion!!.inputString.length)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ val secondQuestionnId = questionId + 1
|
|
|
+ secondQuestion = dao?.getQuestion(secondQuestionnId)
|
|
|
+ if (secondQuestion != null && secondQuestion!!.inputString.isNullOrEmpty().not()) {
|
|
|
+ binding.editResultTwo.post {
|
|
|
+ if (stringBufferTwo.toString().isNullOrEmpty().not()) {
|
|
|
+ stringBufferTwo.delete(0, secondQuestion!!.inputString.length - 1)
|
|
|
+ }
|
|
|
+ stringBufferTwo.append(secondQuestion!!.inputString)
|
|
|
+ binding.editResultTwo.setText(secondQuestion!!.inputString)
|
|
|
+ binding.editResultTwo.setSelection(secondQuestion!!.inputString.length)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ val thirdQuestionId = questionId + 2
|
|
|
+ thirdQuestion = dao?.getQuestion(thirdQuestionId)
|
|
|
+ if (thirdQuestion != null && thirdQuestion!!.inputString.isNullOrEmpty().not()) {
|
|
|
+ binding.editResultThree.post {
|
|
|
+ if (stringBufferThree.toString().isNullOrEmpty().not()) {
|
|
|
+ stringBufferThree.delete(0, thirdQuestion!!.inputString.length - 1)
|
|
|
+ }
|
|
|
+ stringBufferThree.append(thirdQuestion!!.inputString)
|
|
|
+ binding.editResultThree.setText(thirdQuestion!!.inputString)
|
|
|
+ binding.editResultThree.setSelection(thirdQuestion!!.inputString.length)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ val fourthQuestionId = questionId + 3
|
|
|
+ fourthQuestion = dao?.getQuestion(fourthQuestionId)
|
|
|
+ if (fourthQuestion != null && fourthQuestion!!.inputString.isNullOrEmpty().not()) {
|
|
|
+ binding.editResultFour.post {
|
|
|
+ if (stringBufferFour.toString().isNullOrEmpty().not()) {
|
|
|
+ stringBufferFour.delete(0, fourthQuestion!!.inputString.length - 1)
|
|
|
+ }
|
|
|
+ stringBufferFour.append(fourthQuestion!!.inputString)
|
|
|
+ binding.editResultFour.setText(fourthQuestion!!.inputString)
|
|
|
+ binding.editResultFour.setSelection(fourthQuestion!!.inputString.length)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ val fifthQuestionId = questionId + 4
|
|
|
+ fifthQuestion = dao?.getQuestion(fifthQuestionId)
|
|
|
+ if (fifthQuestion != null && fifthQuestion!!.inputString.isNullOrEmpty().not()) {
|
|
|
+ binding.editResultFive.post {
|
|
|
+ if (stringBufferFive.toString().isNullOrEmpty().not()) {
|
|
|
+ stringBufferFive.delete(0, fifthQuestion!!.inputString.length - 1)
|
|
|
+ }
|
|
|
+ stringBufferFive.append(fifthQuestion!!.inputString)
|
|
|
+ binding.editResultFive.setText(fifthQuestion!!.inputString)
|
|
|
+ binding.editResultFive.setSelection(fifthQuestion!!.inputString.length)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 非空判断
|
|
|
+ */
|
|
|
+ private fun judge(): Boolean {
|
|
|
+ if (dao != null) {
|
|
|
+ if (binding.editResultOne.text.toString().trim().isNullOrEmpty()) {
|
|
|
+ "请输入第一道题目的答案".toast()
|
|
|
+ binding.editResultOne.requestFocus()
|
|
|
+ binding.editResultOne.isFocusable = true
|
|
|
+ binding.editResultOne.isFocusableInTouchMode = true
|
|
|
+ binding.editResultOne.postDelayed(
|
|
|
+ { closeKeyBord(binding.editResultOne, mContext) },
|
|
|
+ 300
|
|
|
+ )
|
|
|
+ return false
|
|
|
+ } else {
|
|
|
+ if (firstquestion != null) {
|
|
|
+ firstquestion?.inputString = binding.editResultOne.text.toString().trim()
|
|
|
+ firstquestion!!.reviewId = firstquestion!!.id
|
|
|
+ firstquestion!!.reviewAnswer = binding.editResultOne.text.toString().trim()
|
|
|
+ firstquestion!!.correct = ""
|
|
|
+ dao?.update(firstquestion!!)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (binding.editResultTwo.text.toString().trim().isNullOrEmpty()) {
|
|
|
+ "请输入第二道题目的答案".toast()
|
|
|
+ binding.editResultTwo.requestFocus()
|
|
|
+ binding.editResultTwo.isFocusable = true
|
|
|
+ binding.editResultTwo.isFocusableInTouchMode = true
|
|
|
+ binding.editResultTwo.postDelayed(
|
|
|
+ { closeKeyBord(binding.editResultOne, mContext) },
|
|
|
+ 300
|
|
|
+ )
|
|
|
+ return false
|
|
|
+ } else {
|
|
|
+ if (secondQuestion != null) {
|
|
|
+ secondQuestion?.inputString = binding.editResultTwo.text.toString().trim()
|
|
|
+ secondQuestion!!.reviewId = secondQuestion!!.id
|
|
|
+ secondQuestion!!.reviewAnswer = binding.editResultTwo.text.toString().trim()
|
|
|
+ secondQuestion!!.correct = ""
|
|
|
+ dao?.update(secondQuestion!!)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (binding.editResultThree.text.toString().trim().isNullOrEmpty()) {
|
|
|
+ "请输入第三道题目的答案".toast()
|
|
|
+ binding.editResultThree.requestFocus()
|
|
|
+ binding.editResultThree.isFocusable = true
|
|
|
+ binding.editResultThree.isFocusableInTouchMode = true
|
|
|
+ binding.editResultThree.postDelayed({
|
|
|
+ closeKeyBord(
|
|
|
+ binding.editResultOne,
|
|
|
+ mContext
|
|
|
+ )
|
|
|
+ }, 300)
|
|
|
+ return false
|
|
|
+ } else {
|
|
|
+ if (thirdQuestion != null) {
|
|
|
+ thirdQuestion?.inputString = binding.editResultThree.text.toString().trim()
|
|
|
+ thirdQuestion!!.reviewId = thirdQuestion!!.id
|
|
|
+ thirdQuestion!!.reviewAnswer = binding.editResultThree.text.toString().trim()
|
|
|
+ thirdQuestion!!.correct = ""
|
|
|
+ dao?.update(thirdQuestion!!)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (binding.editResultFour.text.toString().trim().isNullOrEmpty()) {
|
|
|
+ "请输入第四道题目的答案".toast()
|
|
|
+ binding.editResultFour.requestFocus()
|
|
|
+ binding.editResultFour.isFocusable = true
|
|
|
+ binding.editResultFour.isFocusableInTouchMode = true
|
|
|
+ binding.editResultFour.postDelayed({
|
|
|
+ closeKeyBord(
|
|
|
+ binding.editResultOne,
|
|
|
+ mContext
|
|
|
+ )
|
|
|
+ }, 300)
|
|
|
+ return false
|
|
|
+ } else {
|
|
|
+ if (fourthQuestion != null) {
|
|
|
+ fourthQuestion?.inputString = binding.editResultFour.text.toString().trim()
|
|
|
+ fourthQuestion!!.reviewId = fourthQuestion!!.id
|
|
|
+ fourthQuestion!!.reviewAnswer = binding.editResultFour.text.toString().trim()
|
|
|
+ fourthQuestion!!.correct = ""
|
|
|
+ dao?.update(fourthQuestion!!)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (binding.editResultFive.text.toString().trim().isNullOrEmpty()) {
|
|
|
+ "请输入第五道题目的答案".toast()
|
|
|
+ binding.editResultFive.requestFocus()
|
|
|
+ binding.editResultFive.isFocusable = true
|
|
|
+ binding.editResultFive.isFocusableInTouchMode = true
|
|
|
+ binding.editResultFive.postDelayed({
|
|
|
+ closeKeyBord(
|
|
|
+ binding.editResultOne,
|
|
|
+ mContext
|
|
|
+ )
|
|
|
+ }, 300)
|
|
|
+ return false
|
|
|
+ } else {
|
|
|
+ if (fifthQuestion != null) {
|
|
|
+ fifthQuestion?.inputString = binding.editResultFive.text.toString().trim()
|
|
|
+ fifthQuestion!!.reviewId = fifthQuestion!!.id
|
|
|
+ fifthQuestion!!.reviewAnswer = binding.editResultFive.text.toString().trim()
|
|
|
+ fifthQuestion!!.correct = ""
|
|
|
+ dao?.update(fifthQuestion!!)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加数据
|
|
|
+ */
|
|
|
+ private fun append(bean: String) {
|
|
|
+ if (questionOne) {
|
|
|
+ binding.editResultOne.post {
|
|
|
+ stringBufferOne.append(bean)
|
|
|
+ binding.editResultOne.setText(stringBufferOne.toString())
|
|
|
+ binding.editResultOne.setSelection(stringBufferOne.toString().length)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (questionTwo) {
|
|
|
+ binding.editResultTwo.post {
|
|
|
+ stringBufferTwo.append(bean)
|
|
|
+ binding.editResultTwo.setText(stringBufferTwo.toString())
|
|
|
+ binding.editResultTwo.setSelection(stringBufferTwo.toString().length)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (questionThree) {
|
|
|
+ binding.editResultThree.post {
|
|
|
+ stringBufferThree.append(bean)
|
|
|
+ binding.editResultThree.setText(stringBufferThree.toString())
|
|
|
+ binding.editResultThree.setSelection(stringBufferThree.toString().length)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (questionFour) {
|
|
|
+ binding.editResultFour.post {
|
|
|
+ stringBufferFour.append(bean)
|
|
|
+ binding.editResultFour.setText(stringBufferFour.toString())
|
|
|
+ binding.editResultFour.setSelection(stringBufferFour.toString().length)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (questionFive) {
|
|
|
+ binding.editResultFive.post {
|
|
|
+ stringBufferFive.append(bean)
|
|
|
+ binding.editResultFive.setText(stringBufferFive.toString())
|
|
|
+ binding.editResultFive.setSelection(stringBufferFive.toString().length)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除数据
|
|
|
+ */
|
|
|
+ private fun delete() {
|
|
|
+ if (questionOne) {
|
|
|
+ binding.editResultOne.post {
|
|
|
+ if (stringBufferOne.toString().isNullOrEmpty().not()) {
|
|
|
+ val size = stringBufferOne.toString().length - 1
|
|
|
+ stringBufferOne.deleteCharAt(size)
|
|
|
+ binding.editResultOne.setText(stringBufferOne.toString())
|
|
|
+ binding.editResultOne.setSelection(stringBufferOne.toString().length)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (questionTwo) {
|
|
|
+ binding.editResultTwo.post {
|
|
|
+ if (stringBufferTwo.toString().isNullOrEmpty().not()) {
|
|
|
+ val size = stringBufferTwo.toString().length - 1
|
|
|
+ stringBufferTwo.deleteCharAt(size)
|
|
|
+ binding.editResultTwo.setText(stringBufferTwo.toString())
|
|
|
+ binding.editResultTwo.setSelection(stringBufferTwo.toString().length)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (questionThree) {
|
|
|
+ binding.editResultThree.post {
|
|
|
+ if (stringBufferThree.toString().isNullOrEmpty().not()) {
|
|
|
+ val size = stringBufferThree.toString().length - 1
|
|
|
+ stringBufferThree.deleteCharAt(size)
|
|
|
+ binding.editResultThree.setText(stringBufferThree.toString())
|
|
|
+ binding.editResultThree.setSelection(stringBufferThree.toString().length)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (questionFour) {
|
|
|
+ binding.editResultFour.post {
|
|
|
+ if (stringBufferFour.toString().isNullOrEmpty().not()) {
|
|
|
+ val size = stringBufferFour.toString().length - 1
|
|
|
+ stringBufferFour.deleteCharAt(size)
|
|
|
+ binding.editResultFour.setText(stringBufferFour.toString())
|
|
|
+ binding.editResultFour.setSelection(stringBufferFour.toString().length)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (questionFive) {
|
|
|
+ binding.editResultFive.post {
|
|
|
+ if (stringBufferFive.toString().isNullOrEmpty().not()) {
|
|
|
+ val size = stringBufferFive.toString().length - 1
|
|
|
+ stringBufferFive.deleteCharAt(size)
|
|
|
+ binding.editResultFive.setText(stringBufferFive.toString())
|
|
|
+ binding.editResultFive.setSelection(stringBufferFive.toString().length)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存数据
|
|
|
+ */
|
|
|
+ private fun save() {
|
|
|
+ if (dao != null) {
|
|
|
+ if (questionOne) {
|
|
|
+ if (firstquestion != null) {
|
|
|
+ firstquestion!!.inputString = binding.editResultOne.toString().trim()
|
|
|
+ firstquestion!!.reviewId = firstquestion!!.id
|
|
|
+ firstquestion!!.reviewAnswer = binding.editResultOne.toString().trim()
|
|
|
+ firstquestion!!.correct = ""
|
|
|
+ dao?.update(firstquestion!!)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (questionTwo) {
|
|
|
+ if (secondQuestion != null) {
|
|
|
+ secondQuestion!!.inputString = binding.editResultTwo.toString().trim()
|
|
|
+ secondQuestion!!.reviewId = secondQuestion!!.id
|
|
|
+ secondQuestion!!.reviewAnswer = binding.editResultTwo.toString().trim()
|
|
|
+ secondQuestion!!.correct = ""
|
|
|
+ dao?.update(secondQuestion!!)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (questionThree) {
|
|
|
+ if (thirdQuestion != null) {
|
|
|
+ thirdQuestion!!.inputString = binding.editResultThree.toString().trim()
|
|
|
+ thirdQuestion!!.reviewId = thirdQuestion!!.id
|
|
|
+ thirdQuestion!!.reviewAnswer = binding.editResultThree.toString().trim()
|
|
|
+ thirdQuestion!!.correct = ""
|
|
|
+ dao?.update(thirdQuestion!!)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (questionFour) {
|
|
|
+ if (fourthQuestion != null) {
|
|
|
+ fourthQuestion!!.inputString = binding.editResultFour.toString().trim()
|
|
|
+ fourthQuestion!!.reviewId = fourthQuestion!!.id
|
|
|
+ fourthQuestion!!.reviewAnswer = binding.editResultFour.toString().trim()
|
|
|
+ fourthQuestion!!.correct = ""
|
|
|
+ dao?.update(fourthQuestion!!)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (questionFive) {
|
|
|
+ if (fifthQuestion != null) {
|
|
|
+ fifthQuestion!!.inputString = binding.editResultFive.toString().trim()
|
|
|
+ fifthQuestion!!.reviewId = fifthQuestion!!.id
|
|
|
+ fifthQuestion!!.reviewAnswer = binding.editResultFive.toString().trim()
|
|
|
+ fifthQuestion!!.correct = ""
|
|
|
+ dao?.update(fifthQuestion!!)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|